blob: ecddcc2ed7463a889e0a06d0c93d077dbf1feb13 [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{
Christoph Hellwig155130a2010-06-04 11:29:58 +0200130 int ret;
131
132 ret = block_write_begin(mapping, pos, len, flags, pagep, udf_get_block);
133 if (unlikely(ret)) {
134 loff_t isize = mapping->host->i_size;
135 if (pos + len > isize)
136 vmtruncate(mapping->host, isize);
137 }
138
139 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
142static sector_t udf_bmap(struct address_space *mapping, sector_t block)
143{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700144 return generic_block_bmap(mapping, block, udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700147const struct address_space_operations udf_aops = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700148 .readpage = udf_readpage,
149 .writepage = udf_writepage,
150 .sync_page = block_sync_page,
Nick Pigginbe021ee2007-10-16 01:25:20 -0700151 .write_begin = udf_write_begin,
152 .write_end = generic_write_end,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700153 .bmap = udf_bmap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154};
155
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700156void udf_expand_file_adinicb(struct inode *inode, int newsize, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
158 struct page *page;
159 char *kaddr;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800160 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 struct writeback_control udf_wbc = {
162 .sync_mode = WB_SYNC_NONE,
163 .nr_to_write = 1,
164 };
165
166 /* from now on we have normal address_space methods */
167 inode->i_data.a_ops = &udf_aops;
168
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800169 if (!iinfo->i_lenAlloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800171 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 else
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800173 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 mark_inode_dirty(inode);
175 return;
176 }
177
178 page = grab_cache_page(inode->i_mapping, 0);
Matt Mackallcd7619d2005-05-01 08:59:01 -0700179 BUG_ON(!PageLocked(page));
180
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700181 if (!PageUptodate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 kaddr = kmap(page);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800183 memset(kaddr + iinfo->i_lenAlloc, 0x00,
184 PAGE_CACHE_SIZE - iinfo->i_lenAlloc);
185 memcpy(kaddr, iinfo->i_ext.i_data + iinfo->i_lenEAttr,
186 iinfo->i_lenAlloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 flush_dcache_page(page);
188 SetPageUptodate(page);
189 kunmap(page);
190 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800191 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0x00,
192 iinfo->i_lenAlloc);
193 iinfo->i_lenAlloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800195 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 else
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800197 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 inode->i_data.a_ops->writepage(page, &udf_wbc);
200 page_cache_release(page);
201
202 mark_inode_dirty(inode);
203}
204
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700205struct buffer_head *udf_expand_dir_adinicb(struct inode *inode, int *block,
206 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
208 int newblock;
Jan Karaff116fc2007-05-08 00:35:14 -0700209 struct buffer_head *dbh = NULL;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200210 struct kernel_lb_addr eloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 uint8_t alloctype;
Jan Karaff116fc2007-05-08 00:35:14 -0700212 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 struct udf_fileident_bh sfibh, dfibh;
Jan Karaaf793292008-02-08 04:20:50 -0800215 loff_t f_pos = udf_ext0_offset(inode);
216 int size = udf_ext0_offset(inode) + inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 struct fileIdentDesc cfi, *sfi, *dfi;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800218 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
221 alloctype = ICBTAG_FLAG_AD_SHORT;
222 else
223 alloctype = ICBTAG_FLAG_AD_LONG;
224
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700225 if (!inode->i_size) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800226 iinfo->i_alloc_type = alloctype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 mark_inode_dirty(inode);
228 return NULL;
229 }
230
231 /* alloc block, and copy data to it */
232 *block = udf_new_block(inode->i_sb, inode,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800233 iinfo->i_location.partitionReferenceNum,
234 iinfo->i_location.logicalBlockNum, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 if (!(*block))
236 return NULL;
237 newblock = udf_get_pblock(inode->i_sb, *block,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800238 iinfo->i_location.partitionReferenceNum,
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800239 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (!newblock)
241 return NULL;
242 dbh = udf_tgetblk(inode->i_sb, newblock);
243 if (!dbh)
244 return NULL;
245 lock_buffer(dbh);
246 memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
247 set_buffer_uptodate(dbh);
248 unlock_buffer(dbh);
249 mark_buffer_dirty_inode(dbh, inode);
250
Marcin Slusarz4b111112008-02-08 04:20:36 -0800251 sfibh.soffset = sfibh.eoffset =
Jan Karaaf793292008-02-08 04:20:50 -0800252 f_pos & (inode->i_sb->s_blocksize - 1);
Jan Karaff116fc2007-05-08 00:35:14 -0700253 sfibh.sbh = sfibh.ebh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 dfibh.soffset = dfibh.eoffset = 0;
255 dfibh.sbh = dfibh.ebh = dbh;
Jan Karaaf793292008-02-08 04:20:50 -0800256 while (f_pos < size) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800257 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800258 sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL,
259 NULL, NULL, NULL);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700260 if (!sfi) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700261 brelse(dbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return NULL;
263 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800264 iinfo->i_alloc_type = alloctype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 sfi->descTag.tagLocation = cpu_to_le32(*block);
266 dfibh.soffset = dfibh.eoffset;
267 dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
268 dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
269 if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800270 sfi->fileIdent +
271 le16_to_cpu(sfi->lengthOfImpUse))) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800272 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700273 brelse(dbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return NULL;
275 }
276 }
277 mark_buffer_dirty_inode(dbh, inode);
278
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800279 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0,
280 iinfo->i_lenAlloc);
281 iinfo->i_lenAlloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 eloc.logicalBlockNum = *block;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800283 eloc.partitionReferenceNum =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800284 iinfo->i_location.partitionReferenceNum;
Jan Kara2c948b32009-12-03 13:39:28 +0100285 iinfo->i_lenExtents = inode->i_size;
Jan Karaff116fc2007-05-08 00:35:14 -0700286 epos.bh = NULL;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800287 epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700288 epos.offset = udf_file_entry_alloc_offset(inode);
Jan Kara2c948b32009-12-03 13:39:28 +0100289 udf_add_aext(inode, &epos, &eloc, inode->i_size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 /* UniqueID stuff */
291
Jan Kara3bf25cb2007-05-08 00:35:16 -0700292 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 mark_inode_dirty(inode);
294 return dbh;
295}
296
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700297static int udf_get_block(struct inode *inode, sector_t block,
298 struct buffer_head *bh_result, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 int err, new;
301 struct buffer_head *bh;
Marcin Slusarz1ed16172008-02-08 04:20:48 -0800302 sector_t phys = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800303 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700305 if (!create) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 phys = udf_block_map(inode, block);
307 if (phys)
308 map_bh(bh_result, inode->i_sb, phys);
309 return 0;
310 }
311
312 err = -EIO;
313 new = 0;
314 bh = NULL;
315
316 lock_kernel();
317
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800318 iinfo = UDF_I(inode);
319 if (block == iinfo->i_next_alloc_block + 1) {
320 iinfo->i_next_alloc_block++;
321 iinfo->i_next_alloc_goal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 }
323
324 err = 0;
325
326 bh = inode_getblk(inode, block, &err, &phys, &new);
Eric Sesterhenn2c2111c2006-04-02 13:40:13 +0200327 BUG_ON(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 if (err)
329 goto abort;
Eric Sesterhenn2c2111c2006-04-02 13:40:13 +0200330 BUG_ON(!phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 if (new)
333 set_buffer_new(bh_result);
334 map_bh(bh_result, inode->i_sb, phys);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700335
336abort:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 unlock_kernel();
338 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700341static struct buffer_head *udf_getblk(struct inode *inode, long block,
342 int create, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700344 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 struct buffer_head dummy;
346
347 dummy.b_state = 0;
348 dummy.b_blocknr = -1000;
349 *err = udf_get_block(inode, block, &dummy, create);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700350 if (!*err && buffer_mapped(&dummy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700352 if (buffer_new(&dummy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 lock_buffer(bh);
354 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
355 set_buffer_uptodate(bh);
356 unlock_buffer(bh);
357 mark_buffer_dirty_inode(bh, inode);
358 }
359 return bh;
360 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return NULL;
363}
364
Jan Kara31170b62007-05-08 00:35:21 -0700365/* Extend the file by 'blocks' blocks, return the number of extents added */
366int udf_extend_file(struct inode *inode, struct extent_position *last_pos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200367 struct kernel_long_ad *last_ext, sector_t blocks)
Jan Kara31170b62007-05-08 00:35:21 -0700368{
369 sector_t add;
370 int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
371 struct super_block *sb = inode->i_sb;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200372 struct kernel_lb_addr prealloc_loc = {};
Jan Kara31170b62007-05-08 00:35:21 -0700373 int prealloc_len = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800374 struct udf_inode_info *iinfo;
Jan Kara31170b62007-05-08 00:35:21 -0700375
376 /* The previous extent is fake and we should not extend by anything
377 * - there's nothing to do... */
378 if (!blocks && fake)
379 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700380
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800381 iinfo = UDF_I(inode);
Jan Kara31170b62007-05-08 00:35:21 -0700382 /* Round the last extent up to a multiple of block size */
383 if (last_ext->extLength & (sb->s_blocksize - 1)) {
384 last_ext->extLength =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700385 (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
386 (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
387 sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800388 iinfo->i_lenExtents =
389 (iinfo->i_lenExtents + sb->s_blocksize - 1) &
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700390 ~(sb->s_blocksize - 1);
Jan Kara31170b62007-05-08 00:35:21 -0700391 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700392
Jan Kara31170b62007-05-08 00:35:21 -0700393 /* Last extent are just preallocated blocks? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800394 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
395 EXT_NOT_RECORDED_ALLOCATED) {
Jan Kara31170b62007-05-08 00:35:21 -0700396 /* Save the extent so that we can reattach it to the end */
397 prealloc_loc = last_ext->extLocation;
398 prealloc_len = last_ext->extLength;
399 /* Mark the extent as a hole */
400 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700401 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
Jan Kara31170b62007-05-08 00:35:21 -0700402 last_ext->extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800403 last_ext->extLocation.partitionReferenceNum = 0;
Jan Kara31170b62007-05-08 00:35:21 -0700404 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700405
Jan Kara31170b62007-05-08 00:35:21 -0700406 /* Can we merge with the previous extent? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800407 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
408 EXT_NOT_RECORDED_NOT_ALLOCATED) {
409 add = ((1 << 30) - sb->s_blocksize -
410 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK)) >>
411 sb->s_blocksize_bits;
Jan Kara31170b62007-05-08 00:35:21 -0700412 if (add > blocks)
413 add = blocks;
414 blocks -= add;
415 last_ext->extLength += add << sb->s_blocksize_bits;
416 }
417
418 if (fake) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200419 udf_add_aext(inode, last_pos, &last_ext->extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700420 last_ext->extLength, 1);
Jan Kara31170b62007-05-08 00:35:21 -0700421 count++;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800422 } else
Pekka Enberg97e961f2008-10-15 12:29:03 +0200423 udf_write_aext(inode, last_pos, &last_ext->extLocation,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800424 last_ext->extLength, 1);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700425
Jan Kara31170b62007-05-08 00:35:21 -0700426 /* Managed to do everything necessary? */
427 if (!blocks)
428 goto out;
429
430 /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
431 last_ext->extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800432 last_ext->extLocation.partitionReferenceNum = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700433 add = (1 << (30-sb->s_blocksize_bits)) - 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800434 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
435 (add << sb->s_blocksize_bits);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700436
Jan Kara31170b62007-05-08 00:35:21 -0700437 /* Create enough extents to cover the whole hole */
438 while (blocks > add) {
439 blocks -= add;
Pekka Enberg97e961f2008-10-15 12:29:03 +0200440 if (udf_add_aext(inode, last_pos, &last_ext->extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700441 last_ext->extLength, 1) == -1)
Jan Kara31170b62007-05-08 00:35:21 -0700442 return -1;
443 count++;
444 }
445 if (blocks) {
446 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700447 (blocks << sb->s_blocksize_bits);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200448 if (udf_add_aext(inode, last_pos, &last_ext->extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700449 last_ext->extLength, 1) == -1)
Jan Kara31170b62007-05-08 00:35:21 -0700450 return -1;
451 count++;
452 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700453
454out:
Jan Kara31170b62007-05-08 00:35:21 -0700455 /* Do we have some preallocated blocks saved? */
456 if (prealloc_len) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200457 if (udf_add_aext(inode, last_pos, &prealloc_loc,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800458 prealloc_len, 1) == -1)
Jan Kara31170b62007-05-08 00:35:21 -0700459 return -1;
460 last_ext->extLocation = prealloc_loc;
461 last_ext->extLength = prealloc_len;
462 count++;
463 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700464
Jan Kara31170b62007-05-08 00:35:21 -0700465 /* last_pos should point to the last written extent... */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800466 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200467 last_pos->offset -= sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800468 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200469 last_pos->offset -= sizeof(struct long_ad);
Jan Kara31170b62007-05-08 00:35:21 -0700470 else
471 return -1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700472
Jan Kara31170b62007-05-08 00:35:21 -0700473 return count;
474}
475
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700476static struct buffer_head *inode_getblk(struct inode *inode, sector_t block,
Marcin Slusarz1ed16172008-02-08 04:20:48 -0800477 int *err, sector_t *phys, int *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Jan Kara31170b62007-05-08 00:35:21 -0700479 static sector_t last_block;
Jan Karaff116fc2007-05-08 00:35:14 -0700480 struct buffer_head *result = NULL;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200481 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE];
Jan Karaff116fc2007-05-08 00:35:14 -0700482 struct extent_position prev_epos, cur_epos, next_epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 int count = 0, startnum = 0, endnum = 0;
Jan Kara85d71242007-06-01 00:46:29 -0700484 uint32_t elen = 0, tmpelen;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200485 struct kernel_lb_addr eloc, tmpeloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 int c = 1;
Jan Kara60448b12007-05-08 00:35:13 -0700487 loff_t lbcount = 0, b_off = 0;
488 uint32_t newblocknum, newblock;
489 sector_t offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800491 struct udf_inode_info *iinfo = UDF_I(inode);
492 int goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
Jan Kara31170b62007-05-08 00:35:21 -0700493 int lastblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Jan Karaff116fc2007-05-08 00:35:14 -0700495 prev_epos.offset = udf_file_entry_alloc_offset(inode);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800496 prev_epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700497 prev_epos.bh = NULL;
498 cur_epos = next_epos = prev_epos;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700499 b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 /* find the extent which contains the block we are looking for.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700502 alternate between laarr[0] and laarr[1] for locations of the
503 current extent, and the previous extent */
504 do {
505 if (prev_epos.bh != cur_epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700506 brelse(prev_epos.bh);
507 get_bh(cur_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700508 prev_epos.bh = cur_epos.bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700510 if (cur_epos.bh != next_epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700511 brelse(cur_epos.bh);
512 get_bh(next_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700513 cur_epos.bh = next_epos.bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
515
516 lbcount += elen;
517
Jan Karaff116fc2007-05-08 00:35:14 -0700518 prev_epos.block = cur_epos.block;
519 cur_epos.block = next_epos.block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Jan Karaff116fc2007-05-08 00:35:14 -0700521 prev_epos.offset = cur_epos.offset;
522 cur_epos.offset = next_epos.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Marcin Slusarz4b111112008-02-08 04:20:36 -0800524 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
525 if (etype == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 break;
527
528 c = !c;
529
530 laarr[c].extLength = (etype << 30) | elen;
531 laarr[c].extLocation = eloc;
532
533 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
534 pgoal = eloc.logicalBlockNum +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700535 ((elen + inode->i_sb->s_blocksize - 1) >>
536 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700538 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 } while (lbcount + elen <= b_off);
540
541 b_off -= lbcount;
542 offset = b_off >> inode->i_sb->s_blocksize_bits;
Jan Kara85d71242007-06-01 00:46:29 -0700543 /*
544 * Move prev_epos and cur_epos into indirect extent if we are at
545 * the pointer to it
546 */
547 udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
548 udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 /* if the extent is allocated and recorded, return the block
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700551 if the extent is not a multiple of the blocksize, round up */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700553 if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
554 if (elen & (inode->i_sb->s_blocksize - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 elen = EXT_RECORDED_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700556 ((elen + inode->i_sb->s_blocksize - 1) &
557 ~(inode->i_sb->s_blocksize - 1));
Pekka Enberg97e961f2008-10-15 12:29:03 +0200558 etype = udf_write_aext(inode, &cur_epos, &eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
Jan Kara3bf25cb2007-05-08 00:35:16 -0700560 brelse(prev_epos.bh);
561 brelse(cur_epos.bh);
562 brelse(next_epos.bh);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200563 newblock = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 *phys = newblock;
565 return NULL;
566 }
567
Jan Kara31170b62007-05-08 00:35:21 -0700568 last_block = block;
569 /* Are we beyond EOF? */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700570 if (etype == -1) {
Jan Kara31170b62007-05-08 00:35:21 -0700571 int ret;
572
573 if (count) {
574 if (c)
575 laarr[0] = laarr[1];
576 startnum = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700577 } else {
Jan Kara31170b62007-05-08 00:35:21 -0700578 /* Create a fake extent when there's not one */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800579 memset(&laarr[0].extLocation, 0x00,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200580 sizeof(struct kernel_lb_addr));
Jan Kara31170b62007-05-08 00:35:21 -0700581 laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800582 /* Will udf_extend_file() create real extent from
583 a fake one? */
Jan Kara31170b62007-05-08 00:35:21 -0700584 startnum = (offset > 0);
585 }
586 /* Create extents for the hole between EOF and offset */
587 ret = udf_extend_file(inode, &prev_epos, laarr, offset);
588 if (ret == -1) {
589 brelse(prev_epos.bh);
590 brelse(cur_epos.bh);
591 brelse(next_epos.bh);
592 /* We don't really know the error here so we just make
593 * something up */
594 *err = -ENOSPC;
595 return NULL;
596 }
597 c = 0;
598 offset = 0;
599 count += ret;
600 /* We are not covered by a preallocated extent? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800601 if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
602 EXT_NOT_RECORDED_ALLOCATED) {
Jan Kara31170b62007-05-08 00:35:21 -0700603 /* Is there any real extent? - otherwise we overwrite
604 * the fake one... */
605 if (count)
606 c = !c;
607 laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700608 inode->i_sb->s_blocksize;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800609 memset(&laarr[c].extLocation, 0x00,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200610 sizeof(struct kernel_lb_addr));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700611 count++;
612 endnum++;
Jan Kara31170b62007-05-08 00:35:21 -0700613 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700614 endnum = c + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 lastblock = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700616 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 endnum = startnum = ((count > 2) ? 2 : count);
618
Marcin Slusarz4b111112008-02-08 04:20:36 -0800619 /* if the current extent is in position 0,
620 swap it with the previous */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700621 if (!c && count != 1) {
Jan Kara31170b62007-05-08 00:35:21 -0700622 laarr[2] = laarr[0];
623 laarr[0] = laarr[1];
624 laarr[1] = laarr[2];
625 c = 1;
626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Marcin Slusarz4b111112008-02-08 04:20:36 -0800628 /* if the current block is located in an extent,
629 read the next extent */
630 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
631 if (etype != -1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700632 laarr[c + 1].extLength = (etype << 30) | elen;
633 laarr[c + 1].extLocation = eloc;
634 count++;
635 startnum++;
636 endnum++;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800637 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 lastblock = 1;
639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 /* if the current extent is not recorded but allocated, get the
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700642 * block in the extent corresponding to the requested block */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800643 if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800645 else { /* otherwise, allocate a new block */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800646 if (iinfo->i_next_alloc_block == block)
647 goal = iinfo->i_next_alloc_goal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700649 if (!goal) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800650 if (!(goal = pgoal)) /* XXX: what was intended here? */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800651 goal = iinfo->i_location.logicalBlockNum + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 }
653
Marcin Slusarz4b111112008-02-08 04:20:36 -0800654 newblocknum = udf_new_block(inode->i_sb, inode,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800655 iinfo->i_location.partitionReferenceNum,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800656 goal, err);
657 if (!newblocknum) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700658 brelse(prev_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 *err = -ENOSPC;
660 return NULL;
661 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800662 iinfo->i_lenExtents += inode->i_sb->s_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
664
Marcin Slusarz4b111112008-02-08 04:20:36 -0800665 /* if the extent the requsted block is located in contains multiple
666 * blocks, split the extent into at most three extents. blocks prior
667 * to requested block, requested block, and blocks after requested
668 * block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
670
671#ifdef UDF_PREALLOCATE
Jan Kara81056dd2009-07-16 18:02:25 +0200672 /* We preallocate blocks only for regular files. It also makes sense
673 * for directories but there's a problem when to drop the
674 * preallocation. We might use some delayed work for that but I feel
675 * it's overengineering for a filesystem like UDF. */
676 if (S_ISREG(inode->i_mode))
677 udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678#endif
679
680 /* merge any continuous blocks in laarr */
681 udf_merge_extents(inode, laarr, &endnum);
682
683 /* write back the new extents, inserting new extents if the new number
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700684 * of extents is greater than the old number, and deleting extents if
685 * the new number of extents is less than the old number */
Jan Karaff116fc2007-05-08 00:35:14 -0700686 udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Jan Kara3bf25cb2007-05-08 00:35:16 -0700688 brelse(prev_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Marcin Slusarz4b111112008-02-08 04:20:36 -0800690 newblock = udf_get_pblock(inode->i_sb, newblocknum,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800691 iinfo->i_location.partitionReferenceNum, 0);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800692 if (!newblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 *phys = newblock;
695 *err = 0;
696 *new = 1;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800697 iinfo->i_next_alloc_block = block;
698 iinfo->i_next_alloc_goal = newblocknum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 inode->i_ctime = current_fs_time(inode->i_sb);
700
701 if (IS_SYNC(inode))
702 udf_sync_inode(inode);
703 else
704 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return result;
707}
708
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700709static void udf_split_extents(struct inode *inode, int *c, int offset,
710 int newblocknum,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200711 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700712 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
Marcin Slusarz4b111112008-02-08 04:20:36 -0800714 unsigned long blocksize = inode->i_sb->s_blocksize;
715 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
Marcin Slusarz4b111112008-02-08 04:20:36 -0800718 (laarr[*c].extLength >> 30) ==
719 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 int curr = *c;
721 int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800722 blocksize - 1) >> blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 int8_t etype = (laarr[curr].extLength >> 30);
724
Marcin Slusarz4b111112008-02-08 04:20:36 -0800725 if (blen == 1)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700726 ;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800727 else if (!offset || blen == offset + 1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700728 laarr[curr + 2] = laarr[curr + 1];
729 laarr[curr + 1] = laarr[curr];
730 } else {
731 laarr[curr + 3] = laarr[curr + 1];
732 laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700735 if (offset) {
736 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800737 udf_free_blocks(inode->i_sb, inode,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200738 &laarr[curr].extLocation,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800739 0, offset);
740 laarr[curr].extLength =
741 EXT_NOT_RECORDED_NOT_ALLOCATED |
742 (offset << blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 laarr[curr].extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800744 laarr[curr].extLocation.
745 partitionReferenceNum = 0;
746 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 laarr[curr].extLength = (etype << 30) |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800748 (offset << blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700749 curr++;
750 (*c)++;
751 (*endnum)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
Cyrill Gorcunov647bd612007-07-15 23:39:47 -0700753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 laarr[curr].extLocation.logicalBlockNum = newblocknum;
755 if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
756 laarr[curr].extLocation.partitionReferenceNum =
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800757 UDF_I(inode)->i_location.partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800759 blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700760 curr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700762 if (blen != offset + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
Marcin Slusarz4b111112008-02-08 04:20:36 -0800764 laarr[curr].extLocation.logicalBlockNum +=
765 offset + 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700766 laarr[curr].extLength = (etype << 30) |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800767 ((blen - (offset + 1)) << blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700768 curr++;
769 (*endnum)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 }
771 }
772}
773
774static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200775 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700776 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
778 int start, length = 0, currlength = 0, i;
779
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700780 if (*endnum >= (c + 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 if (!lastblock)
782 return;
783 else
784 start = c;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700785 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800786 if ((laarr[c + 1].extLength >> 30) ==
787 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700788 start = c + 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800789 length = currlength =
790 (((laarr[c + 1].extLength &
791 UDF_EXTENT_LENGTH_MASK) +
792 inode->i_sb->s_blocksize - 1) >>
793 inode->i_sb->s_blocksize_bits);
794 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 start = c;
796 }
797
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700798 for (i = start + 1; i <= *endnum; i++) {
799 if (i == *endnum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 if (lastblock)
801 length += UDF_DEFAULT_PREALLOC_BLOCKS;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800802 } else if ((laarr[i].extLength >> 30) ==
803 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
804 length += (((laarr[i].extLength &
805 UDF_EXTENT_LENGTH_MASK) +
806 inode->i_sb->s_blocksize - 1) >>
807 inode->i_sb->s_blocksize_bits);
808 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 break;
810 }
811
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700812 if (length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 int next = laarr[start].extLocation.logicalBlockNum +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700814 (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800815 inode->i_sb->s_blocksize - 1) >>
816 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800818 laarr[start].extLocation.partitionReferenceNum,
819 next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
820 length : UDF_DEFAULT_PREALLOC_BLOCKS) -
821 currlength);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700822 if (numalloc) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800823 if (start == (c + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 laarr[start].extLength +=
Marcin Slusarz4b111112008-02-08 04:20:36 -0800825 (numalloc <<
826 inode->i_sb->s_blocksize_bits);
827 else {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700828 memmove(&laarr[c + 2], &laarr[c + 1],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200829 sizeof(struct long_ad) * (*endnum - (c + 1)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700830 (*endnum)++;
831 laarr[c + 1].extLocation.logicalBlockNum = next;
832 laarr[c + 1].extLocation.partitionReferenceNum =
Marcin Slusarz4b111112008-02-08 04:20:36 -0800833 laarr[c].extLocation.
834 partitionReferenceNum;
835 laarr[c + 1].extLength =
836 EXT_NOT_RECORDED_ALLOCATED |
837 (numalloc <<
838 inode->i_sb->s_blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700839 start = c + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
841
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700842 for (i = start + 1; numalloc && i < *endnum; i++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800843 int elen = ((laarr[i].extLength &
844 UDF_EXTENT_LENGTH_MASK) +
845 inode->i_sb->s_blocksize - 1) >>
846 inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700848 if (elen > numalloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 laarr[i].extLength -=
Marcin Slusarz4b111112008-02-08 04:20:36 -0800850 (numalloc <<
851 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 numalloc = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700853 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 numalloc -= elen;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700855 if (*endnum > (i + 1))
Marcin Slusarz4b111112008-02-08 04:20:36 -0800856 memmove(&laarr[i],
857 &laarr[i + 1],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200858 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -0800859 (*endnum - (i + 1)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700860 i--;
861 (*endnum)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
863 }
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800864 UDF_I(inode)->i_lenExtents +=
Marcin Slusarz4b111112008-02-08 04:20:36 -0800865 numalloc << inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
867 }
868}
869
870static void udf_merge_extents(struct inode *inode,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200871 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700872 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
874 int i;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800875 unsigned long blocksize = inode->i_sb->s_blocksize;
876 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700878 for (i = 0; i < (*endnum - 1); i++) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200879 struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
880 struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Marcin Slusarz4b111112008-02-08 04:20:36 -0800882 if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
883 (((li->extLength >> 30) ==
884 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
885 ((lip1->extLocation.logicalBlockNum -
886 li->extLocation.logicalBlockNum) ==
887 (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
888 blocksize - 1) >> blocksize_bits)))) {
889
890 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
891 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
892 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
893 lip1->extLength = (lip1->extLength -
894 (li->extLength &
895 UDF_EXTENT_LENGTH_MASK) +
896 UDF_EXTENT_LENGTH_MASK) &
897 ~(blocksize - 1);
898 li->extLength = (li->extLength &
899 UDF_EXTENT_FLAG_MASK) +
900 (UDF_EXTENT_LENGTH_MASK + 1) -
901 blocksize;
902 lip1->extLocation.logicalBlockNum =
903 li->extLocation.logicalBlockNum +
904 ((li->extLength &
905 UDF_EXTENT_LENGTH_MASK) >>
906 blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700907 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800908 li->extLength = lip1->extLength +
909 (((li->extLength &
910 UDF_EXTENT_LENGTH_MASK) +
911 blocksize - 1) & ~(blocksize - 1));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700912 if (*endnum > (i + 2))
913 memmove(&laarr[i + 1], &laarr[i + 2],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200914 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -0800915 (*endnum - (i + 2)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700916 i--;
917 (*endnum)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 }
Marcin Slusarz4b111112008-02-08 04:20:36 -0800919 } else if (((li->extLength >> 30) ==
920 (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
921 ((lip1->extLength >> 30) ==
922 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200923 udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800924 ((li->extLength &
925 UDF_EXTENT_LENGTH_MASK) +
926 blocksize - 1) >> blocksize_bits);
927 li->extLocation.logicalBlockNum = 0;
928 li->extLocation.partitionReferenceNum = 0;
929
930 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
931 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
932 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
933 lip1->extLength = (lip1->extLength -
934 (li->extLength &
935 UDF_EXTENT_LENGTH_MASK) +
936 UDF_EXTENT_LENGTH_MASK) &
937 ~(blocksize - 1);
938 li->extLength = (li->extLength &
939 UDF_EXTENT_FLAG_MASK) +
940 (UDF_EXTENT_LENGTH_MASK + 1) -
941 blocksize;
942 } else {
943 li->extLength = lip1->extLength +
944 (((li->extLength &
945 UDF_EXTENT_LENGTH_MASK) +
946 blocksize - 1) & ~(blocksize - 1));
947 if (*endnum > (i + 2))
948 memmove(&laarr[i + 1], &laarr[i + 2],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200949 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -0800950 (*endnum - (i + 2)));
951 i--;
952 (*endnum)--;
953 }
954 } else if ((li->extLength >> 30) ==
955 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
956 udf_free_blocks(inode->i_sb, inode,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200957 &li->extLocation, 0,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800958 ((li->extLength &
959 UDF_EXTENT_LENGTH_MASK) +
960 blocksize - 1) >> blocksize_bits);
961 li->extLocation.logicalBlockNum = 0;
962 li->extLocation.partitionReferenceNum = 0;
963 li->extLength = (li->extLength &
964 UDF_EXTENT_LENGTH_MASK) |
965 EXT_NOT_RECORDED_NOT_ALLOCATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 }
967 }
968}
969
970static void udf_update_extents(struct inode *inode,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200971 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700972 int startnum, int endnum,
973 struct extent_position *epos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
975 int start = 0, i;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200976 struct kernel_lb_addr tmploc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 uint32_t tmplen;
978
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700979 if (startnum > endnum) {
980 for (i = 0; i < (startnum - endnum); i++)
Jan Karaff116fc2007-05-08 00:35:14 -0700981 udf_delete_aext(inode, *epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700982 laarr[i].extLength);
983 } else if (startnum < endnum) {
984 for (i = 0; i < (endnum - startnum); i++) {
Jan Karaff116fc2007-05-08 00:35:14 -0700985 udf_insert_aext(inode, *epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700986 laarr[i].extLength);
Jan Karaff116fc2007-05-08 00:35:14 -0700987 udf_next_aext(inode, epos, &laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700988 &laarr[i].extLength, 1);
989 start++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
991 }
992
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700993 for (i = start; i < endnum; i++) {
Jan Karaff116fc2007-05-08 00:35:14 -0700994 udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200995 udf_write_aext(inode, epos, &laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700996 laarr[i].extLength, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 }
998}
999
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001000struct buffer_head *udf_bread(struct inode *inode, int block,
1001 int create, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001003 struct buffer_head *bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
1005 bh = udf_getblk(inode, block, create, err);
1006 if (!bh)
1007 return NULL;
1008
1009 if (buffer_uptodate(bh))
1010 return bh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 ll_rw_block(READ, 1, &bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 wait_on_buffer(bh);
1015 if (buffer_uptodate(bh))
1016 return bh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 brelse(bh);
1019 *err = -EIO;
1020 return NULL;
1021}
1022
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001023void udf_truncate(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
1025 int offset;
1026 int err;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001027 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001030 S_ISLNK(inode->i_mode)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 return;
1032 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1033 return;
1034
1035 lock_kernel();
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001036 iinfo = UDF_I(inode);
1037 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001038 if (inode->i_sb->s_blocksize <
1039 (udf_file_entry_alloc_offset(inode) +
1040 inode->i_size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 udf_expand_file_adinicb(inode, inode->i_size, &err);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001042 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1043 inode->i_size = iinfo->i_lenAlloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 unlock_kernel();
1045 return;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001046 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 udf_truncate_extents(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001048 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 offset = inode->i_size & (inode->i_sb->s_blocksize - 1);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001050 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr + offset,
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001051 0x00, inode->i_sb->s_blocksize -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001052 offset - udf_file_entry_alloc_offset(inode));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001053 iinfo->i_lenAlloc = inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001055 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001056 block_truncate_page(inode->i_mapping, inode->i_size,
1057 udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 udf_truncate_extents(inode);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 inode->i_mtime = inode->i_ctime = current_fs_time(inode->i_sb);
1062 if (IS_SYNC(inode))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001063 udf_sync_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 else
1065 mark_inode_dirty(inode);
1066 unlock_kernel();
1067}
1068
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001069static void __udf_read_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
1071 struct buffer_head *bh = NULL;
1072 struct fileEntry *fe;
1073 uint16_t ident;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001074 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 /*
1077 * Set defaults, but the inode is still incomplete!
1078 * Note: get_new_inode() sets the following on a new inode:
1079 * i_sb = sb
1080 * i_no = ino
1081 * i_flags = sb->s_flags
1082 * i_state = 0
1083 * clean_inode(): zero fills and sets
1084 * i_count = 1
1085 * i_nlink = 1
1086 * i_op = NULL;
1087 */
Pekka Enberg97e961f2008-10-15 12:29:03 +02001088 bh = udf_read_ptagged(inode->i_sb, &iinfo->i_location, 0, &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001089 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 printk(KERN_ERR "udf: udf_read_inode(ino %ld) failed !bh\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001091 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 make_bad_inode(inode);
1093 return;
1094 }
1095
1096 if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001097 ident != TAG_IDENT_USE) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001098 printk(KERN_ERR "udf: udf_read_inode(ino %ld) "
1099 "failed ident=%d\n", inode->i_ino, ident);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001100 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 make_bad_inode(inode);
1102 return;
1103 }
1104
1105 fe = (struct fileEntry *)bh->b_data;
1106
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001107 if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001108 struct buffer_head *ibh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Pekka Enberg97e961f2008-10-15 12:29:03 +02001110 ibh = udf_read_ptagged(inode->i_sb, &iinfo->i_location, 1,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001111 &ident);
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001112 if (ident == TAG_IDENT_IE && ibh) {
1113 struct buffer_head *nbh = NULL;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001114 struct kernel_lb_addr loc;
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001115 struct indirectEntry *ie;
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001116
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001117 ie = (struct indirectEntry *)ibh->b_data;
1118 loc = lelb_to_cpu(ie->indirectICB.extLocation);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001119
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001120 if (ie->indirectICB.extLength &&
Pekka Enberg97e961f2008-10-15 12:29:03 +02001121 (nbh = udf_read_ptagged(inode->i_sb, &loc, 0,
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001122 &ident))) {
1123 if (ident == TAG_IDENT_FE ||
1124 ident == TAG_IDENT_EFE) {
1125 memcpy(&iinfo->i_location,
1126 &loc,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001127 sizeof(struct kernel_lb_addr));
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001128 brelse(bh);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001129 brelse(ibh);
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001130 brelse(nbh);
1131 __udf_read_inode(inode);
1132 return;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001133 }
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001134 brelse(nbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001136 }
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001137 brelse(ibh);
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001138 } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 printk(KERN_ERR "udf: unsupported strategy type: %d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001140 le16_to_cpu(fe->icbTag.strategyType));
Jan Kara3bf25cb2007-05-08 00:35:16 -07001141 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 make_bad_inode(inode);
1143 return;
1144 }
1145 udf_fill_inode(inode, bh);
Jan Kara31170b62007-05-08 00:35:21 -07001146
Jan Kara3bf25cb2007-05-08 00:35:16 -07001147 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148}
1149
1150static void udf_fill_inode(struct inode *inode, struct buffer_head *bh)
1151{
1152 struct fileEntry *fe;
1153 struct extendedFileEntry *efe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 int offset;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001155 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001156 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158 fe = (struct fileEntry *)bh->b_data;
1159 efe = (struct extendedFileEntry *)bh->b_data;
1160
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001161 if (fe->icbTag.strategyType == cpu_to_le16(4))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001162 iinfo->i_strat4096 = 0;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001163 else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001164 iinfo->i_strat4096 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001166 iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
Marcin Slusarz4b111112008-02-08 04:20:36 -08001167 ICBTAG_FLAG_AD_MASK;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001168 iinfo->i_unique = 0;
1169 iinfo->i_lenEAttr = 0;
1170 iinfo->i_lenExtents = 0;
1171 iinfo->i_lenAlloc = 0;
1172 iinfo->i_next_alloc_block = 0;
1173 iinfo->i_next_alloc_goal = 0;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001174 if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001175 iinfo->i_efe = 1;
1176 iinfo->i_use = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001177 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1178 sizeof(struct extendedFileEntry))) {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001179 make_bad_inode(inode);
1180 return;
1181 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001182 memcpy(iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001183 bh->b_data + sizeof(struct extendedFileEntry),
1184 inode->i_sb->s_blocksize -
1185 sizeof(struct extendedFileEntry));
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001186 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001187 iinfo->i_efe = 0;
1188 iinfo->i_use = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001189 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1190 sizeof(struct fileEntry))) {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001191 make_bad_inode(inode);
1192 return;
1193 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001194 memcpy(iinfo->i_ext.i_data,
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001195 bh->b_data + sizeof(struct fileEntry),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001196 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001197 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001198 iinfo->i_efe = 0;
1199 iinfo->i_use = 1;
1200 iinfo->i_lenAlloc = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001201 ((struct unallocSpaceEntry *)bh->b_data)->
1202 lengthAllocDescs);
1203 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1204 sizeof(struct unallocSpaceEntry))) {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001205 make_bad_inode(inode);
1206 return;
1207 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001208 memcpy(iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001209 bh->b_data + sizeof(struct unallocSpaceEntry),
1210 inode->i_sb->s_blocksize -
1211 sizeof(struct unallocSpaceEntry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 return;
1213 }
1214
1215 inode->i_uid = le32_to_cpu(fe->uid);
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -07001216 if (inode->i_uid == -1 ||
1217 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_IGNORE) ||
1218 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
Phillip Susi4d6660e2006-03-07 21:55:24 -08001219 inode->i_uid = UDF_SB(inode->i_sb)->s_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 inode->i_gid = le32_to_cpu(fe->gid);
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -07001222 if (inode->i_gid == -1 ||
1223 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_IGNORE) ||
1224 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
Phillip Susi4d6660e2006-03-07 21:55:24 -08001225 inode->i_gid = UDF_SB(inode->i_sb)->s_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 inode->i_nlink = le16_to_cpu(fe->fileLinkCount);
1228 if (!inode->i_nlink)
1229 inode->i_nlink = 1;
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 inode->i_size = le64_to_cpu(fe->informationLength);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001232 iinfo->i_lenExtents = inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01001234 if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
Marcin Slusarz87bc7302008-12-02 13:40:11 +01001235 sbi->s_fmode != UDF_INVALID_MODE)
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01001236 inode->i_mode = sbi->s_fmode;
1237 else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
Marcin Slusarz87bc7302008-12-02 13:40:11 +01001238 sbi->s_dmode != UDF_INVALID_MODE)
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01001239 inode->i_mode = sbi->s_dmode;
1240 else
1241 inode->i_mode = udf_convert_permissions(fe);
1242 inode->i_mode &= ~sbi->s_umask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001244 if (iinfo->i_efe == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001246 (inode->i_sb->s_blocksize_bits - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Marcin Slusarz56774802008-02-10 11:25:31 +01001248 if (!udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001249 inode->i_atime = sbi->s_record_time;
1250
Marcin Slusarz56774802008-02-10 11:25:31 +01001251 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1252 fe->modificationTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001253 inode->i_mtime = sbi->s_record_time;
1254
Marcin Slusarz56774802008-02-10 11:25:31 +01001255 if (!udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001256 inode->i_ctime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001258 iinfo->i_unique = le64_to_cpu(fe->uniqueID);
1259 iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
1260 iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
1261 offset = sizeof(struct fileEntry) + iinfo->i_lenEAttr;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001262 } else {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001263 inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001264 (inode->i_sb->s_blocksize_bits - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Marcin Slusarz56774802008-02-10 11:25:31 +01001266 if (!udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001267 inode->i_atime = sbi->s_record_time;
1268
Marcin Slusarz56774802008-02-10 11:25:31 +01001269 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1270 efe->modificationTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001271 inode->i_mtime = sbi->s_record_time;
1272
Marcin Slusarz56774802008-02-10 11:25:31 +01001273 if (!udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001274 iinfo->i_crtime = sbi->s_record_time;
1275
Marcin Slusarz56774802008-02-10 11:25:31 +01001276 if (!udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001277 inode->i_ctime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001279 iinfo->i_unique = le64_to_cpu(efe->uniqueID);
1280 iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
1281 iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001282 offset = sizeof(struct extendedFileEntry) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001283 iinfo->i_lenEAttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 }
1285
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001286 switch (fe->icbTag.fileType) {
1287 case ICBTAG_FILE_TYPE_DIRECTORY:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001288 inode->i_op = &udf_dir_inode_operations;
1289 inode->i_fop = &udf_dir_operations;
1290 inode->i_mode |= S_IFDIR;
1291 inc_nlink(inode);
1292 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001293 case ICBTAG_FILE_TYPE_REALTIME:
1294 case ICBTAG_FILE_TYPE_REGULAR:
1295 case ICBTAG_FILE_TYPE_UNDEF:
Jan Kara742e1792008-04-08 01:17:52 +02001296 case ICBTAG_FILE_TYPE_VAT20:
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001297 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001298 inode->i_data.a_ops = &udf_adinicb_aops;
1299 else
1300 inode->i_data.a_ops = &udf_aops;
1301 inode->i_op = &udf_file_inode_operations;
1302 inode->i_fop = &udf_file_operations;
1303 inode->i_mode |= S_IFREG;
1304 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001305 case ICBTAG_FILE_TYPE_BLOCK:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001306 inode->i_mode |= S_IFBLK;
1307 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001308 case ICBTAG_FILE_TYPE_CHAR:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001309 inode->i_mode |= S_IFCHR;
1310 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001311 case ICBTAG_FILE_TYPE_FIFO:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001312 init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1313 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001314 case ICBTAG_FILE_TYPE_SOCKET:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001315 init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1316 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001317 case ICBTAG_FILE_TYPE_SYMLINK:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001318 inode->i_data.a_ops = &udf_symlink_aops;
Dmitry Monakhovc15d0fc2010-03-29 11:05:21 +04001319 inode->i_op = &udf_symlink_inode_operations;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001320 inode->i_mode = S_IFLNK | S_IRWXUGO;
1321 break;
Jan Karabfb257a2008-04-08 20:37:21 +02001322 case ICBTAG_FILE_TYPE_MAIN:
1323 udf_debug("METADATA FILE-----\n");
1324 break;
1325 case ICBTAG_FILE_TYPE_MIRROR:
1326 udf_debug("METADATA MIRROR FILE-----\n");
1327 break;
1328 case ICBTAG_FILE_TYPE_BITMAP:
1329 udf_debug("METADATA BITMAP FILE-----\n");
1330 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001331 default:
Marcin Slusarz4b111112008-02-08 04:20:36 -08001332 printk(KERN_ERR "udf: udf_fill_inode(ino %ld) failed unknown "
1333 "file type=%d\n", inode->i_ino,
1334 fe->icbTag.fileType);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001335 make_bad_inode(inode);
1336 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001338 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001339 struct deviceSpec *dsea =
1340 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001341 if (dsea) {
1342 init_special_inode(inode, inode->i_mode,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001343 MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
1344 le32_to_cpu(dsea->minorDeviceIdent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 /* Developer ID ??? */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001346 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 make_bad_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 }
1349}
1350
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001351static int udf_alloc_i_data(struct inode *inode, size_t size)
1352{
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001353 struct udf_inode_info *iinfo = UDF_I(inode);
1354 iinfo->i_ext.i_data = kmalloc(size, GFP_KERNEL);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001355
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001356 if (!iinfo->i_ext.i_data) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001357 printk(KERN_ERR "udf:udf_alloc_i_data (ino %ld) "
1358 "no free memory\n", inode->i_ino);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001359 return -ENOMEM;
1360 }
1361
1362 return 0;
1363}
1364
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001365static mode_t udf_convert_permissions(struct fileEntry *fe)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366{
1367 mode_t mode;
1368 uint32_t permissions;
1369 uint32_t flags;
1370
1371 permissions = le32_to_cpu(fe->permissions);
1372 flags = le16_to_cpu(fe->icbTag.flags);
1373
Marcin Slusarz4b111112008-02-08 04:20:36 -08001374 mode = ((permissions) & S_IRWXO) |
1375 ((permissions >> 2) & S_IRWXG) |
1376 ((permissions >> 4) & S_IRWXU) |
1377 ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
1378 ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
1379 ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
1381 return mode;
1382}
1383
Christoph Hellwiga9185b42010-03-05 09:21:37 +01001384int udf_write_inode(struct inode *inode, struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
1386 int ret;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 lock_kernel();
Christoph Hellwiga9185b42010-03-05 09:21:37 +01001389 ret = udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 unlock_kernel();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 return ret;
1393}
1394
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001395int udf_sync_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396{
1397 return udf_update_inode(inode, 1);
1398}
1399
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001400static int udf_update_inode(struct inode *inode, int do_sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
1402 struct buffer_head *bh = NULL;
1403 struct fileEntry *fe;
1404 struct extendedFileEntry *efe;
1405 uint32_t udfperms;
1406 uint16_t icbflags;
1407 uint16_t crclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 int err = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001409 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001410 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001411 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Jan Kara5833ded2010-01-08 16:52:59 +01001413 bh = udf_tgetblk(inode->i_sb,
1414 udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001415 if (!bh) {
Jan Karaaae917c2010-01-08 16:46:29 +01001416 udf_debug("getblk failure\n");
1417 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 }
1419
Jan Karaaae917c2010-01-08 16:46:29 +01001420 lock_buffer(bh);
1421 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 fe = (struct fileEntry *)bh->b_data;
1423 efe = (struct extendedFileEntry *)bh->b_data;
1424
Jan Karaaae917c2010-01-08 16:46:29 +01001425 if (iinfo->i_use) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 struct unallocSpaceEntry *use =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001427 (struct unallocSpaceEntry *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001429 use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001430 memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001431 iinfo->i_ext.i_data, inode->i_sb->s_blocksize -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001432 sizeof(struct unallocSpaceEntry));
Jan Karaaae917c2010-01-08 16:46:29 +01001433 use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
1434 use->descTag.tagLocation =
1435 cpu_to_le32(iinfo->i_location.logicalBlockNum);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001436 crclen = sizeof(struct unallocSpaceEntry) +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001437 iinfo->i_lenAlloc - sizeof(struct tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 use->descTag.descCRCLength = cpu_to_le16(crclen);
Bob Copelandf845fce2008-04-17 09:47:48 +02001439 use->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)use +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001440 sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02001441 crclen));
Marcin Slusarz3f2587b2008-02-08 04:20:39 -08001442 use->descTag.tagChecksum = udf_tag_checksum(&use->descTag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Jan Karaaae917c2010-01-08 16:46:29 +01001444 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 }
1446
Phillip Susi4d6660e2006-03-07 21:55:24 -08001447 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
1448 fe->uid = cpu_to_le32(-1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001449 else
1450 fe->uid = cpu_to_le32(inode->i_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Phillip Susi4d6660e2006-03-07 21:55:24 -08001452 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
1453 fe->gid = cpu_to_le32(-1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001454 else
1455 fe->gid = cpu_to_le32(inode->i_gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Marcin Slusarz4b111112008-02-08 04:20:36 -08001457 udfperms = ((inode->i_mode & S_IRWXO)) |
1458 ((inode->i_mode & S_IRWXG) << 2) |
1459 ((inode->i_mode & S_IRWXU) << 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Marcin Slusarz4b111112008-02-08 04:20:36 -08001461 udfperms |= (le32_to_cpu(fe->permissions) &
1462 (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
1463 FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
1464 FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 fe->permissions = cpu_to_le32(udfperms);
1466
1467 if (S_ISDIR(inode->i_mode))
1468 fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1469 else
1470 fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1471
1472 fe->informationLength = cpu_to_le64(inode->i_size);
1473
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001474 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001475 struct regid *eid;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001476 struct deviceSpec *dsea =
1477 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001478 if (!dsea) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 dsea = (struct deviceSpec *)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001480 udf_add_extendedattr(inode,
1481 sizeof(struct deviceSpec) +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001482 sizeof(struct regid), 12, 0x3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 dsea->attrType = cpu_to_le32(12);
1484 dsea->attrSubtype = 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001485 dsea->attrLength = cpu_to_le32(
1486 sizeof(struct deviceSpec) +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001487 sizeof(struct regid));
1488 dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 }
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001490 eid = (struct regid *)dsea->impUse;
1491 memset(eid, 0, sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 strcpy(eid->ident, UDF_ID_DEVELOPER);
1493 eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1494 eid->identSuffix[1] = UDF_OS_ID_LINUX;
1495 dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1496 dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1497 }
1498
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001499 if (iinfo->i_efe == 0) {
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001500 memcpy(bh->b_data + sizeof(struct fileEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001501 iinfo->i_ext.i_data,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001502 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001503 fe->logicalBlocksRecorded = cpu_to_le64(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001504 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1505 (blocksize_bits - 9));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Marcin Slusarz56774802008-02-10 11:25:31 +01001507 udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
1508 udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
1509 udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001510 memset(&(fe->impIdent), 0, sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1512 fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1513 fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001514 fe->uniqueID = cpu_to_le64(iinfo->i_unique);
1515 fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1516 fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1518 crclen = sizeof(struct fileEntry);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001519 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001520 memcpy(bh->b_data + sizeof(struct extendedFileEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001521 iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001522 inode->i_sb->s_blocksize -
1523 sizeof(struct extendedFileEntry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 efe->objectSize = cpu_to_le64(inode->i_size);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001525 efe->logicalBlocksRecorded = cpu_to_le64(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001526 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1527 (blocksize_bits - 9));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001529 if (iinfo->i_crtime.tv_sec > inode->i_atime.tv_sec ||
1530 (iinfo->i_crtime.tv_sec == inode->i_atime.tv_sec &&
1531 iinfo->i_crtime.tv_nsec > inode->i_atime.tv_nsec))
1532 iinfo->i_crtime = inode->i_atime;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001533
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001534 if (iinfo->i_crtime.tv_sec > inode->i_mtime.tv_sec ||
1535 (iinfo->i_crtime.tv_sec == inode->i_mtime.tv_sec &&
1536 iinfo->i_crtime.tv_nsec > inode->i_mtime.tv_nsec))
1537 iinfo->i_crtime = inode->i_mtime;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001538
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001539 if (iinfo->i_crtime.tv_sec > inode->i_ctime.tv_sec ||
1540 (iinfo->i_crtime.tv_sec == inode->i_ctime.tv_sec &&
1541 iinfo->i_crtime.tv_nsec > inode->i_ctime.tv_nsec))
1542 iinfo->i_crtime = inode->i_ctime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Marcin Slusarz56774802008-02-10 11:25:31 +01001544 udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
1545 udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
1546 udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
1547 udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001549 memset(&(efe->impIdent), 0, sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1551 efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1552 efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001553 efe->uniqueID = cpu_to_le64(iinfo->i_unique);
1554 efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1555 efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1557 crclen = sizeof(struct extendedFileEntry);
1558 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001559 if (iinfo->i_strat4096) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 fe->icbTag.strategyType = cpu_to_le16(4096);
1561 fe->icbTag.strategyParameter = cpu_to_le16(1);
1562 fe->icbTag.numEntries = cpu_to_le16(2);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001563 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 fe->icbTag.strategyType = cpu_to_le16(4);
1565 fe->icbTag.numEntries = cpu_to_le16(1);
1566 }
1567
1568 if (S_ISDIR(inode->i_mode))
1569 fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
1570 else if (S_ISREG(inode->i_mode))
1571 fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
1572 else if (S_ISLNK(inode->i_mode))
1573 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
1574 else if (S_ISBLK(inode->i_mode))
1575 fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
1576 else if (S_ISCHR(inode->i_mode))
1577 fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
1578 else if (S_ISFIFO(inode->i_mode))
1579 fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
1580 else if (S_ISSOCK(inode->i_mode))
1581 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
1582
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001583 icbflags = iinfo->i_alloc_type |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001584 ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
1585 ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
1586 ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
1587 (le16_to_cpu(fe->icbTag.flags) &
1588 ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
1589 ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
1591 fe->icbTag.flags = cpu_to_le16(icbflags);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001592 if (sbi->s_udfrev >= 0x0200)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 fe->descTag.descVersion = cpu_to_le16(3);
1594 else
1595 fe->descTag.descVersion = cpu_to_le16(2);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001596 fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001597 fe->descTag.tagLocation = cpu_to_le32(
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001598 iinfo->i_location.logicalBlockNum);
Jan Karaaae917c2010-01-08 16:46:29 +01001599 crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 fe->descTag.descCRCLength = cpu_to_le16(crclen);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001601 fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02001602 crclen));
Marcin Slusarz3f2587b2008-02-08 04:20:39 -08001603 fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Jan Karaaae917c2010-01-08 16:46:29 +01001605out:
Jan Kara5833ded2010-01-08 16:52:59 +01001606 set_buffer_uptodate(bh);
Jan Karaaae917c2010-01-08 16:46:29 +01001607 unlock_buffer(bh);
1608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 /* write the data blocks */
1610 mark_buffer_dirty(bh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001611 if (do_sync) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 sync_dirty_buffer(bh);
Jan Karaaae917c2010-01-08 16:46:29 +01001613 if (buffer_write_io_error(bh)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001614 printk(KERN_WARNING "IO error syncing udf inode "
1615 "[%s:%08lx]\n", inode->i_sb->s_id,
1616 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 err = -EIO;
1618 }
1619 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001620 brelse(bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 return err;
1623}
1624
Pekka Enberg97e961f2008-10-15 12:29:03 +02001625struct inode *udf_iget(struct super_block *sb, struct kernel_lb_addr *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626{
1627 unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1628 struct inode *inode = iget_locked(sb, block);
1629
1630 if (!inode)
1631 return NULL;
1632
1633 if (inode->i_state & I_NEW) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02001634 memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 __udf_read_inode(inode);
1636 unlock_new_inode(inode);
1637 }
1638
1639 if (is_bad_inode(inode))
1640 goto out_iput;
1641
Pekka Enberg97e961f2008-10-15 12:29:03 +02001642 if (ino->logicalBlockNum >= UDF_SB(sb)->
1643 s_partmaps[ino->partitionReferenceNum].s_partition_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 udf_debug("block=%d, partition=%d out of range\n",
Pekka Enberg97e961f2008-10-15 12:29:03 +02001645 ino->logicalBlockNum, ino->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 make_bad_inode(inode);
1647 goto out_iput;
1648 }
1649
1650 return inode;
1651
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001652 out_iput:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 iput(inode);
1654 return NULL;
1655}
1656
Marcin Slusarz4b111112008-02-08 04:20:36 -08001657int8_t udf_add_aext(struct inode *inode, struct extent_position *epos,
Pekka Enberg97e961f2008-10-15 12:29:03 +02001658 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659{
1660 int adsize;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001661 struct short_ad *sad = NULL;
1662 struct long_ad *lad = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 struct allocExtDesc *aed;
1664 int8_t etype;
1665 uint8_t *ptr;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001666 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Jan Karaff116fc2007-05-08 00:35:14 -07001668 if (!epos->bh)
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001669 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001670 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001671 iinfo->i_lenEAttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 else
Jan Karaff116fc2007-05-08 00:35:14 -07001673 ptr = epos->bh->b_data + epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001675 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001676 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001677 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001678 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 else
1680 return -1;
1681
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001682 if (epos->offset + (2 * adsize) > inode->i_sb->s_blocksize) {
Al Viro391e8bb2010-01-31 21:28:48 -05001683 unsigned char *sptr, *dptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 struct buffer_head *nbh;
1685 int err, loffset;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001686 struct kernel_lb_addr obloc = epos->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Marcin Slusarz4b111112008-02-08 04:20:36 -08001688 epos->block.logicalBlockNum = udf_new_block(inode->i_sb, NULL,
1689 obloc.partitionReferenceNum,
1690 obloc.logicalBlockNum, &err);
1691 if (!epos->block.logicalBlockNum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 return -1;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001693 nbh = udf_tgetblk(inode->i_sb, udf_get_lb_pblock(inode->i_sb,
Pekka Enberg97e961f2008-10-15 12:29:03 +02001694 &epos->block,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001695 0));
1696 if (!nbh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 lock_buffer(nbh);
1699 memset(nbh->b_data, 0x00, inode->i_sb->s_blocksize);
1700 set_buffer_uptodate(nbh);
1701 unlock_buffer(nbh);
1702 mark_buffer_dirty_inode(nbh, inode);
1703
1704 aed = (struct allocExtDesc *)(nbh->b_data);
1705 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001706 aed->previousAllocExtLocation =
1707 cpu_to_le32(obloc.logicalBlockNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001708 if (epos->offset + adsize > inode->i_sb->s_blocksize) {
Jan Karaff116fc2007-05-08 00:35:14 -07001709 loffset = epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 aed->lengthAllocDescs = cpu_to_le32(adsize);
1711 sptr = ptr - adsize;
1712 dptr = nbh->b_data + sizeof(struct allocExtDesc);
1713 memcpy(dptr, sptr, adsize);
Jan Karaff116fc2007-05-08 00:35:14 -07001714 epos->offset = sizeof(struct allocExtDesc) + adsize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001715 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001716 loffset = epos->offset + adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 aed->lengthAllocDescs = cpu_to_le32(0);
1718 sptr = ptr;
Jan Karaff116fc2007-05-08 00:35:14 -07001719 epos->offset = sizeof(struct allocExtDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001721 if (epos->bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07001722 aed = (struct allocExtDesc *)epos->bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01001723 le32_add_cpu(&aed->lengthAllocDescs, adsize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001724 } else {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001725 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 mark_inode_dirty(inode);
1727 }
1728 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001729 if (UDF_SB(inode->i_sb)->s_udfrev >= 0x0200)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 3, 1,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001731 epos->block.logicalBlockNum, sizeof(struct tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 else
1733 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 2, 1,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001734 epos->block.logicalBlockNum, sizeof(struct tag));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001735 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001736 case ICBTAG_FLAG_AD_SHORT:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001737 sad = (struct short_ad *)sptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001738 sad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1739 inode->i_sb->s_blocksize);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001740 sad->extPosition =
1741 cpu_to_le32(epos->block.logicalBlockNum);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001742 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001743 case ICBTAG_FLAG_AD_LONG:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001744 lad = (struct long_ad *)sptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001745 lad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1746 inode->i_sb->s_blocksize);
1747 lad->extLocation = cpu_to_lelb(epos->block);
1748 memset(lad->impUse, 0x00, sizeof(lad->impUse));
1749 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001751 if (epos->bh) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001752 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001753 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Jan Karaff116fc2007-05-08 00:35:14 -07001754 udf_update_tag(epos->bh->b_data, loffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08001756 udf_update_tag(epos->bh->b_data,
1757 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07001758 mark_buffer_dirty_inode(epos->bh, inode);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001759 brelse(epos->bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001760 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001762 }
Jan Karaff116fc2007-05-08 00:35:14 -07001763 epos->bh = nbh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 }
1765
Jan Karaff116fc2007-05-08 00:35:14 -07001766 etype = udf_write_aext(inode, epos, eloc, elen, inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001768 if (!epos->bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001769 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001771 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001772 aed = (struct allocExtDesc *)epos->bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01001773 le32_add_cpu(&aed->lengthAllocDescs, adsize);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001774 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1775 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
1776 udf_update_tag(epos->bh->b_data,
1777 epos->offset + (inc ? 0 : adsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08001779 udf_update_tag(epos->bh->b_data,
1780 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07001781 mark_buffer_dirty_inode(epos->bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 }
1783
1784 return etype;
1785}
1786
Marcin Slusarz4b111112008-02-08 04:20:36 -08001787int8_t udf_write_aext(struct inode *inode, struct extent_position *epos,
Pekka Enberg97e961f2008-10-15 12:29:03 +02001788 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789{
1790 int adsize;
1791 uint8_t *ptr;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001792 struct short_ad *sad;
1793 struct long_ad *lad;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001794 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
Jan Karaff116fc2007-05-08 00:35:14 -07001796 if (!epos->bh)
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001797 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001798 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001799 iinfo->i_lenEAttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 else
Jan Karaff116fc2007-05-08 00:35:14 -07001801 ptr = epos->bh->b_data + epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001803 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001804 case ICBTAG_FLAG_AD_SHORT:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001805 sad = (struct short_ad *)ptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001806 sad->extLength = cpu_to_le32(elen);
Pekka Enberg97e961f2008-10-15 12:29:03 +02001807 sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001808 adsize = sizeof(struct short_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001809 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001810 case ICBTAG_FLAG_AD_LONG:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001811 lad = (struct long_ad *)ptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001812 lad->extLength = cpu_to_le32(elen);
Pekka Enberg97e961f2008-10-15 12:29:03 +02001813 lad->extLocation = cpu_to_lelb(*eloc);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001814 memset(lad->impUse, 0x00, sizeof(lad->impUse));
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001815 adsize = sizeof(struct long_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001816 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001817 default:
1818 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 }
1820
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001821 if (epos->bh) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001822 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001823 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001824 struct allocExtDesc *aed =
1825 (struct allocExtDesc *)epos->bh->b_data;
Jan Karaff116fc2007-05-08 00:35:14 -07001826 udf_update_tag(epos->bh->b_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001827 le32_to_cpu(aed->lengthAllocDescs) +
1828 sizeof(struct allocExtDesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 }
Jan Karaff116fc2007-05-08 00:35:14 -07001830 mark_buffer_dirty_inode(epos->bh, inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001831 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
1835 if (inc)
Jan Karaff116fc2007-05-08 00:35:14 -07001836 epos->offset += adsize;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 return (elen >> 30);
1839}
1840
Marcin Slusarz4b111112008-02-08 04:20:36 -08001841int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001842 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843{
1844 int8_t etype;
1845
Jan Karaff116fc2007-05-08 00:35:14 -07001846 while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001847 (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001848 int block;
Jan Karaff116fc2007-05-08 00:35:14 -07001849 epos->block = *eloc;
1850 epos->offset = sizeof(struct allocExtDesc);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001851 brelse(epos->bh);
Pekka Enberg97e961f2008-10-15 12:29:03 +02001852 block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001853 epos->bh = udf_tread(inode->i_sb, block);
1854 if (!epos->bh) {
1855 udf_debug("reading block %d failed!\n", block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 return -1;
1857 }
1858 }
1859
1860 return etype;
1861}
1862
Marcin Slusarz4b111112008-02-08 04:20:36 -08001863int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001864 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865{
1866 int alen;
1867 int8_t etype;
1868 uint8_t *ptr;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001869 struct short_ad *sad;
1870 struct long_ad *lad;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001871 struct udf_inode_info *iinfo = UDF_I(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001872
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001873 if (!epos->bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07001874 if (!epos->offset)
1875 epos->offset = udf_file_entry_alloc_offset(inode);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001876 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001877 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001878 iinfo->i_lenEAttr;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001879 alen = udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001880 iinfo->i_lenAlloc;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001881 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001882 if (!epos->offset)
1883 epos->offset = sizeof(struct allocExtDesc);
1884 ptr = epos->bh->b_data + epos->offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001885 alen = sizeof(struct allocExtDesc) +
Marcin Slusarz4b111112008-02-08 04:20:36 -08001886 le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
1887 lengthAllocDescs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 }
1889
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001890 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001891 case ICBTAG_FLAG_AD_SHORT:
Marcin Slusarz4b111112008-02-08 04:20:36 -08001892 sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
1893 if (!sad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 return -1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001895 etype = le32_to_cpu(sad->extLength) >> 30;
1896 eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001897 eloc->partitionReferenceNum =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001898 iinfo->i_location.partitionReferenceNum;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001899 *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
1900 break;
1901 case ICBTAG_FLAG_AD_LONG:
Marcin Slusarz4b111112008-02-08 04:20:36 -08001902 lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
1903 if (!lad)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001904 return -1;
1905 etype = le32_to_cpu(lad->extLength) >> 30;
1906 *eloc = lelb_to_cpu(lad->extLocation);
1907 *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
1908 break;
1909 default:
Marcin Slusarz4b111112008-02-08 04:20:36 -08001910 udf_debug("alloc_type = %d unsupported\n",
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001911 iinfo->i_alloc_type);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001912 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 }
1914
1915 return etype;
1916}
1917
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001918static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001919 struct kernel_lb_addr neloc, uint32_t nelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001921 struct kernel_lb_addr oeloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 uint32_t oelen;
1923 int8_t etype;
1924
Jan Karaff116fc2007-05-08 00:35:14 -07001925 if (epos.bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001926 get_bh(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001928 while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02001929 udf_write_aext(inode, &epos, &neloc, nelen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 neloc = oeloc;
1931 nelen = (etype << 30) | oelen;
1932 }
Pekka Enberg97e961f2008-10-15 12:29:03 +02001933 udf_add_aext(inode, &epos, &neloc, nelen, 1);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001934 brelse(epos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001935
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 return (nelen >> 30);
1937}
1938
Marcin Slusarz4b111112008-02-08 04:20:36 -08001939int8_t udf_delete_aext(struct inode *inode, struct extent_position epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001940 struct kernel_lb_addr eloc, uint32_t elen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941{
Jan Karaff116fc2007-05-08 00:35:14 -07001942 struct extent_position oepos;
1943 int adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 int8_t etype;
1945 struct allocExtDesc *aed;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001946 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001948 if (epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07001949 get_bh(epos.bh);
1950 get_bh(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 }
1952
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001953 iinfo = UDF_I(inode);
1954 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001955 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001956 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001957 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 else
1959 adsize = 0;
1960
Jan Karaff116fc2007-05-08 00:35:14 -07001961 oepos = epos;
1962 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 return -1;
1964
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001965 while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02001966 udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001967 if (oepos.bh != epos.bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07001968 oepos.block = epos.block;
Jan Kara3bf25cb2007-05-08 00:35:16 -07001969 brelse(oepos.bh);
1970 get_bh(epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -07001971 oepos.bh = epos.bh;
1972 oepos.offset = epos.offset - adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 }
1974 }
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001975 memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 elen = 0;
1977
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001978 if (epos.bh != oepos.bh) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02001979 udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
1980 udf_write_aext(inode, &oepos, &eloc, elen, 1);
1981 udf_write_aext(inode, &oepos, &eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001982 if (!oepos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001983 iinfo->i_lenAlloc -= (adsize * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001985 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001986 aed = (struct allocExtDesc *)oepos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01001987 le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001988 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001989 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Marcin Slusarz4b111112008-02-08 04:20:36 -08001990 udf_update_tag(oepos.bh->b_data,
1991 oepos.offset - (2 * adsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08001993 udf_update_tag(oepos.bh->b_data,
1994 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07001995 mark_buffer_dirty_inode(oepos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001997 } else {
Pekka Enberg97e961f2008-10-15 12:29:03 +02001998 udf_write_aext(inode, &oepos, &eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001999 if (!oepos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002000 iinfo->i_lenAlloc -= adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002002 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002003 aed = (struct allocExtDesc *)oepos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01002004 le32_add_cpu(&aed->lengthAllocDescs, -adsize);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002005 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002006 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Marcin Slusarz4b111112008-02-08 04:20:36 -08002007 udf_update_tag(oepos.bh->b_data,
2008 epos.offset - adsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08002010 udf_update_tag(oepos.bh->b_data,
2011 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07002012 mark_buffer_dirty_inode(oepos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 }
2014 }
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07002015
Jan Kara3bf25cb2007-05-08 00:35:16 -07002016 brelse(epos.bh);
2017 brelse(oepos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002018
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 return (elen >> 30);
2020}
2021
Marcin Slusarz4b111112008-02-08 04:20:36 -08002022int8_t inode_bmap(struct inode *inode, sector_t block,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002023 struct extent_position *pos, struct kernel_lb_addr *eloc,
Marcin Slusarz4b111112008-02-08 04:20:36 -08002024 uint32_t *elen, sector_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025{
Marcin Slusarz4b111112008-02-08 04:20:36 -08002026 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002027 loff_t lbcount = 0, bcount =
Marcin Slusarz4b111112008-02-08 04:20:36 -08002028 (loff_t) block << blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002030 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002032 iinfo = UDF_I(inode);
Jan Karaff116fc2007-05-08 00:35:14 -07002033 pos->offset = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002034 pos->block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -07002035 pos->bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 *elen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002038 do {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002039 etype = udf_next_aext(inode, pos, eloc, elen, 1);
2040 if (etype == -1) {
2041 *offset = (bcount - lbcount) >> blocksize_bits;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002042 iinfo->i_lenExtents = lbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 return -1;
2044 }
2045 lbcount += *elen;
2046 } while (lbcount <= bcount);
2047
Marcin Slusarz4b111112008-02-08 04:20:36 -08002048 *offset = (bcount + *elen - lbcount) >> blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
2050 return etype;
2051}
2052
Jan Kara60448b12007-05-08 00:35:13 -07002053long udf_block_map(struct inode *inode, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002055 struct kernel_lb_addr eloc;
Jan Karaff116fc2007-05-08 00:35:14 -07002056 uint32_t elen;
Jan Kara60448b12007-05-08 00:35:13 -07002057 sector_t offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002058 struct extent_position epos = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 int ret;
2060
2061 lock_kernel();
2062
Marcin Slusarz4b111112008-02-08 04:20:36 -08002063 if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
2064 (EXT_RECORDED_ALLOCATED >> 30))
Pekka Enberg97e961f2008-10-15 12:29:03 +02002065 ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 else
2067 ret = 0;
2068
2069 unlock_kernel();
Jan Kara3bf25cb2007-05-08 00:35:16 -07002070 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
2072 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
2073 return udf_fixed_to_variable(ret);
2074 else
2075 return ret;
2076}