blob: f94d6f9febf51accc3346b54859c4d4f62aadea7 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/module.h>
35#include <linux/pagemap.h>
36#include <linux/buffer_head.h>
37#include <linux/writeback.h>
38#include <linux/slab.h>
Bob Copelandf845fce2008-04-17 09:47:48 +020039#include <linux/crc-itu-t.h>
Namjae Jeonbc112322011-10-03 14:02:59 +090040#include <linux/mpage.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 *);
Jan Kara49521de2010-10-20 17:42:44 +020054static int udf_sync_inode(struct inode *inode);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -070055static int udf_alloc_i_data(struct inode *inode, size_t size);
Jan Kara60448b12007-05-08 00:35:13 -070056static struct buffer_head *inode_getblk(struct inode *, sector_t, int *,
Marcin Slusarz1ed16172008-02-08 04:20:48 -080057 sector_t *, int *);
Jan Karaff116fc2007-05-08 00:35:14 -070058static int8_t udf_insert_aext(struct inode *, struct extent_position,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020059 struct kernel_lb_addr, uint32_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static void udf_split_extents(struct inode *, int *, int, int,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020061 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static void udf_prealloc_extents(struct inode *, int, int,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020063 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static void udf_merge_extents(struct inode *,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020065 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static void udf_update_extents(struct inode *,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020067 struct kernel_long_ad[EXTENT_MERGE_SIZE], int, int,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070068 struct extent_position *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static int udf_get_block(struct inode *, sector_t, struct buffer_head *, int);
70
Christoph Hellwigb1e32122008-02-22 12:38:48 +010071
Al Viro3aac2b62010-06-07 00:43:39 -040072void udf_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Jan Kara2c948b32009-12-03 13:39:28 +010074 struct udf_inode_info *iinfo = UDF_I(inode);
Al Viro3aac2b62010-06-07 00:43:39 -040075 int want_delete = 0;
Jan Kara2c948b32009-12-03 13:39:28 +010076
Al Viro3aac2b62010-06-07 00:43:39 -040077 if (!inode->i_nlink && !is_bad_inode(inode)) {
78 want_delete = 1;
Jan Kara7e49b6f2010-10-22 00:30:26 +020079 udf_setsize(inode, 0);
Al Viro3aac2b62010-06-07 00:43:39 -040080 udf_update_inode(inode, IS_SYNC(inode));
Jan Kara7e49b6f2010-10-22 00:30:26 +020081 } else
82 truncate_inode_pages(&inode->i_data, 0);
Al Viro3aac2b62010-06-07 00:43:39 -040083 invalidate_inode_buffers(inode);
84 end_writeback(inode);
Jan Kara2c948b32009-12-03 13:39:28 +010085 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB &&
86 inode->i_size != iinfo->i_lenExtents) {
87 printk(KERN_WARNING "UDF-fs (%s): Inode %lu (mode %o) has "
Daniel Mack1537a362010-01-29 15:57:49 +080088 "inode size %llu different from extent length %llu. "
Jan Kara2c948b32009-12-03 13:39:28 +010089 "Filesystem need not be standards compliant.\n",
90 inode->i_sb->s_id, inode->i_ino, inode->i_mode,
91 (unsigned long long)inode->i_size,
92 (unsigned long long)iinfo->i_lenExtents);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -080094 kfree(iinfo->i_ext.i_data);
95 iinfo->i_ext.i_data = NULL;
Al Viro3aac2b62010-06-07 00:43:39 -040096 if (want_delete) {
Al Viro3aac2b62010-06-07 00:43:39 -040097 udf_free_inode(inode);
Al Viro3aac2b62010-06-07 00:43:39 -040098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
101static int udf_writepage(struct page *page, struct writeback_control *wbc)
102{
103 return block_write_full_page(page, udf_get_block, wbc);
104}
105
106static int udf_readpage(struct file *file, struct page *page)
107{
Namjae Jeonbc112322011-10-03 14:02:59 +0900108 return mpage_readpage(page, udf_get_block);
109}
110
111static int udf_readpages(struct file *file, struct address_space *mapping,
112 struct list_head *pages, unsigned nr_pages)
113{
114 return mpage_readpages(mapping, pages, nr_pages, udf_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
Nick Pigginbe021ee2007-10-16 01:25:20 -0700117static int udf_write_begin(struct file *file, struct address_space *mapping,
118 loff_t pos, unsigned len, unsigned flags,
119 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Christoph Hellwig155130a2010-06-04 11:29:58 +0200121 int ret;
122
123 ret = block_write_begin(mapping, pos, len, flags, pagep, udf_get_block);
124 if (unlikely(ret)) {
Jan Kara7e49b6f2010-10-22 00:30:26 +0200125 struct inode *inode = mapping->host;
126 struct udf_inode_info *iinfo = UDF_I(inode);
127 loff_t isize = inode->i_size;
128
129 if (pos + len > isize) {
130 truncate_pagecache(inode, pos + len, isize);
131 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
132 down_write(&iinfo->i_data_sem);
133 udf_truncate_extents(inode);
134 up_write(&iinfo->i_data_sem);
135 }
136 }
Christoph Hellwig155130a2010-06-04 11:29:58 +0200137 }
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,
Namjae Jeonbc112322011-10-03 14:02:59 +0900149 .readpages = udf_readpages,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700150 .writepage = udf_writepage,
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
Jan Kara7e49b6f2010-10-22 00:30:26 +0200156int udf_expand_file_adinicb(struct inode *inode)
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);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200161 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 struct writeback_control udf_wbc = {
163 .sync_mode = WB_SYNC_NONE,
164 .nr_to_write = 1,
165 };
166
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800167 if (!iinfo->i_lenAlloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800169 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 else
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800171 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200172 /* from now on we have normal address_space methods */
173 inode->i_data.a_ops = &udf_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 mark_inode_dirty(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200175 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
177
Jan Kara7e49b6f2010-10-22 00:30:26 +0200178 page = find_or_create_page(inode->i_mapping, 0, GFP_NOFS);
179 if (!page)
180 return -ENOMEM;
Matt Mackallcd7619d2005-05-01 08:59:01 -0700181
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700182 if (!PageUptodate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 kaddr = kmap(page);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800184 memset(kaddr + iinfo->i_lenAlloc, 0x00,
185 PAGE_CACHE_SIZE - iinfo->i_lenAlloc);
186 memcpy(kaddr, iinfo->i_ext.i_data + iinfo->i_lenEAttr,
187 iinfo->i_lenAlloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 flush_dcache_page(page);
189 SetPageUptodate(page);
190 kunmap(page);
191 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800192 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0x00,
193 iinfo->i_lenAlloc);
194 iinfo->i_lenAlloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800196 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 else
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800198 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200199 /* from now on we have normal address_space methods */
200 inode->i_data.a_ops = &udf_aops;
201 err = inode->i_data.a_ops->writepage(page, &udf_wbc);
202 if (err) {
203 /* Restore everything back so that we don't lose data... */
204 lock_page(page);
205 kaddr = kmap(page);
206 memcpy(iinfo->i_ext.i_data + iinfo->i_lenEAttr, kaddr,
207 inode->i_size);
208 kunmap(page);
209 unlock_page(page);
210 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
211 inode->i_data.a_ops = &udf_adinicb_aops;
212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 mark_inode_dirty(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200215
216 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700219struct buffer_head *udf_expand_dir_adinicb(struct inode *inode, int *block,
220 int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
222 int newblock;
Jan Karaff116fc2007-05-08 00:35:14 -0700223 struct buffer_head *dbh = NULL;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200224 struct kernel_lb_addr eloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 uint8_t alloctype;
Jan Karaff116fc2007-05-08 00:35:14 -0700226 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 struct udf_fileident_bh sfibh, dfibh;
Jan Karaaf793292008-02-08 04:20:50 -0800229 loff_t f_pos = udf_ext0_offset(inode);
230 int size = udf_ext0_offset(inode) + inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 struct fileIdentDesc cfi, *sfi, *dfi;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800232 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
235 alloctype = ICBTAG_FLAG_AD_SHORT;
236 else
237 alloctype = ICBTAG_FLAG_AD_LONG;
238
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700239 if (!inode->i_size) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800240 iinfo->i_alloc_type = alloctype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 mark_inode_dirty(inode);
242 return NULL;
243 }
244
245 /* alloc block, and copy data to it */
246 *block = udf_new_block(inode->i_sb, inode,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800247 iinfo->i_location.partitionReferenceNum,
248 iinfo->i_location.logicalBlockNum, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 if (!(*block))
250 return NULL;
251 newblock = udf_get_pblock(inode->i_sb, *block,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800252 iinfo->i_location.partitionReferenceNum,
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800253 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (!newblock)
255 return NULL;
256 dbh = udf_tgetblk(inode->i_sb, newblock);
257 if (!dbh)
258 return NULL;
259 lock_buffer(dbh);
260 memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
261 set_buffer_uptodate(dbh);
262 unlock_buffer(dbh);
263 mark_buffer_dirty_inode(dbh, inode);
264
Marcin Slusarz4b111112008-02-08 04:20:36 -0800265 sfibh.soffset = sfibh.eoffset =
Jan Karaaf793292008-02-08 04:20:50 -0800266 f_pos & (inode->i_sb->s_blocksize - 1);
Jan Karaff116fc2007-05-08 00:35:14 -0700267 sfibh.sbh = sfibh.ebh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 dfibh.soffset = dfibh.eoffset = 0;
269 dfibh.sbh = dfibh.ebh = dbh;
Jan Karaaf793292008-02-08 04:20:50 -0800270 while (f_pos < size) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800271 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800272 sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL,
273 NULL, NULL, NULL);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700274 if (!sfi) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700275 brelse(dbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return NULL;
277 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800278 iinfo->i_alloc_type = alloctype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 sfi->descTag.tagLocation = cpu_to_le32(*block);
280 dfibh.soffset = dfibh.eoffset;
281 dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
282 dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
283 if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800284 sfi->fileIdent +
285 le16_to_cpu(sfi->lengthOfImpUse))) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800286 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700287 brelse(dbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return NULL;
289 }
290 }
291 mark_buffer_dirty_inode(dbh, inode);
292
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800293 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0,
294 iinfo->i_lenAlloc);
295 iinfo->i_lenAlloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 eloc.logicalBlockNum = *block;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800297 eloc.partitionReferenceNum =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800298 iinfo->i_location.partitionReferenceNum;
Jan Kara2c948b32009-12-03 13:39:28 +0100299 iinfo->i_lenExtents = inode->i_size;
Jan Karaff116fc2007-05-08 00:35:14 -0700300 epos.bh = NULL;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800301 epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700302 epos.offset = udf_file_entry_alloc_offset(inode);
Jan Kara2c948b32009-12-03 13:39:28 +0100303 udf_add_aext(inode, &epos, &eloc, inode->i_size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 /* UniqueID stuff */
305
Jan Kara3bf25cb2007-05-08 00:35:16 -0700306 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 mark_inode_dirty(inode);
308 return dbh;
309}
310
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700311static int udf_get_block(struct inode *inode, sector_t block,
312 struct buffer_head *bh_result, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
314 int err, new;
315 struct buffer_head *bh;
Marcin Slusarz1ed16172008-02-08 04:20:48 -0800316 sector_t phys = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800317 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700319 if (!create) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 phys = udf_block_map(inode, block);
321 if (phys)
322 map_bh(bh_result, inode->i_sb, phys);
323 return 0;
324 }
325
326 err = -EIO;
327 new = 0;
328 bh = NULL;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800329 iinfo = UDF_I(inode);
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +0100330
331 down_write(&iinfo->i_data_sem);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800332 if (block == iinfo->i_next_alloc_block + 1) {
333 iinfo->i_next_alloc_block++;
334 iinfo->i_next_alloc_goal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
336
337 err = 0;
338
339 bh = inode_getblk(inode, block, &err, &phys, &new);
Eric Sesterhenn2c2111c2006-04-02 13:40:13 +0200340 BUG_ON(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (err)
342 goto abort;
Eric Sesterhenn2c2111c2006-04-02 13:40:13 +0200343 BUG_ON(!phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 if (new)
346 set_buffer_new(bh_result);
347 map_bh(bh_result, inode->i_sb, phys);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700348
349abort:
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +0100350 up_write(&iinfo->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
353
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700354static struct buffer_head *udf_getblk(struct inode *inode, long block,
355 int create, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700357 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 struct buffer_head dummy;
359
360 dummy.b_state = 0;
361 dummy.b_blocknr = -1000;
362 *err = udf_get_block(inode, block, &dummy, create);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700363 if (!*err && buffer_mapped(&dummy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700365 if (buffer_new(&dummy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 lock_buffer(bh);
367 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
368 set_buffer_uptodate(bh);
369 unlock_buffer(bh);
370 mark_buffer_dirty_inode(bh, inode);
371 }
372 return bh;
373 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return NULL;
376}
377
Jan Kara31170b62007-05-08 00:35:21 -0700378/* Extend the file by 'blocks' blocks, return the number of extents added */
Jan Kara7e49b6f2010-10-22 00:30:26 +0200379static int udf_do_extend_file(struct inode *inode,
380 struct extent_position *last_pos,
381 struct kernel_long_ad *last_ext,
382 sector_t blocks)
Jan Kara31170b62007-05-08 00:35:21 -0700383{
384 sector_t add;
385 int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
386 struct super_block *sb = inode->i_sb;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200387 struct kernel_lb_addr prealloc_loc = {};
Jan Kara31170b62007-05-08 00:35:21 -0700388 int prealloc_len = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800389 struct udf_inode_info *iinfo;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200390 int err;
Jan Kara31170b62007-05-08 00:35:21 -0700391
392 /* The previous extent is fake and we should not extend by anything
393 * - there's nothing to do... */
394 if (!blocks && fake)
395 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700396
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800397 iinfo = UDF_I(inode);
Jan Kara31170b62007-05-08 00:35:21 -0700398 /* Round the last extent up to a multiple of block size */
399 if (last_ext->extLength & (sb->s_blocksize - 1)) {
400 last_ext->extLength =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700401 (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
402 (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
403 sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800404 iinfo->i_lenExtents =
405 (iinfo->i_lenExtents + sb->s_blocksize - 1) &
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700406 ~(sb->s_blocksize - 1);
Jan Kara31170b62007-05-08 00:35:21 -0700407 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700408
Jan Kara31170b62007-05-08 00:35:21 -0700409 /* Last extent are just preallocated blocks? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800410 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
411 EXT_NOT_RECORDED_ALLOCATED) {
Jan Kara31170b62007-05-08 00:35:21 -0700412 /* Save the extent so that we can reattach it to the end */
413 prealloc_loc = last_ext->extLocation;
414 prealloc_len = last_ext->extLength;
415 /* Mark the extent as a hole */
416 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700417 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
Jan Kara31170b62007-05-08 00:35:21 -0700418 last_ext->extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800419 last_ext->extLocation.partitionReferenceNum = 0;
Jan Kara31170b62007-05-08 00:35:21 -0700420 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700421
Jan Kara31170b62007-05-08 00:35:21 -0700422 /* Can we merge with the previous extent? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800423 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
424 EXT_NOT_RECORDED_NOT_ALLOCATED) {
425 add = ((1 << 30) - sb->s_blocksize -
426 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK)) >>
427 sb->s_blocksize_bits;
Jan Kara31170b62007-05-08 00:35:21 -0700428 if (add > blocks)
429 add = blocks;
430 blocks -= add;
431 last_ext->extLength += add << sb->s_blocksize_bits;
432 }
433
434 if (fake) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200435 udf_add_aext(inode, last_pos, &last_ext->extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700436 last_ext->extLength, 1);
Jan Kara31170b62007-05-08 00:35:21 -0700437 count++;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800438 } else
Pekka Enberg97e961f2008-10-15 12:29:03 +0200439 udf_write_aext(inode, last_pos, &last_ext->extLocation,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800440 last_ext->extLength, 1);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700441
Jan Kara31170b62007-05-08 00:35:21 -0700442 /* Managed to do everything necessary? */
443 if (!blocks)
444 goto out;
445
446 /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
447 last_ext->extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800448 last_ext->extLocation.partitionReferenceNum = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700449 add = (1 << (30-sb->s_blocksize_bits)) - 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800450 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
451 (add << sb->s_blocksize_bits);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700452
Jan Kara31170b62007-05-08 00:35:21 -0700453 /* Create enough extents to cover the whole hole */
454 while (blocks > add) {
455 blocks -= add;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200456 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
457 last_ext->extLength, 1);
458 if (err)
459 return err;
Jan Kara31170b62007-05-08 00:35:21 -0700460 count++;
461 }
462 if (blocks) {
463 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700464 (blocks << sb->s_blocksize_bits);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200465 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
466 last_ext->extLength, 1);
467 if (err)
468 return err;
Jan Kara31170b62007-05-08 00:35:21 -0700469 count++;
470 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700471
472out:
Jan Kara31170b62007-05-08 00:35:21 -0700473 /* Do we have some preallocated blocks saved? */
474 if (prealloc_len) {
Jan Kara7e49b6f2010-10-22 00:30:26 +0200475 err = udf_add_aext(inode, last_pos, &prealloc_loc,
476 prealloc_len, 1);
477 if (err)
478 return err;
Jan Kara31170b62007-05-08 00:35:21 -0700479 last_ext->extLocation = prealloc_loc;
480 last_ext->extLength = prealloc_len;
481 count++;
482 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700483
Jan Kara31170b62007-05-08 00:35:21 -0700484 /* last_pos should point to the last written extent... */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800485 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200486 last_pos->offset -= sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800487 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200488 last_pos->offset -= sizeof(struct long_ad);
Jan Kara31170b62007-05-08 00:35:21 -0700489 else
Jan Kara7e49b6f2010-10-22 00:30:26 +0200490 return -EIO;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700491
Jan Kara31170b62007-05-08 00:35:21 -0700492 return count;
493}
494
Jan Kara7e49b6f2010-10-22 00:30:26 +0200495static int udf_extend_file(struct inode *inode, loff_t newsize)
496{
497
498 struct extent_position epos;
499 struct kernel_lb_addr eloc;
500 uint32_t elen;
501 int8_t etype;
502 struct super_block *sb = inode->i_sb;
503 sector_t first_block = newsize >> sb->s_blocksize_bits, offset;
504 int adsize;
505 struct udf_inode_info *iinfo = UDF_I(inode);
506 struct kernel_long_ad extent;
507 int err;
508
509 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
510 adsize = sizeof(struct short_ad);
511 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
512 adsize = sizeof(struct long_ad);
513 else
514 BUG();
515
516 etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
517
518 /* File has extent covering the new size (could happen when extending
519 * inside a block)? */
520 if (etype != -1)
521 return 0;
522 if (newsize & (sb->s_blocksize - 1))
523 offset++;
524 /* Extended file just to the boundary of the last file block? */
525 if (offset == 0)
526 return 0;
527
528 /* Truncate is extending the file by 'offset' blocks */
529 if ((!epos.bh && epos.offset == udf_file_entry_alloc_offset(inode)) ||
530 (epos.bh && epos.offset == sizeof(struct allocExtDesc))) {
531 /* File has no extents at all or has empty last
532 * indirect extent! Create a fake extent... */
533 extent.extLocation.logicalBlockNum = 0;
534 extent.extLocation.partitionReferenceNum = 0;
535 extent.extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
536 } else {
537 epos.offset -= adsize;
538 etype = udf_next_aext(inode, &epos, &extent.extLocation,
539 &extent.extLength, 0);
540 extent.extLength |= etype << 30;
541 }
542 err = udf_do_extend_file(inode, &epos, &extent, offset);
543 if (err < 0)
544 goto out;
545 err = 0;
546 iinfo->i_lenExtents = newsize;
547out:
548 brelse(epos.bh);
549 return err;
550}
551
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700552static struct buffer_head *inode_getblk(struct inode *inode, sector_t block,
Marcin Slusarz1ed16172008-02-08 04:20:48 -0800553 int *err, sector_t *phys, int *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Jan Kara31170b62007-05-08 00:35:21 -0700555 static sector_t last_block;
Jan Karaff116fc2007-05-08 00:35:14 -0700556 struct buffer_head *result = NULL;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200557 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE];
Jan Karaff116fc2007-05-08 00:35:14 -0700558 struct extent_position prev_epos, cur_epos, next_epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 int count = 0, startnum = 0, endnum = 0;
Jan Kara85d71242007-06-01 00:46:29 -0700560 uint32_t elen = 0, tmpelen;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200561 struct kernel_lb_addr eloc, tmpeloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 int c = 1;
Jan Kara60448b12007-05-08 00:35:13 -0700563 loff_t lbcount = 0, b_off = 0;
564 uint32_t newblocknum, newblock;
565 sector_t offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800567 struct udf_inode_info *iinfo = UDF_I(inode);
568 int goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
Jan Kara31170b62007-05-08 00:35:21 -0700569 int lastblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Jan Karaff116fc2007-05-08 00:35:14 -0700571 prev_epos.offset = udf_file_entry_alloc_offset(inode);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800572 prev_epos.block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -0700573 prev_epos.bh = NULL;
574 cur_epos = next_epos = prev_epos;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700575 b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 /* find the extent which contains the block we are looking for.
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700578 alternate between laarr[0] and laarr[1] for locations of the
579 current extent, and the previous extent */
580 do {
581 if (prev_epos.bh != cur_epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700582 brelse(prev_epos.bh);
583 get_bh(cur_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700584 prev_epos.bh = cur_epos.bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700586 if (cur_epos.bh != next_epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700587 brelse(cur_epos.bh);
588 get_bh(next_epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -0700589 cur_epos.bh = next_epos.bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
591
592 lbcount += elen;
593
Jan Karaff116fc2007-05-08 00:35:14 -0700594 prev_epos.block = cur_epos.block;
595 cur_epos.block = next_epos.block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Jan Karaff116fc2007-05-08 00:35:14 -0700597 prev_epos.offset = cur_epos.offset;
598 cur_epos.offset = next_epos.offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Marcin Slusarz4b111112008-02-08 04:20:36 -0800600 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
601 if (etype == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 break;
603
604 c = !c;
605
606 laarr[c].extLength = (etype << 30) | elen;
607 laarr[c].extLocation = eloc;
608
609 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
610 pgoal = eloc.logicalBlockNum +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700611 ((elen + inode->i_sb->s_blocksize - 1) >>
612 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700614 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 } while (lbcount + elen <= b_off);
616
617 b_off -= lbcount;
618 offset = b_off >> inode->i_sb->s_blocksize_bits;
Jan Kara85d71242007-06-01 00:46:29 -0700619 /*
620 * Move prev_epos and cur_epos into indirect extent if we are at
621 * the pointer to it
622 */
623 udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
624 udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 /* if the extent is allocated and recorded, return the block
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700627 if the extent is not a multiple of the blocksize, round up */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700629 if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
630 if (elen & (inode->i_sb->s_blocksize - 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 elen = EXT_RECORDED_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700632 ((elen + inode->i_sb->s_blocksize - 1) &
633 ~(inode->i_sb->s_blocksize - 1));
Jan Kara7e49b6f2010-10-22 00:30:26 +0200634 udf_write_aext(inode, &cur_epos, &eloc, elen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
Jan Kara3bf25cb2007-05-08 00:35:16 -0700636 brelse(prev_epos.bh);
637 brelse(cur_epos.bh);
638 brelse(next_epos.bh);
Pekka Enberg97e961f2008-10-15 12:29:03 +0200639 newblock = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 *phys = newblock;
641 return NULL;
642 }
643
Jan Kara31170b62007-05-08 00:35:21 -0700644 last_block = block;
645 /* Are we beyond EOF? */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700646 if (etype == -1) {
Jan Kara31170b62007-05-08 00:35:21 -0700647 int ret;
648
649 if (count) {
650 if (c)
651 laarr[0] = laarr[1];
652 startnum = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700653 } else {
Jan Kara31170b62007-05-08 00:35:21 -0700654 /* Create a fake extent when there's not one */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800655 memset(&laarr[0].extLocation, 0x00,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200656 sizeof(struct kernel_lb_addr));
Jan Kara31170b62007-05-08 00:35:21 -0700657 laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
Jan Kara7e49b6f2010-10-22 00:30:26 +0200658 /* Will udf_do_extend_file() create real extent from
Marcin Slusarz4b111112008-02-08 04:20:36 -0800659 a fake one? */
Jan Kara31170b62007-05-08 00:35:21 -0700660 startnum = (offset > 0);
661 }
662 /* Create extents for the hole between EOF and offset */
Jan Kara7e49b6f2010-10-22 00:30:26 +0200663 ret = udf_do_extend_file(inode, &prev_epos, laarr, offset);
664 if (ret < 0) {
Jan Kara31170b62007-05-08 00:35:21 -0700665 brelse(prev_epos.bh);
666 brelse(cur_epos.bh);
667 brelse(next_epos.bh);
Jan Kara7e49b6f2010-10-22 00:30:26 +0200668 *err = ret;
Jan Kara31170b62007-05-08 00:35:21 -0700669 return NULL;
670 }
671 c = 0;
672 offset = 0;
673 count += ret;
674 /* We are not covered by a preallocated extent? */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800675 if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
676 EXT_NOT_RECORDED_ALLOCATED) {
Jan Kara31170b62007-05-08 00:35:21 -0700677 /* Is there any real extent? - otherwise we overwrite
678 * the fake one... */
679 if (count)
680 c = !c;
681 laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700682 inode->i_sb->s_blocksize;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800683 memset(&laarr[c].extLocation, 0x00,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200684 sizeof(struct kernel_lb_addr));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700685 count++;
686 endnum++;
Jan Kara31170b62007-05-08 00:35:21 -0700687 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700688 endnum = c + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 lastblock = 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700690 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 endnum = startnum = ((count > 2) ? 2 : count);
692
Marcin Slusarz4b111112008-02-08 04:20:36 -0800693 /* if the current extent is in position 0,
694 swap it with the previous */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700695 if (!c && count != 1) {
Jan Kara31170b62007-05-08 00:35:21 -0700696 laarr[2] = laarr[0];
697 laarr[0] = laarr[1];
698 laarr[1] = laarr[2];
699 c = 1;
700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Marcin Slusarz4b111112008-02-08 04:20:36 -0800702 /* if the current block is located in an extent,
703 read the next extent */
704 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
705 if (etype != -1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700706 laarr[c + 1].extLength = (etype << 30) | elen;
707 laarr[c + 1].extLocation = eloc;
708 count++;
709 startnum++;
710 endnum++;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800711 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 lastblock = 1;
713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 /* if the current extent is not recorded but allocated, get the
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700716 * block in the extent corresponding to the requested block */
Marcin Slusarz4b111112008-02-08 04:20:36 -0800717 if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800719 else { /* otherwise, allocate a new block */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800720 if (iinfo->i_next_alloc_block == block)
721 goal = iinfo->i_next_alloc_goal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700723 if (!goal) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800724 if (!(goal = pgoal)) /* XXX: what was intended here? */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800725 goal = iinfo->i_location.logicalBlockNum + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
727
Marcin Slusarz4b111112008-02-08 04:20:36 -0800728 newblocknum = udf_new_block(inode->i_sb, inode,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800729 iinfo->i_location.partitionReferenceNum,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800730 goal, err);
731 if (!newblocknum) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700732 brelse(prev_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 *err = -ENOSPC;
734 return NULL;
735 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800736 iinfo->i_lenExtents += inode->i_sb->s_blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
738
Marcin Slusarz4b111112008-02-08 04:20:36 -0800739 /* if the extent the requsted block is located in contains multiple
740 * blocks, split the extent into at most three extents. blocks prior
741 * to requested block, requested block, and blocks after requested
742 * block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
744
745#ifdef UDF_PREALLOCATE
Jan Kara81056dd2009-07-16 18:02:25 +0200746 /* We preallocate blocks only for regular files. It also makes sense
747 * for directories but there's a problem when to drop the
748 * preallocation. We might use some delayed work for that but I feel
749 * it's overengineering for a filesystem like UDF. */
750 if (S_ISREG(inode->i_mode))
751 udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752#endif
753
754 /* merge any continuous blocks in laarr */
755 udf_merge_extents(inode, laarr, &endnum);
756
757 /* write back the new extents, inserting new extents if the new number
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700758 * of extents is greater than the old number, and deleting extents if
759 * the new number of extents is less than the old number */
Jan Karaff116fc2007-05-08 00:35:14 -0700760 udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Jan Kara3bf25cb2007-05-08 00:35:16 -0700762 brelse(prev_epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Marcin Slusarz4b111112008-02-08 04:20:36 -0800764 newblock = udf_get_pblock(inode->i_sb, newblocknum,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800765 iinfo->i_location.partitionReferenceNum, 0);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800766 if (!newblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 *phys = newblock;
769 *err = 0;
770 *new = 1;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -0800771 iinfo->i_next_alloc_block = block;
772 iinfo->i_next_alloc_goal = newblocknum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 inode->i_ctime = current_fs_time(inode->i_sb);
774
775 if (IS_SYNC(inode))
776 udf_sync_inode(inode);
777 else
778 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return result;
781}
782
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700783static void udf_split_extents(struct inode *inode, int *c, int offset,
784 int newblocknum,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200785 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700786 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Marcin Slusarz4b111112008-02-08 04:20:36 -0800788 unsigned long blocksize = inode->i_sb->s_blocksize;
789 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
Marcin Slusarz4b111112008-02-08 04:20:36 -0800792 (laarr[*c].extLength >> 30) ==
793 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 int curr = *c;
795 int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800796 blocksize - 1) >> blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 int8_t etype = (laarr[curr].extLength >> 30);
798
Marcin Slusarz4b111112008-02-08 04:20:36 -0800799 if (blen == 1)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700800 ;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800801 else if (!offset || blen == offset + 1) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700802 laarr[curr + 2] = laarr[curr + 1];
803 laarr[curr + 1] = laarr[curr];
804 } else {
805 laarr[curr + 3] = laarr[curr + 1];
806 laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
808
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700809 if (offset) {
810 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800811 udf_free_blocks(inode->i_sb, inode,
Pekka Enberg97e961f2008-10-15 12:29:03 +0200812 &laarr[curr].extLocation,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800813 0, offset);
814 laarr[curr].extLength =
815 EXT_NOT_RECORDED_NOT_ALLOCATED |
816 (offset << blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 laarr[curr].extLocation.logicalBlockNum = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800818 laarr[curr].extLocation.
819 partitionReferenceNum = 0;
820 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 laarr[curr].extLength = (etype << 30) |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800822 (offset << blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700823 curr++;
824 (*c)++;
825 (*endnum)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 }
Cyrill Gorcunov647bd612007-07-15 23:39:47 -0700827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 laarr[curr].extLocation.logicalBlockNum = newblocknum;
829 if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
830 laarr[curr].extLocation.partitionReferenceNum =
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800831 UDF_I(inode)->i_location.partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800833 blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700834 curr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700836 if (blen != offset + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
Marcin Slusarz4b111112008-02-08 04:20:36 -0800838 laarr[curr].extLocation.logicalBlockNum +=
839 offset + 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700840 laarr[curr].extLength = (etype << 30) |
Marcin Slusarz4b111112008-02-08 04:20:36 -0800841 ((blen - (offset + 1)) << blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700842 curr++;
843 (*endnum)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
845 }
846}
847
848static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200849 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700850 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
852 int start, length = 0, currlength = 0, i;
853
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700854 if (*endnum >= (c + 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (!lastblock)
856 return;
857 else
858 start = c;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700859 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800860 if ((laarr[c + 1].extLength >> 30) ==
861 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700862 start = c + 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800863 length = currlength =
864 (((laarr[c + 1].extLength &
865 UDF_EXTENT_LENGTH_MASK) +
866 inode->i_sb->s_blocksize - 1) >>
867 inode->i_sb->s_blocksize_bits);
868 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 start = c;
870 }
871
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700872 for (i = start + 1; i <= *endnum; i++) {
873 if (i == *endnum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 if (lastblock)
875 length += UDF_DEFAULT_PREALLOC_BLOCKS;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800876 } else if ((laarr[i].extLength >> 30) ==
877 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
878 length += (((laarr[i].extLength &
879 UDF_EXTENT_LENGTH_MASK) +
880 inode->i_sb->s_blocksize - 1) >>
881 inode->i_sb->s_blocksize_bits);
882 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 break;
884 }
885
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700886 if (length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 int next = laarr[start].extLocation.logicalBlockNum +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700888 (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800889 inode->i_sb->s_blocksize - 1) >>
890 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800892 laarr[start].extLocation.partitionReferenceNum,
893 next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
894 length : UDF_DEFAULT_PREALLOC_BLOCKS) -
895 currlength);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700896 if (numalloc) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800897 if (start == (c + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 laarr[start].extLength +=
Marcin Slusarz4b111112008-02-08 04:20:36 -0800899 (numalloc <<
900 inode->i_sb->s_blocksize_bits);
901 else {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700902 memmove(&laarr[c + 2], &laarr[c + 1],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200903 sizeof(struct long_ad) * (*endnum - (c + 1)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700904 (*endnum)++;
905 laarr[c + 1].extLocation.logicalBlockNum = next;
906 laarr[c + 1].extLocation.partitionReferenceNum =
Marcin Slusarz4b111112008-02-08 04:20:36 -0800907 laarr[c].extLocation.
908 partitionReferenceNum;
909 laarr[c + 1].extLength =
910 EXT_NOT_RECORDED_ALLOCATED |
911 (numalloc <<
912 inode->i_sb->s_blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700913 start = c + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 }
915
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700916 for (i = start + 1; numalloc && i < *endnum; i++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800917 int elen = ((laarr[i].extLength &
918 UDF_EXTENT_LENGTH_MASK) +
919 inode->i_sb->s_blocksize - 1) >>
920 inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700922 if (elen > numalloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 laarr[i].extLength -=
Marcin Slusarz4b111112008-02-08 04:20:36 -0800924 (numalloc <<
925 inode->i_sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 numalloc = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700927 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 numalloc -= elen;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700929 if (*endnum > (i + 1))
Marcin Slusarz4b111112008-02-08 04:20:36 -0800930 memmove(&laarr[i],
931 &laarr[i + 1],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200932 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -0800933 (*endnum - (i + 1)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700934 i--;
935 (*endnum)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
937 }
Marcin Slusarzc0b34432008-02-08 04:20:42 -0800938 UDF_I(inode)->i_lenExtents +=
Marcin Slusarz4b111112008-02-08 04:20:36 -0800939 numalloc << inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
941 }
942}
943
944static void udf_merge_extents(struct inode *inode,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200945 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700946 int *endnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
948 int i;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800949 unsigned long blocksize = inode->i_sb->s_blocksize;
950 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700952 for (i = 0; i < (*endnum - 1); i++) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200953 struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
954 struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Marcin Slusarz4b111112008-02-08 04:20:36 -0800956 if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
957 (((li->extLength >> 30) ==
958 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
959 ((lip1->extLocation.logicalBlockNum -
960 li->extLocation.logicalBlockNum) ==
961 (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
962 blocksize - 1) >> blocksize_bits)))) {
963
964 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
965 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
966 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
967 lip1->extLength = (lip1->extLength -
968 (li->extLength &
969 UDF_EXTENT_LENGTH_MASK) +
970 UDF_EXTENT_LENGTH_MASK) &
971 ~(blocksize - 1);
972 li->extLength = (li->extLength &
973 UDF_EXTENT_FLAG_MASK) +
974 (UDF_EXTENT_LENGTH_MASK + 1) -
975 blocksize;
976 lip1->extLocation.logicalBlockNum =
977 li->extLocation.logicalBlockNum +
978 ((li->extLength &
979 UDF_EXTENT_LENGTH_MASK) >>
980 blocksize_bits);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700981 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800982 li->extLength = lip1->extLength +
983 (((li->extLength &
984 UDF_EXTENT_LENGTH_MASK) +
985 blocksize - 1) & ~(blocksize - 1));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700986 if (*endnum > (i + 2))
987 memmove(&laarr[i + 1], &laarr[i + 2],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200988 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -0800989 (*endnum - (i + 2)));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700990 i--;
991 (*endnum)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
Marcin Slusarz4b111112008-02-08 04:20:36 -0800993 } else if (((li->extLength >> 30) ==
994 (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
995 ((lip1->extLength >> 30) ==
996 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200997 udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800998 ((li->extLength &
999 UDF_EXTENT_LENGTH_MASK) +
1000 blocksize - 1) >> blocksize_bits);
1001 li->extLocation.logicalBlockNum = 0;
1002 li->extLocation.partitionReferenceNum = 0;
1003
1004 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1005 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1006 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1007 lip1->extLength = (lip1->extLength -
1008 (li->extLength &
1009 UDF_EXTENT_LENGTH_MASK) +
1010 UDF_EXTENT_LENGTH_MASK) &
1011 ~(blocksize - 1);
1012 li->extLength = (li->extLength &
1013 UDF_EXTENT_FLAG_MASK) +
1014 (UDF_EXTENT_LENGTH_MASK + 1) -
1015 blocksize;
1016 } else {
1017 li->extLength = lip1->extLength +
1018 (((li->extLength &
1019 UDF_EXTENT_LENGTH_MASK) +
1020 blocksize - 1) & ~(blocksize - 1));
1021 if (*endnum > (i + 2))
1022 memmove(&laarr[i + 1], &laarr[i + 2],
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001023 sizeof(struct long_ad) *
Marcin Slusarz4b111112008-02-08 04:20:36 -08001024 (*endnum - (i + 2)));
1025 i--;
1026 (*endnum)--;
1027 }
1028 } else if ((li->extLength >> 30) ==
1029 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
1030 udf_free_blocks(inode->i_sb, inode,
Pekka Enberg97e961f2008-10-15 12:29:03 +02001031 &li->extLocation, 0,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001032 ((li->extLength &
1033 UDF_EXTENT_LENGTH_MASK) +
1034 blocksize - 1) >> blocksize_bits);
1035 li->extLocation.logicalBlockNum = 0;
1036 li->extLocation.partitionReferenceNum = 0;
1037 li->extLength = (li->extLength &
1038 UDF_EXTENT_LENGTH_MASK) |
1039 EXT_NOT_RECORDED_NOT_ALLOCATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 }
1041 }
1042}
1043
1044static void udf_update_extents(struct inode *inode,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001045 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001046 int startnum, int endnum,
1047 struct extent_position *epos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
1049 int start = 0, i;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001050 struct kernel_lb_addr tmploc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 uint32_t tmplen;
1052
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001053 if (startnum > endnum) {
1054 for (i = 0; i < (startnum - endnum); i++)
Jan Karaff116fc2007-05-08 00:35:14 -07001055 udf_delete_aext(inode, *epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001056 laarr[i].extLength);
1057 } else if (startnum < endnum) {
1058 for (i = 0; i < (endnum - startnum); i++) {
Jan Karaff116fc2007-05-08 00:35:14 -07001059 udf_insert_aext(inode, *epos, laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001060 laarr[i].extLength);
Jan Karaff116fc2007-05-08 00:35:14 -07001061 udf_next_aext(inode, epos, &laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001062 &laarr[i].extLength, 1);
1063 start++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 }
1065 }
1066
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001067 for (i = start; i < endnum; i++) {
Jan Karaff116fc2007-05-08 00:35:14 -07001068 udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
Pekka Enberg97e961f2008-10-15 12:29:03 +02001069 udf_write_aext(inode, epos, &laarr[i].extLocation,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001070 laarr[i].extLength, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 }
1072}
1073
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001074struct buffer_head *udf_bread(struct inode *inode, int block,
1075 int create, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001077 struct buffer_head *bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079 bh = udf_getblk(inode, block, create, err);
1080 if (!bh)
1081 return NULL;
1082
1083 if (buffer_uptodate(bh))
1084 return bh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 ll_rw_block(READ, 1, &bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 wait_on_buffer(bh);
1089 if (buffer_uptodate(bh))
1090 return bh;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 brelse(bh);
1093 *err = -EIO;
1094 return NULL;
1095}
1096
Jan Kara7e49b6f2010-10-22 00:30:26 +02001097int udf_setsize(struct inode *inode, loff_t newsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 int err;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001100 struct udf_inode_info *iinfo;
Jan Kara7e49b6f2010-10-22 00:30:26 +02001101 int bsize = 1 << inode->i_blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001104 S_ISLNK(inode->i_mode)))
Jan Kara7e49b6f2010-10-22 00:30:26 +02001105 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
Jan Kara7e49b6f2010-10-22 00:30:26 +02001107 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001109 iinfo = UDF_I(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001110 if (newsize > inode->i_size) {
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001111 down_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001112 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1113 if (bsize <
1114 (udf_file_entry_alloc_offset(inode) + newsize)) {
1115 err = udf_expand_file_adinicb(inode);
1116 if (err) {
1117 up_write(&iinfo->i_data_sem);
1118 return err;
1119 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001120 } else
Jan Kara7e49b6f2010-10-22 00:30:26 +02001121 iinfo->i_lenAlloc = newsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 }
Jan Kara7e49b6f2010-10-22 00:30:26 +02001123 err = udf_extend_file(inode, newsize);
1124 if (err) {
1125 up_write(&iinfo->i_data_sem);
1126 return err;
1127 }
1128 truncate_setsize(inode, newsize);
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001129 up_write(&iinfo->i_data_sem);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001130 } else {
Jan Kara7e49b6f2010-10-22 00:30:26 +02001131 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1132 down_write(&iinfo->i_data_sem);
1133 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr + newsize,
1134 0x00, bsize - newsize -
1135 udf_file_entry_alloc_offset(inode));
1136 iinfo->i_lenAlloc = newsize;
1137 truncate_setsize(inode, newsize);
1138 up_write(&iinfo->i_data_sem);
1139 goto update_time;
1140 }
1141 err = block_truncate_page(inode->i_mapping, newsize,
1142 udf_get_block);
1143 if (err)
1144 return err;
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001145 down_write(&iinfo->i_data_sem);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001146 truncate_setsize(inode, newsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 udf_truncate_extents(inode);
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01001148 up_write(&iinfo->i_data_sem);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001149 }
Jan Kara7e49b6f2010-10-22 00:30:26 +02001150update_time:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 inode->i_mtime = inode->i_ctime = current_fs_time(inode->i_sb);
1152 if (IS_SYNC(inode))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001153 udf_sync_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 else
1155 mark_inode_dirty(inode);
Jan Kara7e49b6f2010-10-22 00:30:26 +02001156 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157}
1158
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001159static void __udf_read_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
1161 struct buffer_head *bh = NULL;
1162 struct fileEntry *fe;
1163 uint16_t ident;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001164 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
1166 /*
1167 * Set defaults, but the inode is still incomplete!
1168 * Note: get_new_inode() sets the following on a new inode:
1169 * i_sb = sb
1170 * i_no = ino
1171 * i_flags = sb->s_flags
1172 * i_state = 0
1173 * clean_inode(): zero fills and sets
1174 * i_count = 1
1175 * i_nlink = 1
1176 * i_op = NULL;
1177 */
Pekka Enberg97e961f2008-10-15 12:29:03 +02001178 bh = udf_read_ptagged(inode->i_sb, &iinfo->i_location, 0, &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001179 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 printk(KERN_ERR "udf: udf_read_inode(ino %ld) failed !bh\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001181 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 make_bad_inode(inode);
1183 return;
1184 }
1185
1186 if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001187 ident != TAG_IDENT_USE) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001188 printk(KERN_ERR "udf: udf_read_inode(ino %ld) "
1189 "failed ident=%d\n", inode->i_ino, ident);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001190 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 make_bad_inode(inode);
1192 return;
1193 }
1194
1195 fe = (struct fileEntry *)bh->b_data;
1196
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001197 if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001198 struct buffer_head *ibh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Pekka Enberg97e961f2008-10-15 12:29:03 +02001200 ibh = udf_read_ptagged(inode->i_sb, &iinfo->i_location, 1,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001201 &ident);
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001202 if (ident == TAG_IDENT_IE && ibh) {
1203 struct buffer_head *nbh = NULL;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001204 struct kernel_lb_addr loc;
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001205 struct indirectEntry *ie;
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001206
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001207 ie = (struct indirectEntry *)ibh->b_data;
1208 loc = lelb_to_cpu(ie->indirectICB.extLocation);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001209
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001210 if (ie->indirectICB.extLength &&
Pekka Enberg97e961f2008-10-15 12:29:03 +02001211 (nbh = udf_read_ptagged(inode->i_sb, &loc, 0,
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001212 &ident))) {
1213 if (ident == TAG_IDENT_FE ||
1214 ident == TAG_IDENT_EFE) {
1215 memcpy(&iinfo->i_location,
1216 &loc,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001217 sizeof(struct kernel_lb_addr));
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001218 brelse(bh);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001219 brelse(ibh);
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001220 brelse(nbh);
1221 __udf_read_inode(inode);
1222 return;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001223 }
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001224 brelse(nbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001226 }
marcin.slusarz@gmail.com1ab92782008-01-30 22:03:58 +01001227 brelse(ibh);
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001228 } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 printk(KERN_ERR "udf: unsupported strategy type: %d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001230 le16_to_cpu(fe->icbTag.strategyType));
Jan Kara3bf25cb2007-05-08 00:35:16 -07001231 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 make_bad_inode(inode);
1233 return;
1234 }
1235 udf_fill_inode(inode, bh);
Jan Kara31170b62007-05-08 00:35:21 -07001236
Jan Kara3bf25cb2007-05-08 00:35:16 -07001237 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238}
1239
1240static void udf_fill_inode(struct inode *inode, struct buffer_head *bh)
1241{
1242 struct fileEntry *fe;
1243 struct extendedFileEntry *efe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 int offset;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001245 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001246 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248 fe = (struct fileEntry *)bh->b_data;
1249 efe = (struct extendedFileEntry *)bh->b_data;
1250
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001251 if (fe->icbTag.strategyType == cpu_to_le16(4))
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001252 iinfo->i_strat4096 = 0;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001253 else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001254 iinfo->i_strat4096 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001256 iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
Marcin Slusarz4b111112008-02-08 04:20:36 -08001257 ICBTAG_FLAG_AD_MASK;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001258 iinfo->i_unique = 0;
1259 iinfo->i_lenEAttr = 0;
1260 iinfo->i_lenExtents = 0;
1261 iinfo->i_lenAlloc = 0;
1262 iinfo->i_next_alloc_block = 0;
1263 iinfo->i_next_alloc_goal = 0;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001264 if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001265 iinfo->i_efe = 1;
1266 iinfo->i_use = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001267 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1268 sizeof(struct extendedFileEntry))) {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001269 make_bad_inode(inode);
1270 return;
1271 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001272 memcpy(iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001273 bh->b_data + sizeof(struct extendedFileEntry),
1274 inode->i_sb->s_blocksize -
1275 sizeof(struct extendedFileEntry));
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001276 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001277 iinfo->i_efe = 0;
1278 iinfo->i_use = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001279 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1280 sizeof(struct fileEntry))) {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001281 make_bad_inode(inode);
1282 return;
1283 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001284 memcpy(iinfo->i_ext.i_data,
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001285 bh->b_data + sizeof(struct fileEntry),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001286 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001287 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001288 iinfo->i_efe = 0;
1289 iinfo->i_use = 1;
1290 iinfo->i_lenAlloc = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001291 ((struct unallocSpaceEntry *)bh->b_data)->
1292 lengthAllocDescs);
1293 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1294 sizeof(struct unallocSpaceEntry))) {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001295 make_bad_inode(inode);
1296 return;
1297 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001298 memcpy(iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001299 bh->b_data + sizeof(struct unallocSpaceEntry),
1300 inode->i_sb->s_blocksize -
1301 sizeof(struct unallocSpaceEntry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 return;
1303 }
1304
Jan Karac03cad22010-10-20 22:17:28 +02001305 read_lock(&sbi->s_cred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 inode->i_uid = le32_to_cpu(fe->uid);
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -07001307 if (inode->i_uid == -1 ||
1308 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_IGNORE) ||
1309 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
Phillip Susi4d6660e2006-03-07 21:55:24 -08001310 inode->i_uid = UDF_SB(inode->i_sb)->s_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
1312 inode->i_gid = le32_to_cpu(fe->gid);
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -07001313 if (inode->i_gid == -1 ||
1314 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_IGNORE) ||
1315 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
Phillip Susi4d6660e2006-03-07 21:55:24 -08001316 inode->i_gid = UDF_SB(inode->i_sb)->s_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01001318 if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
Marcin Slusarz87bc7302008-12-02 13:40:11 +01001319 sbi->s_fmode != UDF_INVALID_MODE)
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01001320 inode->i_mode = sbi->s_fmode;
1321 else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
Marcin Slusarz87bc7302008-12-02 13:40:11 +01001322 sbi->s_dmode != UDF_INVALID_MODE)
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01001323 inode->i_mode = sbi->s_dmode;
1324 else
1325 inode->i_mode = udf_convert_permissions(fe);
1326 inode->i_mode &= ~sbi->s_umask;
Jan Karac03cad22010-10-20 22:17:28 +02001327 read_unlock(&sbi->s_cred_lock);
1328
1329 inode->i_nlink = le16_to_cpu(fe->fileLinkCount);
1330 if (!inode->i_nlink)
1331 inode->i_nlink = 1;
1332
1333 inode->i_size = le64_to_cpu(fe->informationLength);
1334 iinfo->i_lenExtents = inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001336 if (iinfo->i_efe == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001338 (inode->i_sb->s_blocksize_bits - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Marcin Slusarz56774802008-02-10 11:25:31 +01001340 if (!udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001341 inode->i_atime = sbi->s_record_time;
1342
Marcin Slusarz56774802008-02-10 11:25:31 +01001343 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1344 fe->modificationTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001345 inode->i_mtime = sbi->s_record_time;
1346
Marcin Slusarz56774802008-02-10 11:25:31 +01001347 if (!udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001348 inode->i_ctime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001350 iinfo->i_unique = le64_to_cpu(fe->uniqueID);
1351 iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
1352 iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
1353 offset = sizeof(struct fileEntry) + iinfo->i_lenEAttr;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001354 } else {
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001355 inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001356 (inode->i_sb->s_blocksize_bits - 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Marcin Slusarz56774802008-02-10 11:25:31 +01001358 if (!udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001359 inode->i_atime = sbi->s_record_time;
1360
Marcin Slusarz56774802008-02-10 11:25:31 +01001361 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1362 efe->modificationTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001363 inode->i_mtime = sbi->s_record_time;
1364
Marcin Slusarz56774802008-02-10 11:25:31 +01001365 if (!udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001366 iinfo->i_crtime = sbi->s_record_time;
1367
Marcin Slusarz56774802008-02-10 11:25:31 +01001368 if (!udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime))
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +01001369 inode->i_ctime = sbi->s_record_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001371 iinfo->i_unique = le64_to_cpu(efe->uniqueID);
1372 iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
1373 iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001374 offset = sizeof(struct extendedFileEntry) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001375 iinfo->i_lenEAttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 }
1377
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001378 switch (fe->icbTag.fileType) {
1379 case ICBTAG_FILE_TYPE_DIRECTORY:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001380 inode->i_op = &udf_dir_inode_operations;
1381 inode->i_fop = &udf_dir_operations;
1382 inode->i_mode |= S_IFDIR;
1383 inc_nlink(inode);
1384 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001385 case ICBTAG_FILE_TYPE_REALTIME:
1386 case ICBTAG_FILE_TYPE_REGULAR:
1387 case ICBTAG_FILE_TYPE_UNDEF:
Jan Kara742e1792008-04-08 01:17:52 +02001388 case ICBTAG_FILE_TYPE_VAT20:
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001389 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001390 inode->i_data.a_ops = &udf_adinicb_aops;
1391 else
1392 inode->i_data.a_ops = &udf_aops;
1393 inode->i_op = &udf_file_inode_operations;
1394 inode->i_fop = &udf_file_operations;
1395 inode->i_mode |= S_IFREG;
1396 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001397 case ICBTAG_FILE_TYPE_BLOCK:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001398 inode->i_mode |= S_IFBLK;
1399 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001400 case ICBTAG_FILE_TYPE_CHAR:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001401 inode->i_mode |= S_IFCHR;
1402 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001403 case ICBTAG_FILE_TYPE_FIFO:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001404 init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1405 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001406 case ICBTAG_FILE_TYPE_SOCKET:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001407 init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1408 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001409 case ICBTAG_FILE_TYPE_SYMLINK:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001410 inode->i_data.a_ops = &udf_symlink_aops;
Dmitry Monakhovc15d0fc2010-03-29 11:05:21 +04001411 inode->i_op = &udf_symlink_inode_operations;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001412 inode->i_mode = S_IFLNK | S_IRWXUGO;
1413 break;
Jan Karabfb257a2008-04-08 20:37:21 +02001414 case ICBTAG_FILE_TYPE_MAIN:
1415 udf_debug("METADATA FILE-----\n");
1416 break;
1417 case ICBTAG_FILE_TYPE_MIRROR:
1418 udf_debug("METADATA MIRROR FILE-----\n");
1419 break;
1420 case ICBTAG_FILE_TYPE_BITMAP:
1421 udf_debug("METADATA BITMAP FILE-----\n");
1422 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001423 default:
Marcin Slusarz4b111112008-02-08 04:20:36 -08001424 printk(KERN_ERR "udf: udf_fill_inode(ino %ld) failed unknown "
1425 "file type=%d\n", inode->i_ino,
1426 fe->icbTag.fileType);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001427 make_bad_inode(inode);
1428 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001430 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001431 struct deviceSpec *dsea =
1432 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001433 if (dsea) {
1434 init_special_inode(inode, inode->i_mode,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001435 MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
1436 le32_to_cpu(dsea->minorDeviceIdent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 /* Developer ID ??? */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001438 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 make_bad_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 }
1441}
1442
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001443static int udf_alloc_i_data(struct inode *inode, size_t size)
1444{
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001445 struct udf_inode_info *iinfo = UDF_I(inode);
1446 iinfo->i_ext.i_data = kmalloc(size, GFP_KERNEL);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001447
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001448 if (!iinfo->i_ext.i_data) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001449 printk(KERN_ERR "udf:udf_alloc_i_data (ino %ld) "
1450 "no free memory\n", inode->i_ino);
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07001451 return -ENOMEM;
1452 }
1453
1454 return 0;
1455}
1456
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001457static mode_t udf_convert_permissions(struct fileEntry *fe)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
1459 mode_t mode;
1460 uint32_t permissions;
1461 uint32_t flags;
1462
1463 permissions = le32_to_cpu(fe->permissions);
1464 flags = le16_to_cpu(fe->icbTag.flags);
1465
Marcin Slusarz4b111112008-02-08 04:20:36 -08001466 mode = ((permissions) & S_IRWXO) |
1467 ((permissions >> 2) & S_IRWXG) |
1468 ((permissions >> 4) & S_IRWXU) |
1469 ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
1470 ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
1471 ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 return mode;
1474}
1475
Christoph Hellwiga9185b42010-03-05 09:21:37 +01001476int udf_write_inode(struct inode *inode, struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
Jan Kara49521de2010-10-20 17:42:44 +02001478 return udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479}
1480
Jan Kara49521de2010-10-20 17:42:44 +02001481static int udf_sync_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482{
1483 return udf_update_inode(inode, 1);
1484}
1485
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001486static int udf_update_inode(struct inode *inode, int do_sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
1488 struct buffer_head *bh = NULL;
1489 struct fileEntry *fe;
1490 struct extendedFileEntry *efe;
1491 uint32_t udfperms;
1492 uint16_t icbflags;
1493 uint16_t crclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 int err = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001495 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001496 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001497 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Jan Kara5833ded2010-01-08 16:52:59 +01001499 bh = udf_tgetblk(inode->i_sb,
1500 udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001501 if (!bh) {
Jan Karaaae917c2010-01-08 16:46:29 +01001502 udf_debug("getblk failure\n");
1503 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 }
1505
Jan Karaaae917c2010-01-08 16:46:29 +01001506 lock_buffer(bh);
1507 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 fe = (struct fileEntry *)bh->b_data;
1509 efe = (struct extendedFileEntry *)bh->b_data;
1510
Jan Karaaae917c2010-01-08 16:46:29 +01001511 if (iinfo->i_use) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 struct unallocSpaceEntry *use =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001513 (struct unallocSpaceEntry *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001515 use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001516 memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001517 iinfo->i_ext.i_data, inode->i_sb->s_blocksize -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001518 sizeof(struct unallocSpaceEntry));
Jan Karaaae917c2010-01-08 16:46:29 +01001519 use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
1520 use->descTag.tagLocation =
1521 cpu_to_le32(iinfo->i_location.logicalBlockNum);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001522 crclen = sizeof(struct unallocSpaceEntry) +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001523 iinfo->i_lenAlloc - sizeof(struct tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 use->descTag.descCRCLength = cpu_to_le16(crclen);
Bob Copelandf845fce2008-04-17 09:47:48 +02001525 use->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)use +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001526 sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02001527 crclen));
Marcin Slusarz3f2587b2008-02-08 04:20:39 -08001528 use->descTag.tagChecksum = udf_tag_checksum(&use->descTag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Jan Karaaae917c2010-01-08 16:46:29 +01001530 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 }
1532
Phillip Susi4d6660e2006-03-07 21:55:24 -08001533 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
1534 fe->uid = cpu_to_le32(-1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001535 else
1536 fe->uid = cpu_to_le32(inode->i_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Phillip Susi4d6660e2006-03-07 21:55:24 -08001538 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
1539 fe->gid = cpu_to_le32(-1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001540 else
1541 fe->gid = cpu_to_le32(inode->i_gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Marcin Slusarz4b111112008-02-08 04:20:36 -08001543 udfperms = ((inode->i_mode & S_IRWXO)) |
1544 ((inode->i_mode & S_IRWXG) << 2) |
1545 ((inode->i_mode & S_IRWXU) << 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Marcin Slusarz4b111112008-02-08 04:20:36 -08001547 udfperms |= (le32_to_cpu(fe->permissions) &
1548 (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
1549 FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
1550 FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 fe->permissions = cpu_to_le32(udfperms);
1552
1553 if (S_ISDIR(inode->i_mode))
1554 fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1555 else
1556 fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1557
1558 fe->informationLength = cpu_to_le64(inode->i_size);
1559
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001560 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001561 struct regid *eid;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001562 struct deviceSpec *dsea =
1563 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001564 if (!dsea) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 dsea = (struct deviceSpec *)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001566 udf_add_extendedattr(inode,
1567 sizeof(struct deviceSpec) +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001568 sizeof(struct regid), 12, 0x3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 dsea->attrType = cpu_to_le32(12);
1570 dsea->attrSubtype = 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001571 dsea->attrLength = cpu_to_le32(
1572 sizeof(struct deviceSpec) +
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001573 sizeof(struct regid));
1574 dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 }
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001576 eid = (struct regid *)dsea->impUse;
1577 memset(eid, 0, sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 strcpy(eid->ident, UDF_ID_DEVELOPER);
1579 eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1580 eid->identSuffix[1] = UDF_OS_ID_LINUX;
1581 dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1582 dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1583 }
1584
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001585 if (iinfo->i_efe == 0) {
Marcin Slusarzc0b34432008-02-08 04:20:42 -08001586 memcpy(bh->b_data + sizeof(struct fileEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001587 iinfo->i_ext.i_data,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001588 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001589 fe->logicalBlocksRecorded = cpu_to_le64(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001590 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1591 (blocksize_bits - 9));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Marcin Slusarz56774802008-02-10 11:25:31 +01001593 udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
1594 udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
1595 udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001596 memset(&(fe->impIdent), 0, sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1598 fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1599 fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001600 fe->uniqueID = cpu_to_le64(iinfo->i_unique);
1601 fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1602 fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1604 crclen = sizeof(struct fileEntry);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001605 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001606 memcpy(bh->b_data + sizeof(struct extendedFileEntry),
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001607 iinfo->i_ext.i_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001608 inode->i_sb->s_blocksize -
1609 sizeof(struct extendedFileEntry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 efe->objectSize = cpu_to_le64(inode->i_size);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001611 efe->logicalBlocksRecorded = cpu_to_le64(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001612 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1613 (blocksize_bits - 9));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001615 if (iinfo->i_crtime.tv_sec > inode->i_atime.tv_sec ||
1616 (iinfo->i_crtime.tv_sec == inode->i_atime.tv_sec &&
1617 iinfo->i_crtime.tv_nsec > inode->i_atime.tv_nsec))
1618 iinfo->i_crtime = inode->i_atime;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001619
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001620 if (iinfo->i_crtime.tv_sec > inode->i_mtime.tv_sec ||
1621 (iinfo->i_crtime.tv_sec == inode->i_mtime.tv_sec &&
1622 iinfo->i_crtime.tv_nsec > inode->i_mtime.tv_nsec))
1623 iinfo->i_crtime = inode->i_mtime;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001624
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001625 if (iinfo->i_crtime.tv_sec > inode->i_ctime.tv_sec ||
1626 (iinfo->i_crtime.tv_sec == inode->i_ctime.tv_sec &&
1627 iinfo->i_crtime.tv_nsec > inode->i_ctime.tv_nsec))
1628 iinfo->i_crtime = inode->i_ctime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Marcin Slusarz56774802008-02-10 11:25:31 +01001630 udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
1631 udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
1632 udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
1633 udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001635 memset(&(efe->impIdent), 0, sizeof(struct regid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1637 efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1638 efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001639 efe->uniqueID = cpu_to_le64(iinfo->i_unique);
1640 efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1641 efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1643 crclen = sizeof(struct extendedFileEntry);
1644 }
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001645 if (iinfo->i_strat4096) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 fe->icbTag.strategyType = cpu_to_le16(4096);
1647 fe->icbTag.strategyParameter = cpu_to_le16(1);
1648 fe->icbTag.numEntries = cpu_to_le16(2);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001649 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 fe->icbTag.strategyType = cpu_to_le16(4);
1651 fe->icbTag.numEntries = cpu_to_le16(1);
1652 }
1653
1654 if (S_ISDIR(inode->i_mode))
1655 fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
1656 else if (S_ISREG(inode->i_mode))
1657 fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
1658 else if (S_ISLNK(inode->i_mode))
1659 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
1660 else if (S_ISBLK(inode->i_mode))
1661 fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
1662 else if (S_ISCHR(inode->i_mode))
1663 fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
1664 else if (S_ISFIFO(inode->i_mode))
1665 fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
1666 else if (S_ISSOCK(inode->i_mode))
1667 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
1668
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001669 icbflags = iinfo->i_alloc_type |
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001670 ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
1671 ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
1672 ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
1673 (le16_to_cpu(fe->icbTag.flags) &
1674 ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
1675 ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
1677 fe->icbTag.flags = cpu_to_le16(icbflags);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001678 if (sbi->s_udfrev >= 0x0200)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 fe->descTag.descVersion = cpu_to_le16(3);
1680 else
1681 fe->descTag.descVersion = cpu_to_le16(2);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001682 fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001683 fe->descTag.tagLocation = cpu_to_le32(
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001684 iinfo->i_location.logicalBlockNum);
Jan Karaaae917c2010-01-08 16:46:29 +01001685 crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 fe->descTag.descCRCLength = cpu_to_le16(crclen);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001687 fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02001688 crclen));
Marcin Slusarz3f2587b2008-02-08 04:20:39 -08001689 fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Jan Karaaae917c2010-01-08 16:46:29 +01001691out:
Jan Kara5833ded2010-01-08 16:52:59 +01001692 set_buffer_uptodate(bh);
Jan Karaaae917c2010-01-08 16:46:29 +01001693 unlock_buffer(bh);
1694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 /* write the data blocks */
1696 mark_buffer_dirty(bh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001697 if (do_sync) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 sync_dirty_buffer(bh);
Jan Karaaae917c2010-01-08 16:46:29 +01001699 if (buffer_write_io_error(bh)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001700 printk(KERN_WARNING "IO error syncing udf inode "
1701 "[%s:%08lx]\n", inode->i_sb->s_id,
1702 inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 err = -EIO;
1704 }
1705 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001706 brelse(bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001707
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 return err;
1709}
1710
Pekka Enberg97e961f2008-10-15 12:29:03 +02001711struct inode *udf_iget(struct super_block *sb, struct kernel_lb_addr *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
1713 unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1714 struct inode *inode = iget_locked(sb, block);
1715
1716 if (!inode)
1717 return NULL;
1718
1719 if (inode->i_state & I_NEW) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02001720 memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 __udf_read_inode(inode);
1722 unlock_new_inode(inode);
1723 }
1724
1725 if (is_bad_inode(inode))
1726 goto out_iput;
1727
Pekka Enberg97e961f2008-10-15 12:29:03 +02001728 if (ino->logicalBlockNum >= UDF_SB(sb)->
1729 s_partmaps[ino->partitionReferenceNum].s_partition_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 udf_debug("block=%d, partition=%d out of range\n",
Pekka Enberg97e961f2008-10-15 12:29:03 +02001731 ino->logicalBlockNum, ino->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 make_bad_inode(inode);
1733 goto out_iput;
1734 }
1735
1736 return inode;
1737
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001738 out_iput:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 iput(inode);
1740 return NULL;
1741}
1742
Jan Kara7e49b6f2010-10-22 00:30:26 +02001743int udf_add_aext(struct inode *inode, struct extent_position *epos,
1744 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745{
1746 int adsize;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001747 struct short_ad *sad = NULL;
1748 struct long_ad *lad = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 struct allocExtDesc *aed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 uint8_t *ptr;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001751 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Jan Karaff116fc2007-05-08 00:35:14 -07001753 if (!epos->bh)
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001754 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001755 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001756 iinfo->i_lenEAttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 else
Jan Karaff116fc2007-05-08 00:35:14 -07001758 ptr = epos->bh->b_data + epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001760 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001761 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001762 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001763 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 else
Jan Kara7e49b6f2010-10-22 00:30:26 +02001765 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001767 if (epos->offset + (2 * adsize) > inode->i_sb->s_blocksize) {
Al Viro391e8bb2010-01-31 21:28:48 -05001768 unsigned char *sptr, *dptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 struct buffer_head *nbh;
1770 int err, loffset;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001771 struct kernel_lb_addr obloc = epos->block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Marcin Slusarz4b111112008-02-08 04:20:36 -08001773 epos->block.logicalBlockNum = udf_new_block(inode->i_sb, NULL,
1774 obloc.partitionReferenceNum,
1775 obloc.logicalBlockNum, &err);
1776 if (!epos->block.logicalBlockNum)
Jan Kara7e49b6f2010-10-22 00:30:26 +02001777 return -ENOSPC;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001778 nbh = udf_tgetblk(inode->i_sb, udf_get_lb_pblock(inode->i_sb,
Pekka Enberg97e961f2008-10-15 12:29:03 +02001779 &epos->block,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001780 0));
1781 if (!nbh)
Jan Kara7e49b6f2010-10-22 00:30:26 +02001782 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 lock_buffer(nbh);
1784 memset(nbh->b_data, 0x00, inode->i_sb->s_blocksize);
1785 set_buffer_uptodate(nbh);
1786 unlock_buffer(nbh);
1787 mark_buffer_dirty_inode(nbh, inode);
1788
1789 aed = (struct allocExtDesc *)(nbh->b_data);
1790 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001791 aed->previousAllocExtLocation =
1792 cpu_to_le32(obloc.logicalBlockNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001793 if (epos->offset + adsize > inode->i_sb->s_blocksize) {
Jan Karaff116fc2007-05-08 00:35:14 -07001794 loffset = epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 aed->lengthAllocDescs = cpu_to_le32(adsize);
1796 sptr = ptr - adsize;
1797 dptr = nbh->b_data + sizeof(struct allocExtDesc);
1798 memcpy(dptr, sptr, adsize);
Jan Karaff116fc2007-05-08 00:35:14 -07001799 epos->offset = sizeof(struct allocExtDesc) + adsize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001800 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001801 loffset = epos->offset + adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 aed->lengthAllocDescs = cpu_to_le32(0);
1803 sptr = ptr;
Jan Karaff116fc2007-05-08 00:35:14 -07001804 epos->offset = sizeof(struct allocExtDesc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001806 if (epos->bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07001807 aed = (struct allocExtDesc *)epos->bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01001808 le32_add_cpu(&aed->lengthAllocDescs, adsize);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001809 } else {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001810 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 mark_inode_dirty(inode);
1812 }
1813 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001814 if (UDF_SB(inode->i_sb)->s_udfrev >= 0x0200)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 3, 1,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001816 epos->block.logicalBlockNum, sizeof(struct tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 else
1818 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 2, 1,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001819 epos->block.logicalBlockNum, sizeof(struct tag));
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001820 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001821 case ICBTAG_FLAG_AD_SHORT:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001822 sad = (struct short_ad *)sptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001823 sad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1824 inode->i_sb->s_blocksize);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001825 sad->extPosition =
1826 cpu_to_le32(epos->block.logicalBlockNum);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001827 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001828 case ICBTAG_FLAG_AD_LONG:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001829 lad = (struct long_ad *)sptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001830 lad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1831 inode->i_sb->s_blocksize);
1832 lad->extLocation = cpu_to_lelb(epos->block);
1833 memset(lad->impUse, 0x00, sizeof(lad->impUse));
1834 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001836 if (epos->bh) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001837 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001838 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Jan Karaff116fc2007-05-08 00:35:14 -07001839 udf_update_tag(epos->bh->b_data, loffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08001841 udf_update_tag(epos->bh->b_data,
1842 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07001843 mark_buffer_dirty_inode(epos->bh, inode);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001844 brelse(epos->bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001845 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001847 }
Jan Karaff116fc2007-05-08 00:35:14 -07001848 epos->bh = nbh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 }
1850
Jan Kara7e49b6f2010-10-22 00:30:26 +02001851 udf_write_aext(inode, epos, eloc, elen, inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001853 if (!epos->bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001854 iinfo->i_lenAlloc += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001856 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001857 aed = (struct allocExtDesc *)epos->bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01001858 le32_add_cpu(&aed->lengthAllocDescs, adsize);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001859 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1860 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
1861 udf_update_tag(epos->bh->b_data,
1862 epos->offset + (inc ? 0 : adsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08001864 udf_update_tag(epos->bh->b_data,
1865 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07001866 mark_buffer_dirty_inode(epos->bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 }
1868
Jan Kara7e49b6f2010-10-22 00:30:26 +02001869 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870}
1871
Jan Kara7e49b6f2010-10-22 00:30:26 +02001872void udf_write_aext(struct inode *inode, struct extent_position *epos,
1873 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874{
1875 int adsize;
1876 uint8_t *ptr;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001877 struct short_ad *sad;
1878 struct long_ad *lad;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001879 struct udf_inode_info *iinfo = UDF_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Jan Karaff116fc2007-05-08 00:35:14 -07001881 if (!epos->bh)
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001882 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001883 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001884 iinfo->i_lenEAttr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 else
Jan Karaff116fc2007-05-08 00:35:14 -07001886 ptr = epos->bh->b_data + epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001888 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001889 case ICBTAG_FLAG_AD_SHORT:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001890 sad = (struct short_ad *)ptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001891 sad->extLength = cpu_to_le32(elen);
Pekka Enberg97e961f2008-10-15 12:29:03 +02001892 sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001893 adsize = sizeof(struct short_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001894 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001895 case ICBTAG_FLAG_AD_LONG:
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001896 lad = (struct long_ad *)ptr;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001897 lad->extLength = cpu_to_le32(elen);
Pekka Enberg97e961f2008-10-15 12:29:03 +02001898 lad->extLocation = cpu_to_lelb(*eloc);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001899 memset(lad->impUse, 0x00, sizeof(lad->impUse));
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001900 adsize = sizeof(struct long_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001901 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001902 default:
Jan Kara7e49b6f2010-10-22 00:30:26 +02001903 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 }
1905
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001906 if (epos->bh) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001907 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001908 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001909 struct allocExtDesc *aed =
1910 (struct allocExtDesc *)epos->bh->b_data;
Jan Karaff116fc2007-05-08 00:35:14 -07001911 udf_update_tag(epos->bh->b_data,
Marcin Slusarz4b111112008-02-08 04:20:36 -08001912 le32_to_cpu(aed->lengthAllocDescs) +
1913 sizeof(struct allocExtDesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 }
Jan Karaff116fc2007-05-08 00:35:14 -07001915 mark_buffer_dirty_inode(epos->bh, inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001916 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 mark_inode_dirty(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
1920 if (inc)
Jan Karaff116fc2007-05-08 00:35:14 -07001921 epos->offset += adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922}
1923
Marcin Slusarz4b111112008-02-08 04:20:36 -08001924int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001925 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926{
1927 int8_t etype;
1928
Jan Karaff116fc2007-05-08 00:35:14 -07001929 while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001930 (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001931 int block;
Jan Karaff116fc2007-05-08 00:35:14 -07001932 epos->block = *eloc;
1933 epos->offset = sizeof(struct allocExtDesc);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001934 brelse(epos->bh);
Pekka Enberg97e961f2008-10-15 12:29:03 +02001935 block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001936 epos->bh = udf_tread(inode->i_sb, block);
1937 if (!epos->bh) {
1938 udf_debug("reading block %d failed!\n", block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 return -1;
1940 }
1941 }
1942
1943 return etype;
1944}
1945
Marcin Slusarz4b111112008-02-08 04:20:36 -08001946int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001947 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948{
1949 int alen;
1950 int8_t etype;
1951 uint8_t *ptr;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001952 struct short_ad *sad;
1953 struct long_ad *lad;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001954 struct udf_inode_info *iinfo = UDF_I(inode);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001955
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001956 if (!epos->bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07001957 if (!epos->offset)
1958 epos->offset = udf_file_entry_alloc_offset(inode);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001959 ptr = iinfo->i_ext.i_data + epos->offset -
Marcin Slusarz4b111112008-02-08 04:20:36 -08001960 udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001961 iinfo->i_lenEAttr;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001962 alen = udf_file_entry_alloc_offset(inode) +
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001963 iinfo->i_lenAlloc;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001964 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07001965 if (!epos->offset)
1966 epos->offset = sizeof(struct allocExtDesc);
1967 ptr = epos->bh->b_data + epos->offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001968 alen = sizeof(struct allocExtDesc) +
Marcin Slusarz4b111112008-02-08 04:20:36 -08001969 le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
1970 lengthAllocDescs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 }
1972
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001973 switch (iinfo->i_alloc_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001974 case ICBTAG_FLAG_AD_SHORT:
Marcin Slusarz4b111112008-02-08 04:20:36 -08001975 sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
1976 if (!sad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 return -1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001978 etype = le32_to_cpu(sad->extLength) >> 30;
1979 eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001980 eloc->partitionReferenceNum =
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001981 iinfo->i_location.partitionReferenceNum;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001982 *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
1983 break;
1984 case ICBTAG_FLAG_AD_LONG:
Marcin Slusarz4b111112008-02-08 04:20:36 -08001985 lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
1986 if (!lad)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001987 return -1;
1988 etype = le32_to_cpu(lad->extLength) >> 30;
1989 *eloc = lelb_to_cpu(lad->extLocation);
1990 *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
1991 break;
1992 default:
Marcin Slusarz4b111112008-02-08 04:20:36 -08001993 udf_debug("alloc_type = %d unsupported\n",
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08001994 iinfo->i_alloc_type);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001995 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 }
1997
1998 return etype;
1999}
2000
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002001static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002002 struct kernel_lb_addr neloc, uint32_t nelen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002004 struct kernel_lb_addr oeloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 uint32_t oelen;
2006 int8_t etype;
2007
Jan Karaff116fc2007-05-08 00:35:14 -07002008 if (epos.bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07002009 get_bh(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002011 while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002012 udf_write_aext(inode, &epos, &neloc, nelen, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 neloc = oeloc;
2014 nelen = (etype << 30) | oelen;
2015 }
Pekka Enberg97e961f2008-10-15 12:29:03 +02002016 udf_add_aext(inode, &epos, &neloc, nelen, 1);
Jan Kara3bf25cb2007-05-08 00:35:16 -07002017 brelse(epos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002018
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 return (nelen >> 30);
2020}
2021
Marcin Slusarz4b111112008-02-08 04:20:36 -08002022int8_t udf_delete_aext(struct inode *inode, struct extent_position epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002023 struct kernel_lb_addr eloc, uint32_t elen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024{
Jan Karaff116fc2007-05-08 00:35:14 -07002025 struct extent_position oepos;
2026 int adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 int8_t etype;
2028 struct allocExtDesc *aed;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002029 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002031 if (epos.bh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002032 get_bh(epos.bh);
2033 get_bh(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 }
2035
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002036 iinfo = UDF_I(inode);
2037 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002038 adsize = sizeof(struct short_ad);
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002039 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002040 adsize = sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 else
2042 adsize = 0;
2043
Jan Karaff116fc2007-05-08 00:35:14 -07002044 oepos = epos;
2045 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 return -1;
2047
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002048 while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002049 udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002050 if (oepos.bh != epos.bh) {
Jan Karaff116fc2007-05-08 00:35:14 -07002051 oepos.block = epos.block;
Jan Kara3bf25cb2007-05-08 00:35:16 -07002052 brelse(oepos.bh);
2053 get_bh(epos.bh);
Jan Karaff116fc2007-05-08 00:35:14 -07002054 oepos.bh = epos.bh;
2055 oepos.offset = epos.offset - adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 }
2057 }
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002058 memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 elen = 0;
2060
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002061 if (epos.bh != oepos.bh) {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002062 udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
2063 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2064 udf_write_aext(inode, &oepos, &eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002065 if (!oepos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002066 iinfo->i_lenAlloc -= (adsize * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002068 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002069 aed = (struct allocExtDesc *)oepos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01002070 le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002071 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002072 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Marcin Slusarz4b111112008-02-08 04:20:36 -08002073 udf_update_tag(oepos.bh->b_data,
2074 oepos.offset - (2 * adsize));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08002076 udf_update_tag(oepos.bh->b_data,
2077 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07002078 mark_buffer_dirty_inode(oepos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002080 } else {
Pekka Enberg97e961f2008-10-15 12:29:03 +02002081 udf_write_aext(inode, &oepos, &eloc, elen, 1);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002082 if (!oepos.bh) {
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002083 iinfo->i_lenAlloc -= adsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 mark_inode_dirty(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002085 } else {
Jan Karaff116fc2007-05-08 00:35:14 -07002086 aed = (struct allocExtDesc *)oepos.bh->b_data;
marcin.slusarz@gmail.comc2104fd2008-01-30 22:03:57 +01002087 le32_add_cpu(&aed->lengthAllocDescs, -adsize);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002088 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002089 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
Marcin Slusarz4b111112008-02-08 04:20:36 -08002090 udf_update_tag(oepos.bh->b_data,
2091 epos.offset - adsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 else
Marcin Slusarz4b111112008-02-08 04:20:36 -08002093 udf_update_tag(oepos.bh->b_data,
2094 sizeof(struct allocExtDesc));
Jan Karaff116fc2007-05-08 00:35:14 -07002095 mark_buffer_dirty_inode(oepos.bh, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 }
2097 }
Cyrill Gorcunov647bd612007-07-15 23:39:47 -07002098
Jan Kara3bf25cb2007-05-08 00:35:16 -07002099 brelse(epos.bh);
2100 brelse(oepos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002101
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 return (elen >> 30);
2103}
2104
Marcin Slusarz4b111112008-02-08 04:20:36 -08002105int8_t inode_bmap(struct inode *inode, sector_t block,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002106 struct extent_position *pos, struct kernel_lb_addr *eloc,
Marcin Slusarz4b111112008-02-08 04:20:36 -08002107 uint32_t *elen, sector_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108{
Marcin Slusarz4b111112008-02-08 04:20:36 -08002109 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002110 loff_t lbcount = 0, bcount =
Marcin Slusarz4b111112008-02-08 04:20:36 -08002111 (loff_t) block << blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 int8_t etype;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002113 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002115 iinfo = UDF_I(inode);
Jan Karaff116fc2007-05-08 00:35:14 -07002116 pos->offset = 0;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002117 pos->block = iinfo->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -07002118 pos->bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 *elen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002121 do {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002122 etype = udf_next_aext(inode, pos, eloc, elen, 1);
2123 if (etype == -1) {
2124 *offset = (bcount - lbcount) >> blocksize_bits;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -08002125 iinfo->i_lenExtents = lbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 return -1;
2127 }
2128 lbcount += *elen;
2129 } while (lbcount <= bcount);
2130
Marcin Slusarz4b111112008-02-08 04:20:36 -08002131 *offset = (bcount + *elen - lbcount) >> blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
2133 return etype;
2134}
2135
Jan Kara60448b12007-05-08 00:35:13 -07002136long udf_block_map(struct inode *inode, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002138 struct kernel_lb_addr eloc;
Jan Karaff116fc2007-05-08 00:35:14 -07002139 uint32_t elen;
Jan Kara60448b12007-05-08 00:35:13 -07002140 sector_t offset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002141 struct extent_position epos = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 int ret;
2143
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01002144 down_read(&UDF_I(inode)->i_data_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
Marcin Slusarz4b111112008-02-08 04:20:36 -08002146 if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
2147 (EXT_RECORDED_ALLOCATED >> 30))
Pekka Enberg97e961f2008-10-15 12:29:03 +02002148 ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 else
2150 ret = 0;
2151
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +01002152 up_read(&UDF_I(inode)->i_data_sem);
Jan Kara3bf25cb2007-05-08 00:35:16 -07002153 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
2155 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
2156 return udf_fixed_to_variable(ret);
2157 else
2158 return ret;
2159}