blob: 124852bcf6fe0fcfef3559bfbe2af43d0bba8f80 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * inode.c
3 *
4 * PURPOSE
5 * Inode handling routines for the OSTA-UDF(tm) filesystem.
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * COPYRIGHT
8 * This file is distributed under the terms of the GNU General Public
9 * License (GPL). Copies of the GPL can be obtained from:
10 * ftp://prep.ai.mit.edu/pub/gnu/GPL
11 * Each contributing author retains all rights to their own work.
12 *
13 * (C) 1998 Dave Boynton
14 * (C) 1998-2004 Ben Fennema
15 * (C) 1999-2000 Stelias Computing Inc
16 *
17 * HISTORY
18 *
19 * 10/04/98 dgb Added rudimentary directory functions
20 * 10/07/98 Fully working udf_block_map! It works!
21 * 11/25/98 bmap altered to better support extents
Marcin Slusarz4b111112008-02-08 04:20:36 -080022 * 12/06/98 blf partition support in udf_iget, udf_block_map
23 * and udf_read_inode
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * 12/12/98 rewrote udf_block_map to handle next extents and descs across
25 * block boundaries (which is not actually allowed)
26 * 12/20/98 added support for strategy 4096
27 * 03/07/99 rewrote udf_block_map (again)
28 * New funcs, inode_bmap, udf_next_aext
29 * 04/19/99 Support for writing device EA's for major/minor #
30 */
31
32#include "udfdecl.h"
33#include <linux/mm.h>
34#include <linux/smp_lock.h>
35#include <linux/module.h>
36#include <linux/pagemap.h>
37#include <linux/buffer_head.h>
38#include <linux/writeback.h>
39#include <linux/slab.h>
Bob Copelandf845fce2008-04-17 09:47:48 +020040#include <linux/crc-itu-t.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include "udf_i.h"
43#include "udf_sb.h"
44
45MODULE_AUTHOR("Ben Fennema");
46MODULE_DESCRIPTION("Universal Disk Format Filesystem");
47MODULE_LICENSE("GPL");
48
49#define EXTENT_MERGE_SIZE 5
50
51static mode_t udf_convert_permissions(struct fileEntry *);
52static int udf_update_inode(struct inode *, int);
53static void udf_fill_inode(struct inode *, struct buffer_head *);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -070054static int udf_alloc_i_data(struct inode *inode, size_t size);
Jan Kara60448b12007-05-08 00:35:13 -070055static struct buffer_head *inode_getblk(struct inode *, sector_t, int *,
Marcin Slusarz1ed16172008-02-08 04:20:48 -080056 sector_t *, int *);
Jan Karaff116fc2007-05-08 00:35:14 -070057static int8_t udf_insert_aext(struct inode *, struct extent_position,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020058 struct kernel_lb_addr, uint32_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059static void udf_split_extents(struct inode *, int *, int, int,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020060 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static void udf_prealloc_extents(struct inode *, int, int,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020062 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static void udf_merge_extents(struct inode *,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020064 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static void udf_update_extents(struct inode *,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020066 struct kernel_long_ad[EXTENT_MERGE_SIZE], int, int,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070067 struct extent_position *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static int udf_get_block(struct inode *, sector_t, struct buffer_head *, int);
69
Christoph Hellwigb1e32122008-02-22 12:38:48 +010070
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070071void udf_delete_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Mark Fashehfef26652005-09-09 13:01:31 -070073 truncate_inode_pages(&inode->i_data, 0);
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 if (is_bad_inode(inode))
76 goto no_delete;
77
78 inode->i_size = 0;
79 udf_truncate(inode);
80 lock_kernel();
81
82 udf_update_inode(inode, IS_SYNC(inode));
83 udf_free_inode(inode);
84
85 unlock_kernel();
86 return;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070087
88no_delete:
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 clear_inode(inode);
90}
91
Jan Kara74584ae2007-06-16 10:16:14 -070092/*
Jan Kara81056dd2009-07-16 18:02:25 +020093 * If we are going to release inode from memory, we truncate last inode extent
94 * to proper length. We could use drop_inode() but it's called under inode_lock
95 * and thus we cannot mark inode dirty there. We use clear_inode() but we have
96 * to make sure to write inode as it's not written automatically.
Jan Kara74584ae2007-06-16 10:16:14 -070097 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070098void udf_clear_inode(struct inode *inode)
99{
Jan Kara2c948b32009-12-03 13:39:28 +0100100 struct udf_inode_info *iinfo = UDF_I(inode);
101
102 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB &&
103 inode->i_size != iinfo->i_lenExtents) {
104 printk(KERN_WARNING "UDF-fs (%s): Inode %lu (mode %o) has "
Daniel Mack1537a362010-01-29 15:57:49 +0800105 "inode size %llu different from extent length %llu. "
Jan Kara2c948b32009-12-03 13:39:28 +0100106 "Filesystem need not be standards compliant.\n",
107 inode->i_sb->s_id, inode->i_ino, inode->i_mode,
108 (unsigned long long)inode->i_size,
109 (unsigned long long)iinfo->i_lenExtents);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 }
Christoph Hellwig257ba152010-03-03 09:05:04 -0500111
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800112 kfree(iinfo->i_ext.i_data);
113 iinfo->i_ext.i_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
116static int udf_writepage(struct page *page, struct writeback_control *wbc)
117{
118 return block_write_full_page(page, udf_get_block, wbc);
119}
120
121static int udf_readpage(struct file *file, struct page *page)
122{
123 return block_read_full_page(page, udf_get_block);
124}
125
Nick Pigginbe021ee2007-10-16 01:25:20 -0700126static int udf_write_begin(struct file *file, struct address_space *mapping,
127 loff_t pos, unsigned len, unsigned flags,
128 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
Nick Pigginbe021ee2007-10-16 01:25:20 -0700130 *pagep = NULL;
131 return block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
132 udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
135static sector_t udf_bmap(struct address_space *mapping, sector_t block)
136{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700137 return generic_block_bmap(mapping, block, udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700140const struct address_space_operations udf_aops = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700141 .readpage = udf_readpage,
142 .writepage = udf_writepage,
143 .sync_page = block_sync_page,
Nick Pigginbe021ee2007-10-16 01:25:20 -0700144 .write_begin = udf_write_begin,
145 .write_end = generic_write_end,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700146 .bmap = udf_bmap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147};
148
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700149void udf_expand_file_adinicb(struct inode *inode, int newsize, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
151 struct page *page;
152 char *kaddr;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800153 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 struct writeback_control udf_wbc = {
155 .sync_mode = WB_SYNC_NONE,
156 .nr_to_write = 1,
157 };
158
159 /* from now on we have normal address_space methods */
160 inode->i_data.a_ops = &udf_aops;
161
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800162 if (!iinfo->i_lenAlloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800164 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 else
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800166 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 mark_inode_dirty(inode);
168 return;
169 }
170
171 page = grab_cache_page(inode->i_mapping, 0);
Matt Mackallcd7619d2005-05-01 08:59:01 -0700172 BUG_ON(!PageLocked(page));
173
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700174 if (!PageUptodate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 kaddr = kmap(page);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800176 memset(kaddr + iinfo->i_lenAlloc, 0x00,
177 PAGE_CACHE_SIZE - iinfo->i_lenAlloc);
178 memcpy(kaddr, iinfo->i_ext.i_data + iinfo->i_lenEAttr,
179 iinfo->i_lenAlloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 flush_dcache_page(page);
181 SetPageUptodate(page);
182 kunmap(page);
183 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800184 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0x00,
185 iinfo->i_lenAlloc);
186 iinfo->i_lenAlloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800188 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 else
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800190 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 inode->i_data.a_ops->writepage(page, &udf_wbc);
193 page_cache_release(page);
194
195 mark_inode_dirty(inode);
196}
197
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700198struct buffer_head *udf_expand_dir_adinicb(struct inode *inode, int *block,
199 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
201 int newblock;
Jan Karaff116fc2007-05-08 00:35:14 -0700202 struct buffer_head *dbh = NULL;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200203 struct kernel_lb_addr eloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 uint8_t alloctype;
Jan Karaff116fc2007-05-08 00:35:14 -0700205 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 struct udf_fileident_bh sfibh, dfibh;
Jan Karaaf793292008-02-08 04:20:50 -0800208 loff_t f_pos = udf_ext0_offset(inode);
209 int size = udf_ext0_offset(inode) + inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 struct fileIdentDesc cfi, *sfi, *dfi;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800211 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
214 alloctype = ICBTAG_FLAG_AD_SHORT;
215 else
216 alloctype = ICBTAG_FLAG_AD_LONG;
217
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700218 if (!inode->i_size) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800219 iinfo->i_alloc_type = alloctype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 mark_inode_dirty(inode);
221 return NULL;
222 }
223
224 /* alloc block, and copy data to it */
225 *block = udf_new_block(inode->i_sb, inode,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800226 iinfo->i_location.partitionReferenceNum,
227 iinfo->i_location.logicalBlockNum, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 if (!(*block))
229 return NULL;
230 newblock = udf_get_pblock(inode->i_sb, *block,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800231 iinfo->i_location.partitionReferenceNum,
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800232 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (!newblock)
234 return NULL;
235 dbh = udf_tgetblk(inode->i_sb, newblock);
236 if (!dbh)
237 return NULL;
238 lock_buffer(dbh);
239 memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
240 set_buffer_uptodate(dbh);
241 unlock_buffer(dbh);
242 mark_buffer_dirty_inode(dbh, inode);
243
Marcin Slusarz4b111112008-02-08 04:20:36 -0800244 sfibh.soffset = sfibh.eoffset =
Jan Karaaf793292008-02-08 04:20:50 -0800245 f_pos & (inode->i_sb->s_blocksize - 1);
Jan Karaff116fc2007-05-08 00:35:14 -0700246 sfibh.sbh = sfibh.ebh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 dfibh.soffset = dfibh.eoffset = 0;
248 dfibh.sbh = dfibh.ebh = dbh;
Jan Karaaf793292008-02-08 04:20:50 -0800249 while (f_pos < size) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800250 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800251 sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL,
252 NULL, NULL, NULL);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700253 if (!sfi) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700254 brelse(dbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return NULL;
256 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800257 iinfo->i_alloc_type = alloctype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 sfi->descTag.tagLocation = cpu_to_le32(*block);
259 dfibh.soffset = dfibh.eoffset;
260 dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
261 dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
262 if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800263 sfi->fileIdent +
264 le16_to_cpu(sfi->lengthOfImpUse))) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800265 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700266 brelse(dbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 return NULL;
268 }
269 }
270 mark_buffer_dirty_inode(dbh, inode);
271
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800272 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0,
273 iinfo->i_lenAlloc);
274 iinfo->i_lenAlloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 eloc.logicalBlockNum = *block;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800276 eloc.partitionReferenceNum =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800277 iinfo->i_location.partitionReferenceNum;
Jan Kara2c948b32009-12-03 13:39:28 +0100278 iinfo->i_lenExtents = inode->i_size;
Jan Karaff116fc2007-05-08 00:35:14 -0700279 epos.bh = NULL;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800280 epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700281 epos.offset = udf_file_entry_alloc_offset(inode);
Jan Kara2c948b32009-12-03 13:39:28 +0100282 udf_add_aext(inode, &epos, &eloc, inode->i_size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 /* UniqueID stuff */
284
Jan Kara3bf25cb2007-05-08 00:35:16 -0700285 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 mark_inode_dirty(inode);
287 return dbh;
288}
289
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700290static int udf_get_block(struct inode *inode, sector_t block,
291 struct buffer_head *bh_result, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 int err, new;
294 struct buffer_head *bh;
Marcin Slusarz1ed16172008-02-08 04:20:48 -0800295 sector_t phys = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800296 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700298 if (!create) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 phys = udf_block_map(inode, block);
300 if (phys)
301 map_bh(bh_result, inode->i_sb, phys);
302 return 0;
303 }
304
305 err = -EIO;
306 new = 0;
307 bh = NULL;
308
309 lock_kernel();
310
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800311 iinfo = UDF_I(inode);
312 if (block == iinfo->i_next_alloc_block + 1) {
313 iinfo->i_next_alloc_block++;
314 iinfo->i_next_alloc_goal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
316
317 err = 0;
318
319 bh = inode_getblk(inode, block, &err, &phys, &new);
Eric Sesterhenn2c2111c2006-04-02 13:40:13 +0200320 BUG_ON(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 if (err)
322 goto abort;
Eric Sesterhenn2c2111c2006-04-02 13:40:13 +0200323 BUG_ON(!phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 if (new)
326 set_buffer_new(bh_result);
327 map_bh(bh_result, inode->i_sb, phys);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700328
329abort:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 unlock_kernel();
331 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700334static struct buffer_head *udf_getblk(struct inode *inode, long block,
335 int create, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700337 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 struct buffer_head dummy;
339
340 dummy.b_state = 0;
341 dummy.b_blocknr = -1000;
342 *err = udf_get_block(inode, block, &dummy, create);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700343 if (!*err && buffer_mapped(&dummy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700345 if (buffer_new(&dummy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 lock_buffer(bh);
347 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
348 set_buffer_uptodate(bh);
349 unlock_buffer(bh);
350 mark_buffer_dirty_inode(bh, inode);
351 }
352 return bh;
353 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return NULL;
356}
357
Jan Kara31170b62007-05-08 00:35:21 -0700358/* Extend the file by 'blocks' blocks, return the number of extents added */
359int udf_extend_file(struct inode *inode, struct extent_position *last_pos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200360 struct kernel_long_ad *last_ext, sector_t blocks)
Jan Kara31170b62007-05-08 00:35:21 -0700361{
362 sector_t add;
363 int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
364 struct super_block *sb = inode->i_sb;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200365 struct kernel_lb_addr prealloc_loc = {};
Jan Kara31170b62007-05-08 00:35:21 -0700366 int prealloc_len = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800367 struct udf_inode_info *iinfo;
Jan Kara31170b62007-05-08 00:35:21 -0700368
369 /* The previous extent is fake and we should not extend by anything
370 * - there's nothing to do... */
371 if (!blocks && fake)
372 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700373
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800374 iinfo = UDF_I(inode);
Jan Kara31170b62007-05-08 00:35:21 -0700375 /* Round the last extent up to a multiple of block size */
376 if (last_ext->extLength & (sb->s_blocksize - 1)) {
377 last_ext->extLength =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700378 (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
379 (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
380 sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800381 iinfo->i_lenExtents =
382 (iinfo->i_lenExtents + sb->s_blocksize - 1) &
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700383 ~(sb->s_blocksize - 1);
Jan Kara31170b62007-05-08 00:35:21 -0700384 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700385
Jan Kara31170b62007-05-08 00:35:21 -0700386 /* Last extent are just preallocated blocks? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800387 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
388 EXT_NOT_RECORDED_ALLOCATED) {
Jan Kara31170b62007-05-08 00:35:21 -0700389 /* Save the extent so that we can reattach it to the end */
390 prealloc_loc = last_ext->extLocation;
391 prealloc_len = last_ext->extLength;
392 /* Mark the extent as a hole */
393 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700394 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
Jan Kara31170b62007-05-08 00:35:21 -0700395 last_ext->extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800396 last_ext->extLocation.partitionReferenceNum = 0;
Jan Kara31170b62007-05-08 00:35:21 -0700397 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700398
Jan Kara31170b62007-05-08 00:35:21 -0700399 /* Can we merge with the previous extent? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800400 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
401 EXT_NOT_RECORDED_NOT_ALLOCATED) {
402 add = ((1 << 30) - sb->s_blocksize -
403 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK)) >>
404 sb->s_blocksize_bits;
Jan Kara31170b62007-05-08 00:35:21 -0700405 if (add > blocks)
406 add = blocks;
407 blocks -= add;
408 last_ext->extLength += add << sb->s_blocksize_bits;
409 }
410
411 if (fake) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200412 udf_add_aext(inode, last_pos, &last_ext->extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700413 last_ext->extLength, 1);
Jan Kara31170b62007-05-08 00:35:21 -0700414 count++;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800415 } else
Pekka Enberg97e961f2008-10-15 12:29:03 +0200416 udf_write_aext(inode, last_pos, &last_ext->extLocation,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800417 last_ext->extLength, 1);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700418
Jan Kara31170b62007-05-08 00:35:21 -0700419 /* Managed to do everything necessary? */
420 if (!blocks)
421 goto out;
422
423 /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
424 last_ext->extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800425 last_ext->extLocation.partitionReferenceNum = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700426 add = (1 << (30-sb->s_blocksize_bits)) - 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800427 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
428 (add << sb->s_blocksize_bits);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700429
Jan Kara31170b62007-05-08 00:35:21 -0700430 /* Create enough extents to cover the whole hole */
431 while (blocks > add) {
432 blocks -= add;
Pekka Enberg97e961f2008-10-15 12:29:03 +0200433 if (udf_add_aext(inode, last_pos, &last_ext->extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700434 last_ext->extLength, 1) == -1)
Jan Kara31170b62007-05-08 00:35:21 -0700435 return -1;
436 count++;
437 }
438 if (blocks) {
439 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700440 (blocks << sb->s_blocksize_bits);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200441 if (udf_add_aext(inode, last_pos, &last_ext->extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700442 last_ext->extLength, 1) == -1)
Jan Kara31170b62007-05-08 00:35:21 -0700443 return -1;
444 count++;
445 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700446
447out:
Jan Kara31170b62007-05-08 00:35:21 -0700448 /* Do we have some preallocated blocks saved? */
449 if (prealloc_len) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200450 if (udf_add_aext(inode, last_pos, &prealloc_loc,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800451 prealloc_len, 1) == -1)
Jan Kara31170b62007-05-08 00:35:21 -0700452 return -1;
453 last_ext->extLocation = prealloc_loc;
454 last_ext->extLength = prealloc_len;
455 count++;
456 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700457
Jan Kara31170b62007-05-08 00:35:21 -0700458 /* last_pos should point to the last written extent... */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800459 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200460 last_pos->offset -= sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800461 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200462 last_pos->offset -= sizeof(struct long_ad);
Jan Kara31170b62007-05-08 00:35:21 -0700463 else
464 return -1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700465
Jan Kara31170b62007-05-08 00:35:21 -0700466 return count;
467}
468
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700469static struct buffer_head *inode_getblk(struct inode *inode, sector_t block,
Marcin Slusarz1ed16172008-02-08 04:20:48 -0800470 int *err, sector_t *phys, int *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Jan Kara31170b62007-05-08 00:35:21 -0700472 static sector_t last_block;
Jan Karaff116fc2007-05-08 00:35:14 -0700473 struct buffer_head *result = NULL;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200474 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE];
Jan Karaff116fc2007-05-08 00:35:14 -0700475 struct extent_position prev_epos, cur_epos, next_epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 int count = 0, startnum = 0, endnum = 0;
Jan Kara85d71242007-06-01 00:46:29 -0700477 uint32_t elen = 0, tmpelen;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200478 struct kernel_lb_addr eloc, tmpeloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 int c = 1;
Jan Kara60448b12007-05-08 00:35:13 -0700480 loff_t lbcount = 0, b_off = 0;
481 uint32_t newblocknum, newblock;
482 sector_t offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800484 struct udf_inode_info *iinfo = UDF_I(inode);
485 int goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
Jan Kara31170b62007-05-08 00:35:21 -0700486 int lastblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Jan Karaff116fc2007-05-08 00:35:14 -0700488 prev_epos.offset = udf_file_entry_alloc_offset(inode);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800489 prev_epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700490 prev_epos.bh = NULL;
491 cur_epos = next_epos = prev_epos;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700492 b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 /* find the extent which contains the block we are looking for.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700495 alternate between laarr[0] and laarr[1] for locations of the
496 current extent, and the previous extent */
497 do {
498 if (prev_epos.bh != cur_epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700499 brelse(prev_epos.bh);
500 get_bh(cur_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700501 prev_epos.bh = cur_epos.bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700503 if (cur_epos.bh != next_epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700504 brelse(cur_epos.bh);
505 get_bh(next_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700506 cur_epos.bh = next_epos.bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
508
509 lbcount += elen;
510
Jan Karaff116fc2007-05-08 00:35:14 -0700511 prev_epos.block = cur_epos.block;
512 cur_epos.block = next_epos.block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Jan Karaff116fc2007-05-08 00:35:14 -0700514 prev_epos.offset = cur_epos.offset;
515 cur_epos.offset = next_epos.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Marcin Slusarz4b111112008-02-08 04:20:36 -0800517 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
518 if (etype == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 break;
520
521 c = !c;
522
523 laarr[c].extLength = (etype << 30) | elen;
524 laarr[c].extLocation = eloc;
525
526 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
527 pgoal = eloc.logicalBlockNum +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700528 ((elen + inode->i_sb->s_blocksize - 1) >>
529 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700531 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 } while (lbcount + elen <= b_off);
533
534 b_off -= lbcount;
535 offset = b_off >> inode->i_sb->s_blocksize_bits;
Jan Kara85d71242007-06-01 00:46:29 -0700536 /*
537 * Move prev_epos and cur_epos into indirect extent if we are at
538 * the pointer to it
539 */
540 udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
541 udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 /* if the extent is allocated and recorded, return the block
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700544 if the extent is not a multiple of the blocksize, round up */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700546 if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
547 if (elen & (inode->i_sb->s_blocksize - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 elen = EXT_RECORDED_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700549 ((elen + inode->i_sb->s_blocksize - 1) &
550 ~(inode->i_sb->s_blocksize - 1));
Pekka Enberg97e961f2008-10-15 12:29:03 +0200551 etype = udf_write_aext(inode, &cur_epos, &eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
Jan Kara3bf25cb2007-05-08 00:35:16 -0700553 brelse(prev_epos.bh);
554 brelse(cur_epos.bh);
555 brelse(next_epos.bh);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200556 newblock = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 *phys = newblock;
558 return NULL;
559 }
560
Jan Kara31170b62007-05-08 00:35:21 -0700561 last_block = block;
562 /* Are we beyond EOF? */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700563 if (etype == -1) {
Jan Kara31170b62007-05-08 00:35:21 -0700564 int ret;
565
566 if (count) {
567 if (c)
568 laarr[0] = laarr[1];
569 startnum = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700570 } else {
Jan Kara31170b62007-05-08 00:35:21 -0700571 /* Create a fake extent when there's not one */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800572 memset(&laarr[0].extLocation, 0x00,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200573 sizeof(struct kernel_lb_addr));
Jan Kara31170b62007-05-08 00:35:21 -0700574 laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800575 /* Will udf_extend_file() create real extent from
576 a fake one? */
Jan Kara31170b62007-05-08 00:35:21 -0700577 startnum = (offset > 0);
578 }
579 /* Create extents for the hole between EOF and offset */
580 ret = udf_extend_file(inode, &prev_epos, laarr, offset);
581 if (ret == -1) {
582 brelse(prev_epos.bh);
583 brelse(cur_epos.bh);
584 brelse(next_epos.bh);
585 /* We don't really know the error here so we just make
586 * something up */
587 *err = -ENOSPC;
588 return NULL;
589 }
590 c = 0;
591 offset = 0;
592 count += ret;
593 /* We are not covered by a preallocated extent? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800594 if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
595 EXT_NOT_RECORDED_ALLOCATED) {
Jan Kara31170b62007-05-08 00:35:21 -0700596 /* Is there any real extent? - otherwise we overwrite
597 * the fake one... */
598 if (count)
599 c = !c;
600 laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700601 inode->i_sb->s_blocksize;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800602 memset(&laarr[c].extLocation, 0x00,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200603 sizeof(struct kernel_lb_addr));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700604 count++;
605 endnum++;
Jan Kara31170b62007-05-08 00:35:21 -0700606 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700607 endnum = c + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 lastblock = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700609 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 endnum = startnum = ((count > 2) ? 2 : count);
611
Marcin Slusarz4b111112008-02-08 04:20:36 -0800612 /* if the current extent is in position 0,
613 swap it with the previous */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700614 if (!c && count != 1) {
Jan Kara31170b62007-05-08 00:35:21 -0700615 laarr[2] = laarr[0];
616 laarr[0] = laarr[1];
617 laarr[1] = laarr[2];
618 c = 1;
619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Marcin Slusarz4b111112008-02-08 04:20:36 -0800621 /* if the current block is located in an extent,
622 read the next extent */
623 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
624 if (etype != -1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700625 laarr[c + 1].extLength = (etype << 30) | elen;
626 laarr[c + 1].extLocation = eloc;
627 count++;
628 startnum++;
629 endnum++;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800630 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 lastblock = 1;
632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 /* if the current extent is not recorded but allocated, get the
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700635 * block in the extent corresponding to the requested block */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800636 if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800638 else { /* otherwise, allocate a new block */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800639 if (iinfo->i_next_alloc_block == block)
640 goal = iinfo->i_next_alloc_goal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700642 if (!goal) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800643 if (!(goal = pgoal)) /* XXX: what was intended here? */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800644 goal = iinfo->i_location.logicalBlockNum + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
646
Marcin Slusarz4b111112008-02-08 04:20:36 -0800647 newblocknum = udf_new_block(inode->i_sb, inode,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800648 iinfo->i_location.partitionReferenceNum,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800649 goal, err);
650 if (!newblocknum) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700651 brelse(prev_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 *err = -ENOSPC;
653 return NULL;
654 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800655 iinfo->i_lenExtents += inode->i_sb->s_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
657
Marcin Slusarz4b111112008-02-08 04:20:36 -0800658 /* if the extent the requsted block is located in contains multiple
659 * blocks, split the extent into at most three extents. blocks prior
660 * to requested block, requested block, and blocks after requested
661 * block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
663
664#ifdef UDF_PREALLOCATE
Jan Kara81056dd2009-07-16 18:02:25 +0200665 /* We preallocate blocks only for regular files. It also makes sense
666 * for directories but there's a problem when to drop the
667 * preallocation. We might use some delayed work for that but I feel
668 * it's overengineering for a filesystem like UDF. */
669 if (S_ISREG(inode->i_mode))
670 udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671#endif
672
673 /* merge any continuous blocks in laarr */
674 udf_merge_extents(inode, laarr, &endnum);
675
676 /* write back the new extents, inserting new extents if the new number
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700677 * of extents is greater than the old number, and deleting extents if
678 * the new number of extents is less than the old number */
Jan Karaff116fc2007-05-08 00:35:14 -0700679 udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Jan Kara3bf25cb2007-05-08 00:35:16 -0700681 brelse(prev_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Marcin Slusarz4b111112008-02-08 04:20:36 -0800683 newblock = udf_get_pblock(inode->i_sb, newblocknum,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800684 iinfo->i_location.partitionReferenceNum, 0);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800685 if (!newblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 *phys = newblock;
688 *err = 0;
689 *new = 1;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800690 iinfo->i_next_alloc_block = block;
691 iinfo->i_next_alloc_goal = newblocknum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 inode->i_ctime = current_fs_time(inode->i_sb);
693
694 if (IS_SYNC(inode))
695 udf_sync_inode(inode);
696 else
697 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return result;
700}
701
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700702static void udf_split_extents(struct inode *inode, int *c, int offset,
703 int newblocknum,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200704 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700705 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
Marcin Slusarz4b111112008-02-08 04:20:36 -0800707 unsigned long blocksize = inode->i_sb->s_blocksize;
708 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
Marcin Slusarz4b111112008-02-08 04:20:36 -0800711 (laarr[*c].extLength >> 30) ==
712 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 int curr = *c;
714 int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800715 blocksize - 1) >> blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 int8_t etype = (laarr[curr].extLength >> 30);
717
Marcin Slusarz4b111112008-02-08 04:20:36 -0800718 if (blen == 1)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700719 ;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800720 else if (!offset || blen == offset + 1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700721 laarr[curr + 2] = laarr[curr + 1];
722 laarr[curr + 1] = laarr[curr];
723 } else {
724 laarr[curr + 3] = laarr[curr + 1];
725 laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
727
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700728 if (offset) {
729 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800730 udf_free_blocks(inode->i_sb, inode,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200731 &laarr[curr].extLocation,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800732 0, offset);
733 laarr[curr].extLength =
734 EXT_NOT_RECORDED_NOT_ALLOCATED |
735 (offset << blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 laarr[curr].extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800737 laarr[curr].extLocation.
738 partitionReferenceNum = 0;
739 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 laarr[curr].extLength = (etype << 30) |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800741 (offset << blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700742 curr++;
743 (*c)++;
744 (*endnum)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
Cyrill Gorcunov647bd612007-07-15 23:39:47 -0700746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 laarr[curr].extLocation.logicalBlockNum = newblocknum;
748 if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
749 laarr[curr].extLocation.partitionReferenceNum =
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800750 UDF_I(inode)->i_location.partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800752 blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700753 curr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700755 if (blen != offset + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
Marcin Slusarz4b111112008-02-08 04:20:36 -0800757 laarr[curr].extLocation.logicalBlockNum +=
758 offset + 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700759 laarr[curr].extLength = (etype << 30) |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800760 ((blen - (offset + 1)) << blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700761 curr++;
762 (*endnum)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
764 }
765}
766
767static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200768 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700769 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
771 int start, length = 0, currlength = 0, i;
772
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700773 if (*endnum >= (c + 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (!lastblock)
775 return;
776 else
777 start = c;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700778 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800779 if ((laarr[c + 1].extLength >> 30) ==
780 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700781 start = c + 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800782 length = currlength =
783 (((laarr[c + 1].extLength &
784 UDF_EXTENT_LENGTH_MASK) +
785 inode->i_sb->s_blocksize - 1) >>
786 inode->i_sb->s_blocksize_bits);
787 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 start = c;
789 }
790
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700791 for (i = start + 1; i <= *endnum; i++) {
792 if (i == *endnum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (lastblock)
794 length += UDF_DEFAULT_PREALLOC_BLOCKS;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800795 } else if ((laarr[i].extLength >> 30) ==
796 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
797 length += (((laarr[i].extLength &
798 UDF_EXTENT_LENGTH_MASK) +
799 inode->i_sb->s_blocksize - 1) >>
800 inode->i_sb->s_blocksize_bits);
801 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 break;
803 }
804
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700805 if (length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 int next = laarr[start].extLocation.logicalBlockNum +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700807 (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800808 inode->i_sb->s_blocksize - 1) >>
809 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800811 laarr[start].extLocation.partitionReferenceNum,
812 next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
813 length : UDF_DEFAULT_PREALLOC_BLOCKS) -
814 currlength);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700815 if (numalloc) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800816 if (start == (c + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 laarr[start].extLength +=
Marcin Slusarz4b111112008-02-08 04:20:36 -0800818 (numalloc <<
819 inode->i_sb->s_blocksize_bits);
820 else {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700821 memmove(&laarr[c + 2], &laarr[c + 1],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200822 sizeof(struct long_ad) * (*endnum - (c + 1)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700823 (*endnum)++;
824 laarr[c + 1].extLocation.logicalBlockNum = next;
825 laarr[c + 1].extLocation.partitionReferenceNum =
Marcin Slusarz4b111112008-02-08 04:20:36 -0800826 laarr[c].extLocation.
827 partitionReferenceNum;
828 laarr[c + 1].extLength =
829 EXT_NOT_RECORDED_ALLOCATED |
830 (numalloc <<
831 inode->i_sb->s_blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700832 start = c + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 }
834
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700835 for (i = start + 1; numalloc && i < *endnum; i++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800836 int elen = ((laarr[i].extLength &
837 UDF_EXTENT_LENGTH_MASK) +
838 inode->i_sb->s_blocksize - 1) >>
839 inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700841 if (elen > numalloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 laarr[i].extLength -=
Marcin Slusarz4b111112008-02-08 04:20:36 -0800843 (numalloc <<
844 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 numalloc = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700846 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 numalloc -= elen;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700848 if (*endnum > (i + 1))
Marcin Slusarz4b111112008-02-08 04:20:36 -0800849 memmove(&laarr[i],
850 &laarr[i + 1],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200851 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -0800852 (*endnum - (i + 1)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700853 i--;
854 (*endnum)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
856 }
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800857 UDF_I(inode)->i_lenExtents +=
Marcin Slusarz4b111112008-02-08 04:20:36 -0800858 numalloc << inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
860 }
861}
862
863static void udf_merge_extents(struct inode *inode,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200864 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700865 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
867 int i;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800868 unsigned long blocksize = inode->i_sb->s_blocksize;
869 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700871 for (i = 0; i < (*endnum - 1); i++) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200872 struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
873 struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Marcin Slusarz4b111112008-02-08 04:20:36 -0800875 if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
876 (((li->extLength >> 30) ==
877 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
878 ((lip1->extLocation.logicalBlockNum -
879 li->extLocation.logicalBlockNum) ==
880 (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
881 blocksize - 1) >> blocksize_bits)))) {
882
883 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
884 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
885 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
886 lip1->extLength = (lip1->extLength -
887 (li->extLength &
888 UDF_EXTENT_LENGTH_MASK) +
889 UDF_EXTENT_LENGTH_MASK) &
890 ~(blocksize - 1);
891 li->extLength = (li->extLength &
892 UDF_EXTENT_FLAG_MASK) +
893 (UDF_EXTENT_LENGTH_MASK + 1) -
894 blocksize;
895 lip1->extLocation.logicalBlockNum =
896 li->extLocation.logicalBlockNum +
897 ((li->extLength &
898 UDF_EXTENT_LENGTH_MASK) >>
899 blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700900 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800901 li->extLength = lip1->extLength +
902 (((li->extLength &
903 UDF_EXTENT_LENGTH_MASK) +
904 blocksize - 1) & ~(blocksize - 1));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700905 if (*endnum > (i + 2))
906 memmove(&laarr[i + 1], &laarr[i + 2],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200907 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -0800908 (*endnum - (i + 2)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700909 i--;
910 (*endnum)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 }
Marcin Slusarz4b111112008-02-08 04:20:36 -0800912 } else if (((li->extLength >> 30) ==
913 (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
914 ((lip1->extLength >> 30) ==
915 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200916 udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800917 ((li->extLength &
918 UDF_EXTENT_LENGTH_MASK) +
919 blocksize - 1) >> blocksize_bits);
920 li->extLocation.logicalBlockNum = 0;
921 li->extLocation.partitionReferenceNum = 0;
922
923 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
924 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
925 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
926 lip1->extLength = (lip1->extLength -
927 (li->extLength &
928 UDF_EXTENT_LENGTH_MASK) +
929 UDF_EXTENT_LENGTH_MASK) &
930 ~(blocksize - 1);
931 li->extLength = (li->extLength &
932 UDF_EXTENT_FLAG_MASK) +
933 (UDF_EXTENT_LENGTH_MASK + 1) -
934 blocksize;
935 } else {
936 li->extLength = lip1->extLength +
937 (((li->extLength &
938 UDF_EXTENT_LENGTH_MASK) +
939 blocksize - 1) & ~(blocksize - 1));
940 if (*endnum > (i + 2))
941 memmove(&laarr[i + 1], &laarr[i + 2],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200942 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -0800943 (*endnum - (i + 2)));
944 i--;
945 (*endnum)--;
946 }
947 } else if ((li->extLength >> 30) ==
948 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
949 udf_free_blocks(inode->i_sb, inode,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200950 &li->extLocation, 0,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800951 ((li->extLength &
952 UDF_EXTENT_LENGTH_MASK) +
953 blocksize - 1) >> blocksize_bits);
954 li->extLocation.logicalBlockNum = 0;
955 li->extLocation.partitionReferenceNum = 0;
956 li->extLength = (li->extLength &
957 UDF_EXTENT_LENGTH_MASK) |
958 EXT_NOT_RECORDED_NOT_ALLOCATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 }
960 }
961}
962
963static void udf_update_extents(struct inode *inode,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200964 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700965 int startnum, int endnum,
966 struct extent_position *epos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
968 int start = 0, i;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200969 struct kernel_lb_addr tmploc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 uint32_t tmplen;
971
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700972 if (startnum > endnum) {
973 for (i = 0; i < (startnum - endnum); i++)
Jan Karaff116fc2007-05-08 00:35:14 -0700974 udf_delete_aext(inode, *epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700975 laarr[i].extLength);
976 } else if (startnum < endnum) {
977 for (i = 0; i < (endnum - startnum); i++) {
Jan Karaff116fc2007-05-08 00:35:14 -0700978 udf_insert_aext(inode, *epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700979 laarr[i].extLength);
Jan Karaff116fc2007-05-08 00:35:14 -0700980 udf_next_aext(inode, epos, &laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700981 &laarr[i].extLength, 1);
982 start++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 }
984 }
985
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700986 for (i = start; i < endnum; i++) {
Jan Karaff116fc2007-05-08 00:35:14 -0700987 udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200988 udf_write_aext(inode, epos, &laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700989 laarr[i].extLength, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
991}
992
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700993struct buffer_head *udf_bread(struct inode *inode, int block,
994 int create, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700996 struct buffer_head *bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998 bh = udf_getblk(inode, block, create, err);
999 if (!bh)
1000 return NULL;
1001
1002 if (buffer_uptodate(bh))
1003 return bh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 ll_rw_block(READ, 1, &bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 wait_on_buffer(bh);
1008 if (buffer_uptodate(bh))
1009 return bh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 brelse(bh);
1012 *err = -EIO;
1013 return NULL;
1014}
1015
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001016void udf_truncate(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
1018 int offset;
1019 int err;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001020 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001023 S_ISLNK(inode->i_mode)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 return;
1025 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1026 return;
1027
1028 lock_kernel();
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001029 iinfo = UDF_I(inode);
1030 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001031 if (inode->i_sb->s_blocksize <
1032 (udf_file_entry_alloc_offset(inode) +
1033 inode->i_size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 udf_expand_file_adinicb(inode, inode->i_size, &err);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001035 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1036 inode->i_size = iinfo->i_lenAlloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 unlock_kernel();
1038 return;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001039 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 udf_truncate_extents(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001041 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 offset = inode->i_size & (inode->i_sb->s_blocksize - 1);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001043 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr + offset,
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001044 0x00, inode->i_sb->s_blocksize -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001045 offset - udf_file_entry_alloc_offset(inode));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001046 iinfo->i_lenAlloc = inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001048 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001049 block_truncate_page(inode->i_mapping, inode->i_size,
1050 udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 udf_truncate_extents(inode);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 inode->i_mtime = inode->i_ctime = current_fs_time(inode->i_sb);
1055 if (IS_SYNC(inode))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001056 udf_sync_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 else
1058 mark_inode_dirty(inode);
1059 unlock_kernel();
1060}
1061
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001062static void __udf_read_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
1064 struct buffer_head *bh = NULL;
1065 struct fileEntry *fe;
1066 uint16_t ident;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001067 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 /*
1070 * Set defaults, but the inode is still incomplete!
1071 * Note: get_new_inode() sets the following on a new inode:
1072 * i_sb = sb
1073 * i_no = ino
1074 * i_flags = sb->s_flags
1075 * i_state = 0
1076 * clean_inode(): zero fills and sets
1077 * i_count = 1
1078 * i_nlink = 1
1079 * i_op = NULL;
1080 */
Pekka Enberg97e961f2008-10-15 12:29:03 +02001081 bh = udf_read_ptagged(inode->i_sb, &iinfo->i_location, 0, &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001082 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 printk(KERN_ERR "udf: udf_read_inode(ino %ld) failed !bh\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001084 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 make_bad_inode(inode);
1086 return;
1087 }
1088
1089 if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001090 ident != TAG_IDENT_USE) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001091 printk(KERN_ERR "udf: udf_read_inode(ino %ld) "
1092 "failed ident=%d\n", inode->i_ino, ident);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001093 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 make_bad_inode(inode);
1095 return;
1096 }
1097
1098 fe = (struct fileEntry *)bh->b_data;
1099
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001100 if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001101 struct buffer_head *ibh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Pekka Enberg97e961f2008-10-15 12:29:03 +02001103 ibh = udf_read_ptagged(inode->i_sb, &iinfo->i_location, 1,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001104 &ident);
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001105 if (ident == TAG_IDENT_IE && ibh) {
1106 struct buffer_head *nbh = NULL;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001107 struct kernel_lb_addr loc;
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001108 struct indirectEntry *ie;
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001109
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001110 ie = (struct indirectEntry *)ibh->b_data;
1111 loc = lelb_to_cpu(ie->indirectICB.extLocation);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001112
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001113 if (ie->indirectICB.extLength &&
Pekka Enberg97e961f2008-10-15 12:29:03 +02001114 (nbh = udf_read_ptagged(inode->i_sb, &loc, 0,
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001115 &ident))) {
1116 if (ident == TAG_IDENT_FE ||
1117 ident == TAG_IDENT_EFE) {
1118 memcpy(&iinfo->i_location,
1119 &loc,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001120 sizeof(struct kernel_lb_addr));
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001121 brelse(bh);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001122 brelse(ibh);
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001123 brelse(nbh);
1124 __udf_read_inode(inode);
1125 return;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001126 }
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001127 brelse(nbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001129 }
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001130 brelse(ibh);
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001131 } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 printk(KERN_ERR "udf: unsupported strategy type: %d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001133 le16_to_cpu(fe->icbTag.strategyType));
Jan Kara3bf25cb2007-05-08 00:35:16 -07001134 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 make_bad_inode(inode);
1136 return;
1137 }
1138 udf_fill_inode(inode, bh);
Jan Kara31170b62007-05-08 00:35:21 -07001139
Jan Kara3bf25cb2007-05-08 00:35:16 -07001140 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141}
1142
1143static void udf_fill_inode(struct inode *inode, struct buffer_head *bh)
1144{
1145 struct fileEntry *fe;
1146 struct extendedFileEntry *efe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 int offset;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001148 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001149 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
1151 fe = (struct fileEntry *)bh->b_data;
1152 efe = (struct extendedFileEntry *)bh->b_data;
1153
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001154 if (fe->icbTag.strategyType == cpu_to_le16(4))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001155 iinfo->i_strat4096 = 0;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001156 else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001157 iinfo->i_strat4096 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001159 iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
Marcin Slusarz4b111112008-02-08 04:20:36 -08001160 ICBTAG_FLAG_AD_MASK;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001161 iinfo->i_unique = 0;
1162 iinfo->i_lenEAttr = 0;
1163 iinfo->i_lenExtents = 0;
1164 iinfo->i_lenAlloc = 0;
1165 iinfo->i_next_alloc_block = 0;
1166 iinfo->i_next_alloc_goal = 0;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001167 if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001168 iinfo->i_efe = 1;
1169 iinfo->i_use = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001170 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1171 sizeof(struct extendedFileEntry))) {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001172 make_bad_inode(inode);
1173 return;
1174 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001175 memcpy(iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001176 bh->b_data + sizeof(struct extendedFileEntry),
1177 inode->i_sb->s_blocksize -
1178 sizeof(struct extendedFileEntry));
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001179 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001180 iinfo->i_efe = 0;
1181 iinfo->i_use = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001182 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1183 sizeof(struct fileEntry))) {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001184 make_bad_inode(inode);
1185 return;
1186 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001187 memcpy(iinfo->i_ext.i_data,
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001188 bh->b_data + sizeof(struct fileEntry),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001189 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001190 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001191 iinfo->i_efe = 0;
1192 iinfo->i_use = 1;
1193 iinfo->i_lenAlloc = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001194 ((struct unallocSpaceEntry *)bh->b_data)->
1195 lengthAllocDescs);
1196 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1197 sizeof(struct unallocSpaceEntry))) {
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 unallocSpaceEntry),
1203 inode->i_sb->s_blocksize -
1204 sizeof(struct unallocSpaceEntry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 return;
1206 }
1207
1208 inode->i_uid = le32_to_cpu(fe->uid);
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -07001209 if (inode->i_uid == -1 ||
1210 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_IGNORE) ||
1211 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
Phillip Susi4d6660e2006-03-07 21:55:24 -08001212 inode->i_uid = UDF_SB(inode->i_sb)->s_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 inode->i_gid = le32_to_cpu(fe->gid);
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -07001215 if (inode->i_gid == -1 ||
1216 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_IGNORE) ||
1217 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
Phillip Susi4d6660e2006-03-07 21:55:24 -08001218 inode->i_gid = UDF_SB(inode->i_sb)->s_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220 inode->i_nlink = le16_to_cpu(fe->fileLinkCount);
1221 if (!inode->i_nlink)
1222 inode->i_nlink = 1;
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 inode->i_size = le64_to_cpu(fe->informationLength);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001225 iinfo->i_lenExtents = inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01001227 if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
Marcin Slusarz87bc7302008-12-02 13:40:11 +01001228 sbi->s_fmode != UDF_INVALID_MODE)
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01001229 inode->i_mode = sbi->s_fmode;
1230 else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
Marcin Slusarz87bc7302008-12-02 13:40:11 +01001231 sbi->s_dmode != UDF_INVALID_MODE)
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01001232 inode->i_mode = sbi->s_dmode;
1233 else
1234 inode->i_mode = udf_convert_permissions(fe);
1235 inode->i_mode &= ~sbi->s_umask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001237 if (iinfo->i_efe == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001239 (inode->i_sb->s_blocksize_bits - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Marcin Slusarz56774802008-02-10 11:25:31 +01001241 if (!udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001242 inode->i_atime = sbi->s_record_time;
1243
Marcin Slusarz56774802008-02-10 11:25:31 +01001244 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1245 fe->modificationTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001246 inode->i_mtime = sbi->s_record_time;
1247
Marcin Slusarz56774802008-02-10 11:25:31 +01001248 if (!udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001249 inode->i_ctime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001251 iinfo->i_unique = le64_to_cpu(fe->uniqueID);
1252 iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
1253 iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
1254 offset = sizeof(struct fileEntry) + iinfo->i_lenEAttr;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001255 } else {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001256 inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001257 (inode->i_sb->s_blocksize_bits - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Marcin Slusarz56774802008-02-10 11:25:31 +01001259 if (!udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001260 inode->i_atime = sbi->s_record_time;
1261
Marcin Slusarz56774802008-02-10 11:25:31 +01001262 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1263 efe->modificationTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001264 inode->i_mtime = sbi->s_record_time;
1265
Marcin Slusarz56774802008-02-10 11:25:31 +01001266 if (!udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001267 iinfo->i_crtime = sbi->s_record_time;
1268
Marcin Slusarz56774802008-02-10 11:25:31 +01001269 if (!udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001270 inode->i_ctime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001272 iinfo->i_unique = le64_to_cpu(efe->uniqueID);
1273 iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
1274 iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001275 offset = sizeof(struct extendedFileEntry) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001276 iinfo->i_lenEAttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 }
1278
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001279 switch (fe->icbTag.fileType) {
1280 case ICBTAG_FILE_TYPE_DIRECTORY:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001281 inode->i_op = &udf_dir_inode_operations;
1282 inode->i_fop = &udf_dir_operations;
1283 inode->i_mode |= S_IFDIR;
1284 inc_nlink(inode);
1285 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001286 case ICBTAG_FILE_TYPE_REALTIME:
1287 case ICBTAG_FILE_TYPE_REGULAR:
1288 case ICBTAG_FILE_TYPE_UNDEF:
Jan Kara742e1792008-04-08 01:17:52 +02001289 case ICBTAG_FILE_TYPE_VAT20:
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001290 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001291 inode->i_data.a_ops = &udf_adinicb_aops;
1292 else
1293 inode->i_data.a_ops = &udf_aops;
1294 inode->i_op = &udf_file_inode_operations;
1295 inode->i_fop = &udf_file_operations;
1296 inode->i_mode |= S_IFREG;
1297 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001298 case ICBTAG_FILE_TYPE_BLOCK:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001299 inode->i_mode |= S_IFBLK;
1300 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001301 case ICBTAG_FILE_TYPE_CHAR:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001302 inode->i_mode |= S_IFCHR;
1303 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001304 case ICBTAG_FILE_TYPE_FIFO:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001305 init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1306 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001307 case ICBTAG_FILE_TYPE_SOCKET:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001308 init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1309 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001310 case ICBTAG_FILE_TYPE_SYMLINK:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001311 inode->i_data.a_ops = &udf_symlink_aops;
Dmitry Monakhovc15d0fc2010-03-29 11:05:21 +04001312 inode->i_op = &udf_symlink_inode_operations;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001313 inode->i_mode = S_IFLNK | S_IRWXUGO;
1314 break;
Jan Karabfb257a2008-04-08 20:37:21 +02001315 case ICBTAG_FILE_TYPE_MAIN:
1316 udf_debug("METADATA FILE-----\n");
1317 break;
1318 case ICBTAG_FILE_TYPE_MIRROR:
1319 udf_debug("METADATA MIRROR FILE-----\n");
1320 break;
1321 case ICBTAG_FILE_TYPE_BITMAP:
1322 udf_debug("METADATA BITMAP FILE-----\n");
1323 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001324 default:
Marcin Slusarz4b111112008-02-08 04:20:36 -08001325 printk(KERN_ERR "udf: udf_fill_inode(ino %ld) failed unknown "
1326 "file type=%d\n", inode->i_ino,
1327 fe->icbTag.fileType);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001328 make_bad_inode(inode);
1329 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001331 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001332 struct deviceSpec *dsea =
1333 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001334 if (dsea) {
1335 init_special_inode(inode, inode->i_mode,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001336 MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
1337 le32_to_cpu(dsea->minorDeviceIdent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 /* Developer ID ??? */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001339 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 make_bad_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 }
1342}
1343
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001344static int udf_alloc_i_data(struct inode *inode, size_t size)
1345{
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001346 struct udf_inode_info *iinfo = UDF_I(inode);
1347 iinfo->i_ext.i_data = kmalloc(size, GFP_KERNEL);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001348
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001349 if (!iinfo->i_ext.i_data) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001350 printk(KERN_ERR "udf:udf_alloc_i_data (ino %ld) "
1351 "no free memory\n", inode->i_ino);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001352 return -ENOMEM;
1353 }
1354
1355 return 0;
1356}
1357
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001358static mode_t udf_convert_permissions(struct fileEntry *fe)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
1360 mode_t mode;
1361 uint32_t permissions;
1362 uint32_t flags;
1363
1364 permissions = le32_to_cpu(fe->permissions);
1365 flags = le16_to_cpu(fe->icbTag.flags);
1366
Marcin Slusarz4b111112008-02-08 04:20:36 -08001367 mode = ((permissions) & S_IRWXO) |
1368 ((permissions >> 2) & S_IRWXG) |
1369 ((permissions >> 4) & S_IRWXU) |
1370 ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
1371 ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
1372 ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374 return mode;
1375}
1376
Christoph Hellwiga9185b42010-03-05 09:21:37 +01001377int udf_write_inode(struct inode *inode, struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
1379 int ret;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 lock_kernel();
Christoph Hellwiga9185b42010-03-05 09:21:37 +01001382 ret = udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 unlock_kernel();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 return ret;
1386}
1387
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001388int udf_sync_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
1390 return udf_update_inode(inode, 1);
1391}
1392
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001393static int udf_update_inode(struct inode *inode, int do_sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
1395 struct buffer_head *bh = NULL;
1396 struct fileEntry *fe;
1397 struct extendedFileEntry *efe;
1398 uint32_t udfperms;
1399 uint16_t icbflags;
1400 uint16_t crclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 int err = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001402 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001403 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001404 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Jan Kara5833ded2010-01-08 16:52:59 +01001406 bh = udf_tgetblk(inode->i_sb,
1407 udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001408 if (!bh) {
Jan Karaaae917c2010-01-08 16:46:29 +01001409 udf_debug("getblk failure\n");
1410 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 }
1412
Jan Karaaae917c2010-01-08 16:46:29 +01001413 lock_buffer(bh);
1414 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 fe = (struct fileEntry *)bh->b_data;
1416 efe = (struct extendedFileEntry *)bh->b_data;
1417
Jan Karaaae917c2010-01-08 16:46:29 +01001418 if (iinfo->i_use) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 struct unallocSpaceEntry *use =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001420 (struct unallocSpaceEntry *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001422 use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001423 memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001424 iinfo->i_ext.i_data, inode->i_sb->s_blocksize -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001425 sizeof(struct unallocSpaceEntry));
Jan Karaaae917c2010-01-08 16:46:29 +01001426 use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
1427 use->descTag.tagLocation =
1428 cpu_to_le32(iinfo->i_location.logicalBlockNum);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001429 crclen = sizeof(struct unallocSpaceEntry) +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001430 iinfo->i_lenAlloc - sizeof(struct tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 use->descTag.descCRCLength = cpu_to_le16(crclen);
Bob Copelandf845fce2008-04-17 09:47:48 +02001432 use->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)use +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001433 sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02001434 crclen));
Marcin Slusarz3f2587b2008-02-08 04:20:39 -08001435 use->descTag.tagChecksum = udf_tag_checksum(&use->descTag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Jan Karaaae917c2010-01-08 16:46:29 +01001437 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 }
1439
Phillip Susi4d6660e2006-03-07 21:55:24 -08001440 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
1441 fe->uid = cpu_to_le32(-1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001442 else
1443 fe->uid = cpu_to_le32(inode->i_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Phillip Susi4d6660e2006-03-07 21:55:24 -08001445 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
1446 fe->gid = cpu_to_le32(-1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001447 else
1448 fe->gid = cpu_to_le32(inode->i_gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Marcin Slusarz4b111112008-02-08 04:20:36 -08001450 udfperms = ((inode->i_mode & S_IRWXO)) |
1451 ((inode->i_mode & S_IRWXG) << 2) |
1452 ((inode->i_mode & S_IRWXU) << 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Marcin Slusarz4b111112008-02-08 04:20:36 -08001454 udfperms |= (le32_to_cpu(fe->permissions) &
1455 (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
1456 FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
1457 FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 fe->permissions = cpu_to_le32(udfperms);
1459
1460 if (S_ISDIR(inode->i_mode))
1461 fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1462 else
1463 fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1464
1465 fe->informationLength = cpu_to_le64(inode->i_size);
1466
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001467 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001468 struct regid *eid;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001469 struct deviceSpec *dsea =
1470 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001471 if (!dsea) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 dsea = (struct deviceSpec *)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001473 udf_add_extendedattr(inode,
1474 sizeof(struct deviceSpec) +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001475 sizeof(struct regid), 12, 0x3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 dsea->attrType = cpu_to_le32(12);
1477 dsea->attrSubtype = 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001478 dsea->attrLength = cpu_to_le32(
1479 sizeof(struct deviceSpec) +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001480 sizeof(struct regid));
1481 dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 }
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001483 eid = (struct regid *)dsea->impUse;
1484 memset(eid, 0, sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 strcpy(eid->ident, UDF_ID_DEVELOPER);
1486 eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1487 eid->identSuffix[1] = UDF_OS_ID_LINUX;
1488 dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1489 dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1490 }
1491
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001492 if (iinfo->i_efe == 0) {
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001493 memcpy(bh->b_data + sizeof(struct fileEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001494 iinfo->i_ext.i_data,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001495 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001496 fe->logicalBlocksRecorded = cpu_to_le64(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001497 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1498 (blocksize_bits - 9));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Marcin Slusarz56774802008-02-10 11:25:31 +01001500 udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
1501 udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
1502 udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001503 memset(&(fe->impIdent), 0, sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1505 fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1506 fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001507 fe->uniqueID = cpu_to_le64(iinfo->i_unique);
1508 fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1509 fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1511 crclen = sizeof(struct fileEntry);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001512 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001513 memcpy(bh->b_data + sizeof(struct extendedFileEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001514 iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001515 inode->i_sb->s_blocksize -
1516 sizeof(struct extendedFileEntry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 efe->objectSize = cpu_to_le64(inode->i_size);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001518 efe->logicalBlocksRecorded = cpu_to_le64(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001519 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1520 (blocksize_bits - 9));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001522 if (iinfo->i_crtime.tv_sec > inode->i_atime.tv_sec ||
1523 (iinfo->i_crtime.tv_sec == inode->i_atime.tv_sec &&
1524 iinfo->i_crtime.tv_nsec > inode->i_atime.tv_nsec))
1525 iinfo->i_crtime = inode->i_atime;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001526
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001527 if (iinfo->i_crtime.tv_sec > inode->i_mtime.tv_sec ||
1528 (iinfo->i_crtime.tv_sec == inode->i_mtime.tv_sec &&
1529 iinfo->i_crtime.tv_nsec > inode->i_mtime.tv_nsec))
1530 iinfo->i_crtime = inode->i_mtime;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001531
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001532 if (iinfo->i_crtime.tv_sec > inode->i_ctime.tv_sec ||
1533 (iinfo->i_crtime.tv_sec == inode->i_ctime.tv_sec &&
1534 iinfo->i_crtime.tv_nsec > inode->i_ctime.tv_nsec))
1535 iinfo->i_crtime = inode->i_ctime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Marcin Slusarz56774802008-02-10 11:25:31 +01001537 udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
1538 udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
1539 udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
1540 udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001542 memset(&(efe->impIdent), 0, sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1544 efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1545 efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001546 efe->uniqueID = cpu_to_le64(iinfo->i_unique);
1547 efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1548 efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1550 crclen = sizeof(struct extendedFileEntry);
1551 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001552 if (iinfo->i_strat4096) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 fe->icbTag.strategyType = cpu_to_le16(4096);
1554 fe->icbTag.strategyParameter = cpu_to_le16(1);
1555 fe->icbTag.numEntries = cpu_to_le16(2);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001556 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 fe->icbTag.strategyType = cpu_to_le16(4);
1558 fe->icbTag.numEntries = cpu_to_le16(1);
1559 }
1560
1561 if (S_ISDIR(inode->i_mode))
1562 fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
1563 else if (S_ISREG(inode->i_mode))
1564 fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
1565 else if (S_ISLNK(inode->i_mode))
1566 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
1567 else if (S_ISBLK(inode->i_mode))
1568 fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
1569 else if (S_ISCHR(inode->i_mode))
1570 fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
1571 else if (S_ISFIFO(inode->i_mode))
1572 fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
1573 else if (S_ISSOCK(inode->i_mode))
1574 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
1575
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001576 icbflags = iinfo->i_alloc_type |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001577 ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
1578 ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
1579 ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
1580 (le16_to_cpu(fe->icbTag.flags) &
1581 ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
1582 ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
1584 fe->icbTag.flags = cpu_to_le16(icbflags);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001585 if (sbi->s_udfrev >= 0x0200)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 fe->descTag.descVersion = cpu_to_le16(3);
1587 else
1588 fe->descTag.descVersion = cpu_to_le16(2);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001589 fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001590 fe->descTag.tagLocation = cpu_to_le32(
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001591 iinfo->i_location.logicalBlockNum);
Jan Karaaae917c2010-01-08 16:46:29 +01001592 crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 fe->descTag.descCRCLength = cpu_to_le16(crclen);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001594 fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02001595 crclen));
Marcin Slusarz3f2587b2008-02-08 04:20:39 -08001596 fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Jan Karaaae917c2010-01-08 16:46:29 +01001598out:
Jan Kara5833ded2010-01-08 16:52:59 +01001599 set_buffer_uptodate(bh);
Jan Karaaae917c2010-01-08 16:46:29 +01001600 unlock_buffer(bh);
1601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 /* write the data blocks */
1603 mark_buffer_dirty(bh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001604 if (do_sync) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 sync_dirty_buffer(bh);
Jan Karaaae917c2010-01-08 16:46:29 +01001606 if (buffer_write_io_error(bh)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001607 printk(KERN_WARNING "IO error syncing udf inode "
1608 "[%s:%08lx]\n", inode->i_sb->s_id,
1609 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 err = -EIO;
1611 }
1612 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001613 brelse(bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 return err;
1616}
1617
Pekka Enberg97e961f2008-10-15 12:29:03 +02001618struct inode *udf_iget(struct super_block *sb, struct kernel_lb_addr *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619{
1620 unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1621 struct inode *inode = iget_locked(sb, block);
1622
1623 if (!inode)
1624 return NULL;
1625
1626 if (inode->i_state & I_NEW) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02001627 memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 __udf_read_inode(inode);
1629 unlock_new_inode(inode);
1630 }
1631
1632 if (is_bad_inode(inode))
1633 goto out_iput;
1634
Pekka Enberg97e961f2008-10-15 12:29:03 +02001635 if (ino->logicalBlockNum >= UDF_SB(sb)->
1636 s_partmaps[ino->partitionReferenceNum].s_partition_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 udf_debug("block=%d, partition=%d out of range\n",
Pekka Enberg97e961f2008-10-15 12:29:03 +02001638 ino->logicalBlockNum, ino->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 make_bad_inode(inode);
1640 goto out_iput;
1641 }
1642
1643 return inode;
1644
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001645 out_iput:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 iput(inode);
1647 return NULL;
1648}
1649
Marcin Slusarz4b111112008-02-08 04:20:36 -08001650int8_t udf_add_aext(struct inode *inode, struct extent_position *epos,
Pekka Enberg97e961f2008-10-15 12:29:03 +02001651 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652{
1653 int adsize;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001654 struct short_ad *sad = NULL;
1655 struct long_ad *lad = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 struct allocExtDesc *aed;
1657 int8_t etype;
1658 uint8_t *ptr;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001659 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Jan Karaff116fc2007-05-08 00:35:14 -07001661 if (!epos->bh)
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001662 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001663 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001664 iinfo->i_lenEAttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 else
Jan Karaff116fc2007-05-08 00:35:14 -07001666 ptr = epos->bh->b_data + epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001668 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001669 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001670 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001671 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 else
1673 return -1;
1674
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001675 if (epos->offset + (2 * adsize) > inode->i_sb->s_blocksize) {
Al Viro391e8bb2010-01-31 21:28:48 -05001676 unsigned char *sptr, *dptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 struct buffer_head *nbh;
1678 int err, loffset;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001679 struct kernel_lb_addr obloc = epos->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
Marcin Slusarz4b111112008-02-08 04:20:36 -08001681 epos->block.logicalBlockNum = udf_new_block(inode->i_sb, NULL,
1682 obloc.partitionReferenceNum,
1683 obloc.logicalBlockNum, &err);
1684 if (!epos->block.logicalBlockNum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 return -1;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001686 nbh = udf_tgetblk(inode->i_sb, udf_get_lb_pblock(inode->i_sb,
Pekka Enberg97e961f2008-10-15 12:29:03 +02001687 &epos->block,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001688 0));
1689 if (!nbh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 lock_buffer(nbh);
1692 memset(nbh->b_data, 0x00, inode->i_sb->s_blocksize);
1693 set_buffer_uptodate(nbh);
1694 unlock_buffer(nbh);
1695 mark_buffer_dirty_inode(nbh, inode);
1696
1697 aed = (struct allocExtDesc *)(nbh->b_data);
1698 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001699 aed->previousAllocExtLocation =
1700 cpu_to_le32(obloc.logicalBlockNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001701 if (epos->offset + adsize > inode->i_sb->s_blocksize) {
Jan Karaff116fc2007-05-08 00:35:14 -07001702 loffset = epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 aed->lengthAllocDescs = cpu_to_le32(adsize);
1704 sptr = ptr - adsize;
1705 dptr = nbh->b_data + sizeof(struct allocExtDesc);
1706 memcpy(dptr, sptr, adsize);
Jan Karaff116fc2007-05-08 00:35:14 -07001707 epos->offset = sizeof(struct allocExtDesc) + adsize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001708 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001709 loffset = epos->offset + adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 aed->lengthAllocDescs = cpu_to_le32(0);
1711 sptr = ptr;
Jan Karaff116fc2007-05-08 00:35:14 -07001712 epos->offset = sizeof(struct allocExtDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001714 if (epos->bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07001715 aed = (struct allocExtDesc *)epos->bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01001716 le32_add_cpu(&aed->lengthAllocDescs, adsize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001717 } else {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001718 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 mark_inode_dirty(inode);
1720 }
1721 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001722 if (UDF_SB(inode->i_sb)->s_udfrev >= 0x0200)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 3, 1,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001724 epos->block.logicalBlockNum, sizeof(struct tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 else
1726 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 2, 1,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001727 epos->block.logicalBlockNum, sizeof(struct tag));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001728 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001729 case ICBTAG_FLAG_AD_SHORT:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001730 sad = (struct short_ad *)sptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001731 sad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1732 inode->i_sb->s_blocksize);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001733 sad->extPosition =
1734 cpu_to_le32(epos->block.logicalBlockNum);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001735 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001736 case ICBTAG_FLAG_AD_LONG:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001737 lad = (struct long_ad *)sptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001738 lad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1739 inode->i_sb->s_blocksize);
1740 lad->extLocation = cpu_to_lelb(epos->block);
1741 memset(lad->impUse, 0x00, sizeof(lad->impUse));
1742 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001744 if (epos->bh) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001745 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001746 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Jan Karaff116fc2007-05-08 00:35:14 -07001747 udf_update_tag(epos->bh->b_data, loffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08001749 udf_update_tag(epos->bh->b_data,
1750 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07001751 mark_buffer_dirty_inode(epos->bh, inode);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001752 brelse(epos->bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001753 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001755 }
Jan Karaff116fc2007-05-08 00:35:14 -07001756 epos->bh = nbh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 }
1758
Jan Karaff116fc2007-05-08 00:35:14 -07001759 etype = udf_write_aext(inode, epos, eloc, elen, inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001761 if (!epos->bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001762 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001764 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001765 aed = (struct allocExtDesc *)epos->bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01001766 le32_add_cpu(&aed->lengthAllocDescs, adsize);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001767 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1768 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
1769 udf_update_tag(epos->bh->b_data,
1770 epos->offset + (inc ? 0 : adsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08001772 udf_update_tag(epos->bh->b_data,
1773 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07001774 mark_buffer_dirty_inode(epos->bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 }
1776
1777 return etype;
1778}
1779
Marcin Slusarz4b111112008-02-08 04:20:36 -08001780int8_t udf_write_aext(struct inode *inode, struct extent_position *epos,
Pekka Enberg97e961f2008-10-15 12:29:03 +02001781 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782{
1783 int adsize;
1784 uint8_t *ptr;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001785 struct short_ad *sad;
1786 struct long_ad *lad;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001787 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Jan Karaff116fc2007-05-08 00:35:14 -07001789 if (!epos->bh)
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001790 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001791 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001792 iinfo->i_lenEAttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 else
Jan Karaff116fc2007-05-08 00:35:14 -07001794 ptr = epos->bh->b_data + epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001796 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001797 case ICBTAG_FLAG_AD_SHORT:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001798 sad = (struct short_ad *)ptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001799 sad->extLength = cpu_to_le32(elen);
Pekka Enberg97e961f2008-10-15 12:29:03 +02001800 sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001801 adsize = sizeof(struct short_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001802 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001803 case ICBTAG_FLAG_AD_LONG:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001804 lad = (struct long_ad *)ptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001805 lad->extLength = cpu_to_le32(elen);
Pekka Enberg97e961f2008-10-15 12:29:03 +02001806 lad->extLocation = cpu_to_lelb(*eloc);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001807 memset(lad->impUse, 0x00, sizeof(lad->impUse));
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001808 adsize = sizeof(struct long_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001809 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001810 default:
1811 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 }
1813
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001814 if (epos->bh) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001815 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001816 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001817 struct allocExtDesc *aed =
1818 (struct allocExtDesc *)epos->bh->b_data;
Jan Karaff116fc2007-05-08 00:35:14 -07001819 udf_update_tag(epos->bh->b_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001820 le32_to_cpu(aed->lengthAllocDescs) +
1821 sizeof(struct allocExtDesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 }
Jan Karaff116fc2007-05-08 00:35:14 -07001823 mark_buffer_dirty_inode(epos->bh, inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001824 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
1828 if (inc)
Jan Karaff116fc2007-05-08 00:35:14 -07001829 epos->offset += adsize;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001830
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 return (elen >> 30);
1832}
1833
Marcin Slusarz4b111112008-02-08 04:20:36 -08001834int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001835 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
1837 int8_t etype;
1838
Jan Karaff116fc2007-05-08 00:35:14 -07001839 while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001840 (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001841 int block;
Jan Karaff116fc2007-05-08 00:35:14 -07001842 epos->block = *eloc;
1843 epos->offset = sizeof(struct allocExtDesc);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001844 brelse(epos->bh);
Pekka Enberg97e961f2008-10-15 12:29:03 +02001845 block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001846 epos->bh = udf_tread(inode->i_sb, block);
1847 if (!epos->bh) {
1848 udf_debug("reading block %d failed!\n", block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 return -1;
1850 }
1851 }
1852
1853 return etype;
1854}
1855
Marcin Slusarz4b111112008-02-08 04:20:36 -08001856int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001857 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858{
1859 int alen;
1860 int8_t etype;
1861 uint8_t *ptr;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001862 struct short_ad *sad;
1863 struct long_ad *lad;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001864 struct udf_inode_info *iinfo = UDF_I(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001865
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001866 if (!epos->bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07001867 if (!epos->offset)
1868 epos->offset = udf_file_entry_alloc_offset(inode);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001869 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001870 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001871 iinfo->i_lenEAttr;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001872 alen = udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001873 iinfo->i_lenAlloc;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001874 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001875 if (!epos->offset)
1876 epos->offset = sizeof(struct allocExtDesc);
1877 ptr = epos->bh->b_data + epos->offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001878 alen = sizeof(struct allocExtDesc) +
Marcin Slusarz4b111112008-02-08 04:20:36 -08001879 le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
1880 lengthAllocDescs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 }
1882
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001883 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001884 case ICBTAG_FLAG_AD_SHORT:
Marcin Slusarz4b111112008-02-08 04:20:36 -08001885 sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
1886 if (!sad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 return -1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001888 etype = le32_to_cpu(sad->extLength) >> 30;
1889 eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001890 eloc->partitionReferenceNum =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001891 iinfo->i_location.partitionReferenceNum;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001892 *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
1893 break;
1894 case ICBTAG_FLAG_AD_LONG:
Marcin Slusarz4b111112008-02-08 04:20:36 -08001895 lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
1896 if (!lad)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001897 return -1;
1898 etype = le32_to_cpu(lad->extLength) >> 30;
1899 *eloc = lelb_to_cpu(lad->extLocation);
1900 *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
1901 break;
1902 default:
Marcin Slusarz4b111112008-02-08 04:20:36 -08001903 udf_debug("alloc_type = %d unsupported\n",
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001904 iinfo->i_alloc_type);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001905 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 }
1907
1908 return etype;
1909}
1910
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001911static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001912 struct kernel_lb_addr neloc, uint32_t nelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001914 struct kernel_lb_addr oeloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 uint32_t oelen;
1916 int8_t etype;
1917
Jan Karaff116fc2007-05-08 00:35:14 -07001918 if (epos.bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001919 get_bh(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001921 while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02001922 udf_write_aext(inode, &epos, &neloc, nelen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 neloc = oeloc;
1924 nelen = (etype << 30) | oelen;
1925 }
Pekka Enberg97e961f2008-10-15 12:29:03 +02001926 udf_add_aext(inode, &epos, &neloc, nelen, 1);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001927 brelse(epos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001928
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 return (nelen >> 30);
1930}
1931
Marcin Slusarz4b111112008-02-08 04:20:36 -08001932int8_t udf_delete_aext(struct inode *inode, struct extent_position epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001933 struct kernel_lb_addr eloc, uint32_t elen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
Jan Karaff116fc2007-05-08 00:35:14 -07001935 struct extent_position oepos;
1936 int adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 int8_t etype;
1938 struct allocExtDesc *aed;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001939 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001941 if (epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07001942 get_bh(epos.bh);
1943 get_bh(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 }
1945
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001946 iinfo = UDF_I(inode);
1947 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001948 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001949 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001950 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 else
1952 adsize = 0;
1953
Jan Karaff116fc2007-05-08 00:35:14 -07001954 oepos = epos;
1955 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 return -1;
1957
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001958 while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02001959 udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001960 if (oepos.bh != epos.bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07001961 oepos.block = epos.block;
Jan Kara3bf25cb2007-05-08 00:35:16 -07001962 brelse(oepos.bh);
1963 get_bh(epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -07001964 oepos.bh = epos.bh;
1965 oepos.offset = epos.offset - adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 }
1967 }
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001968 memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 elen = 0;
1970
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001971 if (epos.bh != oepos.bh) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02001972 udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
1973 udf_write_aext(inode, &oepos, &eloc, elen, 1);
1974 udf_write_aext(inode, &oepos, &eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001975 if (!oepos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001976 iinfo->i_lenAlloc -= (adsize * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001978 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001979 aed = (struct allocExtDesc *)oepos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01001980 le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001981 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001982 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Marcin Slusarz4b111112008-02-08 04:20:36 -08001983 udf_update_tag(oepos.bh->b_data,
1984 oepos.offset - (2 * adsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08001986 udf_update_tag(oepos.bh->b_data,
1987 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07001988 mark_buffer_dirty_inode(oepos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001990 } else {
Pekka Enberg97e961f2008-10-15 12:29:03 +02001991 udf_write_aext(inode, &oepos, &eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001992 if (!oepos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001993 iinfo->i_lenAlloc -= adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001995 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001996 aed = (struct allocExtDesc *)oepos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01001997 le32_add_cpu(&aed->lengthAllocDescs, -adsize);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001998 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001999 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Marcin Slusarz4b111112008-02-08 04:20:36 -08002000 udf_update_tag(oepos.bh->b_data,
2001 epos.offset - adsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08002003 udf_update_tag(oepos.bh->b_data,
2004 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07002005 mark_buffer_dirty_inode(oepos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 }
2007 }
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07002008
Jan Kara3bf25cb2007-05-08 00:35:16 -07002009 brelse(epos.bh);
2010 brelse(oepos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 return (elen >> 30);
2013}
2014
Marcin Slusarz4b111112008-02-08 04:20:36 -08002015int8_t inode_bmap(struct inode *inode, sector_t block,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002016 struct extent_position *pos, struct kernel_lb_addr *eloc,
Marcin Slusarz4b111112008-02-08 04:20:36 -08002017 uint32_t *elen, sector_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018{
Marcin Slusarz4b111112008-02-08 04:20:36 -08002019 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002020 loff_t lbcount = 0, bcount =
Marcin Slusarz4b111112008-02-08 04:20:36 -08002021 (loff_t) block << blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002023 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002025 iinfo = UDF_I(inode);
Jan Karaff116fc2007-05-08 00:35:14 -07002026 pos->offset = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002027 pos->block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -07002028 pos->bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 *elen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002031 do {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002032 etype = udf_next_aext(inode, pos, eloc, elen, 1);
2033 if (etype == -1) {
2034 *offset = (bcount - lbcount) >> blocksize_bits;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002035 iinfo->i_lenExtents = lbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 return -1;
2037 }
2038 lbcount += *elen;
2039 } while (lbcount <= bcount);
2040
Marcin Slusarz4b111112008-02-08 04:20:36 -08002041 *offset = (bcount + *elen - lbcount) >> blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
2043 return etype;
2044}
2045
Jan Kara60448b12007-05-08 00:35:13 -07002046long udf_block_map(struct inode *inode, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002048 struct kernel_lb_addr eloc;
Jan Karaff116fc2007-05-08 00:35:14 -07002049 uint32_t elen;
Jan Kara60448b12007-05-08 00:35:13 -07002050 sector_t offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002051 struct extent_position epos = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 int ret;
2053
2054 lock_kernel();
2055
Marcin Slusarz4b111112008-02-08 04:20:36 -08002056 if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
2057 (EXT_RECORDED_ALLOCATED >> 30))
Pekka Enberg97e961f2008-10-15 12:29:03 +02002058 ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 else
2060 ret = 0;
2061
2062 unlock_kernel();
Jan Kara3bf25cb2007-05-08 00:35:16 -07002063 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
2065 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
2066 return udf_fixed_to_variable(ret);
2067 else
2068 return ret;
2069}