]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * inode.c | |
3 | * | |
4 | * PURPOSE | |
5 | * Inode handling routines for the OSTA-UDF(tm) filesystem. | |
6 | * | |
1da177e4 LT |
7 | * 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 | |
4b11111a MS |
22 | * 12/06/98 blf partition support in udf_iget, udf_block_map |
23 | * and udf_read_inode | |
1da177e4 LT |
24 | * 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> | |
1da177e4 LT |
34 | #include <linux/module.h> |
35 | #include <linux/pagemap.h> | |
36 | #include <linux/buffer_head.h> | |
37 | #include <linux/writeback.h> | |
38 | #include <linux/slab.h> | |
f845fced | 39 | #include <linux/crc-itu-t.h> |
bc112323 | 40 | #include <linux/mpage.h> |
1da177e4 LT |
41 | |
42 | #include "udf_i.h" | |
43 | #include "udf_sb.h" | |
44 | ||
45 | MODULE_AUTHOR("Ben Fennema"); | |
46 | MODULE_DESCRIPTION("Universal Disk Format Filesystem"); | |
47 | MODULE_LICENSE("GPL"); | |
48 | ||
49 | #define EXTENT_MERGE_SIZE 5 | |
50 | ||
faa17292 | 51 | static umode_t udf_convert_permissions(struct fileEntry *); |
1da177e4 LT |
52 | static int udf_update_inode(struct inode *, int); |
53 | static void udf_fill_inode(struct inode *, struct buffer_head *); | |
49521de1 | 54 | static int udf_sync_inode(struct inode *inode); |
647bd61a | 55 | static int udf_alloc_i_data(struct inode *inode, size_t size); |
7b0b0933 | 56 | static sector_t inode_getblk(struct inode *, sector_t, int *, int *); |
ff116fc8 | 57 | static int8_t udf_insert_aext(struct inode *, struct extent_position, |
5ca4e4be | 58 | struct kernel_lb_addr, uint32_t); |
1da177e4 | 59 | static void udf_split_extents(struct inode *, int *, int, int, |
5ca4e4be | 60 | struct kernel_long_ad[EXTENT_MERGE_SIZE], int *); |
1da177e4 | 61 | static void udf_prealloc_extents(struct inode *, int, int, |
5ca4e4be | 62 | struct kernel_long_ad[EXTENT_MERGE_SIZE], int *); |
1da177e4 | 63 | static void udf_merge_extents(struct inode *, |
5ca4e4be | 64 | struct kernel_long_ad[EXTENT_MERGE_SIZE], int *); |
1da177e4 | 65 | static void udf_update_extents(struct inode *, |
5ca4e4be | 66 | struct kernel_long_ad[EXTENT_MERGE_SIZE], int, int, |
cb00ea35 | 67 | struct extent_position *); |
1da177e4 LT |
68 | static int udf_get_block(struct inode *, sector_t, struct buffer_head *, int); |
69 | ||
b1e32126 | 70 | |
3aac2b62 | 71 | void udf_evict_inode(struct inode *inode) |
1da177e4 | 72 | { |
2c948b3f | 73 | struct udf_inode_info *iinfo = UDF_I(inode); |
3aac2b62 AV |
74 | int want_delete = 0; |
75 | ||
3aac2b62 AV |
76 | if (!inode->i_nlink && !is_bad_inode(inode)) { |
77 | want_delete = 1; | |
7e49b6f2 | 78 | udf_setsize(inode, 0); |
3aac2b62 | 79 | udf_update_inode(inode, IS_SYNC(inode)); |
7e49b6f2 JK |
80 | } else |
81 | truncate_inode_pages(&inode->i_data, 0); | |
3aac2b62 | 82 | invalidate_inode_buffers(inode); |
dbd5768f | 83 | clear_inode(inode); |
2c948b3f JK |
84 | if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB && |
85 | inode->i_size != iinfo->i_lenExtents) { | |
78ace70c JP |
86 | udf_warn(inode->i_sb, "Inode %lu (mode %o) has inode size %llu different from extent length %llu. Filesystem need not be standards compliant.\n", |
87 | inode->i_ino, inode->i_mode, | |
88 | (unsigned long long)inode->i_size, | |
89 | (unsigned long long)iinfo->i_lenExtents); | |
1da177e4 | 90 | } |
48d6d8ff MS |
91 | kfree(iinfo->i_ext.i_data); |
92 | iinfo->i_ext.i_data = NULL; | |
3aac2b62 | 93 | if (want_delete) { |
3aac2b62 | 94 | udf_free_inode(inode); |
3aac2b62 | 95 | } |
1da177e4 LT |
96 | } |
97 | ||
5eec54fc IA |
98 | static void udf_write_failed(struct address_space *mapping, loff_t to) |
99 | { | |
100 | struct inode *inode = mapping->host; | |
101 | struct udf_inode_info *iinfo = UDF_I(inode); | |
102 | loff_t isize = inode->i_size; | |
103 | ||
104 | if (to > isize) { | |
105 | truncate_pagecache(inode, to, isize); | |
106 | if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) { | |
107 | down_write(&iinfo->i_data_sem); | |
108 | udf_truncate_extents(inode); | |
109 | up_write(&iinfo->i_data_sem); | |
110 | } | |
111 | } | |
112 | } | |
113 | ||
1da177e4 LT |
114 | static int udf_writepage(struct page *page, struct writeback_control *wbc) |
115 | { | |
116 | return block_write_full_page(page, udf_get_block, wbc); | |
117 | } | |
118 | ||
378b8e1a NJ |
119 | static int udf_writepages(struct address_space *mapping, |
120 | struct writeback_control *wbc) | |
121 | { | |
122 | return mpage_writepages(mapping, wbc, udf_get_block); | |
123 | } | |
124 | ||
1da177e4 LT |
125 | static int udf_readpage(struct file *file, struct page *page) |
126 | { | |
bc112323 NJ |
127 | return mpage_readpage(page, udf_get_block); |
128 | } | |
129 | ||
130 | static int udf_readpages(struct file *file, struct address_space *mapping, | |
131 | struct list_head *pages, unsigned nr_pages) | |
132 | { | |
133 | return mpage_readpages(mapping, pages, nr_pages, udf_get_block); | |
1da177e4 LT |
134 | } |
135 | ||
be021ee4 NP |
136 | static int udf_write_begin(struct file *file, struct address_space *mapping, |
137 | loff_t pos, unsigned len, unsigned flags, | |
138 | struct page **pagep, void **fsdata) | |
1da177e4 | 139 | { |
155130a4 CH |
140 | int ret; |
141 | ||
142 | ret = block_write_begin(mapping, pos, len, flags, pagep, udf_get_block); | |
5eec54fc IA |
143 | if (unlikely(ret)) |
144 | udf_write_failed(mapping, pos + len); | |
145 | return ret; | |
146 | } | |
155130a4 | 147 | |
5eec54fc IA |
148 | static ssize_t udf_direct_IO(int rw, struct kiocb *iocb, |
149 | const struct iovec *iov, | |
150 | loff_t offset, unsigned long nr_segs) | |
151 | { | |
152 | struct file *file = iocb->ki_filp; | |
153 | struct address_space *mapping = file->f_mapping; | |
154 | struct inode *inode = mapping->host; | |
155 | ssize_t ret; | |
156 | ||
157 | ret = blockdev_direct_IO(rw, iocb, inode, iov, offset, nr_segs, | |
158 | udf_get_block); | |
159 | if (unlikely(ret < 0 && (rw & WRITE))) | |
160 | udf_write_failed(mapping, offset + iov_length(iov, nr_segs)); | |
155130a4 | 161 | return ret; |
1da177e4 LT |
162 | } |
163 | ||
164 | static sector_t udf_bmap(struct address_space *mapping, sector_t block) | |
165 | { | |
cb00ea35 | 166 | return generic_block_bmap(mapping, block, udf_get_block); |
1da177e4 LT |
167 | } |
168 | ||
f5e54d6e | 169 | const struct address_space_operations udf_aops = { |
28de7948 | 170 | .readpage = udf_readpage, |
bc112323 | 171 | .readpages = udf_readpages, |
28de7948 | 172 | .writepage = udf_writepage, |
378b8e1a | 173 | .writepages = udf_writepages, |
5eec54fc IA |
174 | .write_begin = udf_write_begin, |
175 | .write_end = generic_write_end, | |
176 | .direct_IO = udf_direct_IO, | |
28de7948 | 177 | .bmap = udf_bmap, |
1da177e4 LT |
178 | }; |
179 | ||
d2eb8c35 JK |
180 | /* |
181 | * Expand file stored in ICB to a normal one-block-file | |
182 | * | |
183 | * This function requires i_data_sem for writing and releases it. | |
184 | * This function requires i_mutex held | |
185 | */ | |
7e49b6f2 | 186 | int udf_expand_file_adinicb(struct inode *inode) |
1da177e4 LT |
187 | { |
188 | struct page *page; | |
189 | char *kaddr; | |
48d6d8ff | 190 | struct udf_inode_info *iinfo = UDF_I(inode); |
7e49b6f2 | 191 | int err; |
1da177e4 LT |
192 | struct writeback_control udf_wbc = { |
193 | .sync_mode = WB_SYNC_NONE, | |
194 | .nr_to_write = 1, | |
195 | }; | |
196 | ||
48d6d8ff | 197 | if (!iinfo->i_lenAlloc) { |
1da177e4 | 198 | if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD)) |
48d6d8ff | 199 | iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT; |
1da177e4 | 200 | else |
48d6d8ff | 201 | iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG; |
7e49b6f2 JK |
202 | /* from now on we have normal address_space methods */ |
203 | inode->i_data.a_ops = &udf_aops; | |
d2eb8c35 | 204 | up_write(&iinfo->i_data_sem); |
1da177e4 | 205 | mark_inode_dirty(inode); |
7e49b6f2 | 206 | return 0; |
1da177e4 | 207 | } |
d2eb8c35 JK |
208 | /* |
209 | * Release i_data_sem so that we can lock a page - page lock ranks | |
210 | * above i_data_sem. i_mutex still protects us against file changes. | |
211 | */ | |
212 | up_write(&iinfo->i_data_sem); | |
1da177e4 | 213 | |
7e49b6f2 JK |
214 | page = find_or_create_page(inode->i_mapping, 0, GFP_NOFS); |
215 | if (!page) | |
216 | return -ENOMEM; | |
cd7619d6 | 217 | |
cb00ea35 | 218 | if (!PageUptodate(page)) { |
1da177e4 | 219 | kaddr = kmap(page); |
48d6d8ff MS |
220 | memset(kaddr + iinfo->i_lenAlloc, 0x00, |
221 | PAGE_CACHE_SIZE - iinfo->i_lenAlloc); | |
222 | memcpy(kaddr, iinfo->i_ext.i_data + iinfo->i_lenEAttr, | |
223 | iinfo->i_lenAlloc); | |
1da177e4 LT |
224 | flush_dcache_page(page); |
225 | SetPageUptodate(page); | |
226 | kunmap(page); | |
227 | } | |
d2eb8c35 | 228 | down_write(&iinfo->i_data_sem); |
48d6d8ff MS |
229 | memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0x00, |
230 | iinfo->i_lenAlloc); | |
231 | iinfo->i_lenAlloc = 0; | |
1da177e4 | 232 | if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD)) |
48d6d8ff | 233 | iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT; |
1da177e4 | 234 | else |
48d6d8ff | 235 | iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG; |
7e49b6f2 JK |
236 | /* from now on we have normal address_space methods */ |
237 | inode->i_data.a_ops = &udf_aops; | |
d2eb8c35 | 238 | up_write(&iinfo->i_data_sem); |
7e49b6f2 JK |
239 | err = inode->i_data.a_ops->writepage(page, &udf_wbc); |
240 | if (err) { | |
241 | /* Restore everything back so that we don't lose data... */ | |
242 | lock_page(page); | |
243 | kaddr = kmap(page); | |
d2eb8c35 | 244 | down_write(&iinfo->i_data_sem); |
7e49b6f2 JK |
245 | memcpy(iinfo->i_ext.i_data + iinfo->i_lenEAttr, kaddr, |
246 | inode->i_size); | |
247 | kunmap(page); | |
248 | unlock_page(page); | |
249 | iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB; | |
250 | inode->i_data.a_ops = &udf_adinicb_aops; | |
d2eb8c35 | 251 | up_write(&iinfo->i_data_sem); |
7e49b6f2 | 252 | } |
1da177e4 | 253 | page_cache_release(page); |
1da177e4 | 254 | mark_inode_dirty(inode); |
7e49b6f2 JK |
255 | |
256 | return err; | |
1da177e4 LT |
257 | } |
258 | ||
cb00ea35 CG |
259 | struct buffer_head *udf_expand_dir_adinicb(struct inode *inode, int *block, |
260 | int *err) | |
1da177e4 LT |
261 | { |
262 | int newblock; | |
ff116fc8 | 263 | struct buffer_head *dbh = NULL; |
5ca4e4be | 264 | struct kernel_lb_addr eloc; |
1da177e4 | 265 | uint8_t alloctype; |
ff116fc8 | 266 | struct extent_position epos; |
1da177e4 LT |
267 | |
268 | struct udf_fileident_bh sfibh, dfibh; | |
af793295 JK |
269 | loff_t f_pos = udf_ext0_offset(inode); |
270 | int size = udf_ext0_offset(inode) + inode->i_size; | |
1da177e4 | 271 | struct fileIdentDesc cfi, *sfi, *dfi; |
48d6d8ff | 272 | struct udf_inode_info *iinfo = UDF_I(inode); |
1da177e4 LT |
273 | |
274 | if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD)) | |
275 | alloctype = ICBTAG_FLAG_AD_SHORT; | |
276 | else | |
277 | alloctype = ICBTAG_FLAG_AD_LONG; | |
278 | ||
cb00ea35 | 279 | if (!inode->i_size) { |
48d6d8ff | 280 | iinfo->i_alloc_type = alloctype; |
1da177e4 LT |
281 | mark_inode_dirty(inode); |
282 | return NULL; | |
283 | } | |
284 | ||
285 | /* alloc block, and copy data to it */ | |
286 | *block = udf_new_block(inode->i_sb, inode, | |
48d6d8ff MS |
287 | iinfo->i_location.partitionReferenceNum, |
288 | iinfo->i_location.logicalBlockNum, err); | |
1da177e4 LT |
289 | if (!(*block)) |
290 | return NULL; | |
291 | newblock = udf_get_pblock(inode->i_sb, *block, | |
48d6d8ff | 292 | iinfo->i_location.partitionReferenceNum, |
c0b34438 | 293 | 0); |
1da177e4 LT |
294 | if (!newblock) |
295 | return NULL; | |
296 | dbh = udf_tgetblk(inode->i_sb, newblock); | |
297 | if (!dbh) | |
298 | return NULL; | |
299 | lock_buffer(dbh); | |
300 | memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize); | |
301 | set_buffer_uptodate(dbh); | |
302 | unlock_buffer(dbh); | |
303 | mark_buffer_dirty_inode(dbh, inode); | |
304 | ||
4b11111a | 305 | sfibh.soffset = sfibh.eoffset = |
af793295 | 306 | f_pos & (inode->i_sb->s_blocksize - 1); |
ff116fc8 | 307 | sfibh.sbh = sfibh.ebh = NULL; |
1da177e4 LT |
308 | dfibh.soffset = dfibh.eoffset = 0; |
309 | dfibh.sbh = dfibh.ebh = dbh; | |
af793295 | 310 | while (f_pos < size) { |
48d6d8ff | 311 | iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB; |
4b11111a MS |
312 | sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL, |
313 | NULL, NULL, NULL); | |
cb00ea35 | 314 | if (!sfi) { |
3bf25cb4 | 315 | brelse(dbh); |
1da177e4 LT |
316 | return NULL; |
317 | } | |
48d6d8ff | 318 | iinfo->i_alloc_type = alloctype; |
1da177e4 LT |
319 | sfi->descTag.tagLocation = cpu_to_le32(*block); |
320 | dfibh.soffset = dfibh.eoffset; | |
321 | dfibh.eoffset += (sfibh.eoffset - sfibh.soffset); | |
322 | dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset); | |
323 | if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse, | |
4b11111a MS |
324 | sfi->fileIdent + |
325 | le16_to_cpu(sfi->lengthOfImpUse))) { | |
48d6d8ff | 326 | iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB; |
3bf25cb4 | 327 | brelse(dbh); |
1da177e4 LT |
328 | return NULL; |
329 | } | |
330 | } | |
331 | mark_buffer_dirty_inode(dbh, inode); | |
332 | ||
48d6d8ff MS |
333 | memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0, |
334 | iinfo->i_lenAlloc); | |
335 | iinfo->i_lenAlloc = 0; | |
1da177e4 | 336 | eloc.logicalBlockNum = *block; |
4b11111a | 337 | eloc.partitionReferenceNum = |
48d6d8ff | 338 | iinfo->i_location.partitionReferenceNum; |
2c948b3f | 339 | iinfo->i_lenExtents = inode->i_size; |
ff116fc8 | 340 | epos.bh = NULL; |
48d6d8ff | 341 | epos.block = iinfo->i_location; |
ff116fc8 | 342 | epos.offset = udf_file_entry_alloc_offset(inode); |
2c948b3f | 343 | udf_add_aext(inode, &epos, &eloc, inode->i_size, 0); |
1da177e4 LT |
344 | /* UniqueID stuff */ |
345 | ||
3bf25cb4 | 346 | brelse(epos.bh); |
1da177e4 LT |
347 | mark_inode_dirty(inode); |
348 | return dbh; | |
349 | } | |
350 | ||
cb00ea35 CG |
351 | static int udf_get_block(struct inode *inode, sector_t block, |
352 | struct buffer_head *bh_result, int create) | |
1da177e4 LT |
353 | { |
354 | int err, new; | |
1ed16171 | 355 | sector_t phys = 0; |
48d6d8ff | 356 | struct udf_inode_info *iinfo; |
1da177e4 | 357 | |
cb00ea35 | 358 | if (!create) { |
1da177e4 LT |
359 | phys = udf_block_map(inode, block); |
360 | if (phys) | |
361 | map_bh(bh_result, inode->i_sb, phys); | |
362 | return 0; | |
363 | } | |
364 | ||
365 | err = -EIO; | |
366 | new = 0; | |
48d6d8ff | 367 | iinfo = UDF_I(inode); |
4d0fb621 AIB |
368 | |
369 | down_write(&iinfo->i_data_sem); | |
48d6d8ff MS |
370 | if (block == iinfo->i_next_alloc_block + 1) { |
371 | iinfo->i_next_alloc_block++; | |
372 | iinfo->i_next_alloc_goal++; | |
1da177e4 LT |
373 | } |
374 | ||
1da177e4 | 375 | |
7b0b0933 JK |
376 | phys = inode_getblk(inode, block, &err, &new); |
377 | if (!phys) | |
1da177e4 | 378 | goto abort; |
1da177e4 LT |
379 | |
380 | if (new) | |
381 | set_buffer_new(bh_result); | |
382 | map_bh(bh_result, inode->i_sb, phys); | |
28de7948 CG |
383 | |
384 | abort: | |
4d0fb621 | 385 | up_write(&iinfo->i_data_sem); |
1da177e4 | 386 | return err; |
1da177e4 LT |
387 | } |
388 | ||
cb00ea35 CG |
389 | static struct buffer_head *udf_getblk(struct inode *inode, long block, |
390 | int create, int *err) | |
1da177e4 | 391 | { |
28de7948 | 392 | struct buffer_head *bh; |
1da177e4 LT |
393 | struct buffer_head dummy; |
394 | ||
395 | dummy.b_state = 0; | |
396 | dummy.b_blocknr = -1000; | |
397 | *err = udf_get_block(inode, block, &dummy, create); | |
cb00ea35 | 398 | if (!*err && buffer_mapped(&dummy)) { |
1da177e4 | 399 | bh = sb_getblk(inode->i_sb, dummy.b_blocknr); |
cb00ea35 | 400 | if (buffer_new(&dummy)) { |
1da177e4 LT |
401 | lock_buffer(bh); |
402 | memset(bh->b_data, 0x00, inode->i_sb->s_blocksize); | |
403 | set_buffer_uptodate(bh); | |
404 | unlock_buffer(bh); | |
405 | mark_buffer_dirty_inode(bh, inode); | |
406 | } | |
407 | return bh; | |
408 | } | |
28de7948 | 409 | |
1da177e4 LT |
410 | return NULL; |
411 | } | |
412 | ||
31170b6a | 413 | /* Extend the file by 'blocks' blocks, return the number of extents added */ |
7e49b6f2 JK |
414 | static int udf_do_extend_file(struct inode *inode, |
415 | struct extent_position *last_pos, | |
416 | struct kernel_long_ad *last_ext, | |
417 | sector_t blocks) | |
31170b6a JK |
418 | { |
419 | sector_t add; | |
420 | int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK); | |
421 | struct super_block *sb = inode->i_sb; | |
5ca4e4be | 422 | struct kernel_lb_addr prealloc_loc = {}; |
31170b6a | 423 | int prealloc_len = 0; |
48d6d8ff | 424 | struct udf_inode_info *iinfo; |
7e49b6f2 | 425 | int err; |
31170b6a JK |
426 | |
427 | /* The previous extent is fake and we should not extend by anything | |
428 | * - there's nothing to do... */ | |
429 | if (!blocks && fake) | |
430 | return 0; | |
28de7948 | 431 | |
48d6d8ff | 432 | iinfo = UDF_I(inode); |
31170b6a JK |
433 | /* Round the last extent up to a multiple of block size */ |
434 | if (last_ext->extLength & (sb->s_blocksize - 1)) { | |
435 | last_ext->extLength = | |
28de7948 CG |
436 | (last_ext->extLength & UDF_EXTENT_FLAG_MASK) | |
437 | (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) + | |
438 | sb->s_blocksize - 1) & ~(sb->s_blocksize - 1)); | |
48d6d8ff MS |
439 | iinfo->i_lenExtents = |
440 | (iinfo->i_lenExtents + sb->s_blocksize - 1) & | |
28de7948 | 441 | ~(sb->s_blocksize - 1); |
31170b6a | 442 | } |
28de7948 | 443 | |
31170b6a | 444 | /* Last extent are just preallocated blocks? */ |
4b11111a MS |
445 | if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) == |
446 | EXT_NOT_RECORDED_ALLOCATED) { | |
31170b6a JK |
447 | /* Save the extent so that we can reattach it to the end */ |
448 | prealloc_loc = last_ext->extLocation; | |
449 | prealloc_len = last_ext->extLength; | |
450 | /* Mark the extent as a hole */ | |
451 | last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED | | |
28de7948 | 452 | (last_ext->extLength & UDF_EXTENT_LENGTH_MASK); |
31170b6a | 453 | last_ext->extLocation.logicalBlockNum = 0; |
4b11111a | 454 | last_ext->extLocation.partitionReferenceNum = 0; |
31170b6a | 455 | } |
28de7948 | 456 | |
31170b6a | 457 | /* Can we merge with the previous extent? */ |
4b11111a MS |
458 | if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) == |
459 | EXT_NOT_RECORDED_NOT_ALLOCATED) { | |
460 | add = ((1 << 30) - sb->s_blocksize - | |
461 | (last_ext->extLength & UDF_EXTENT_LENGTH_MASK)) >> | |
462 | sb->s_blocksize_bits; | |
31170b6a JK |
463 | if (add > blocks) |
464 | add = blocks; | |
465 | blocks -= add; | |
466 | last_ext->extLength += add << sb->s_blocksize_bits; | |
467 | } | |
468 | ||
469 | if (fake) { | |
97e961fd | 470 | udf_add_aext(inode, last_pos, &last_ext->extLocation, |
cb00ea35 | 471 | last_ext->extLength, 1); |
31170b6a | 472 | count++; |
4b11111a | 473 | } else |
97e961fd | 474 | udf_write_aext(inode, last_pos, &last_ext->extLocation, |
4b11111a | 475 | last_ext->extLength, 1); |
28de7948 | 476 | |
31170b6a JK |
477 | /* Managed to do everything necessary? */ |
478 | if (!blocks) | |
479 | goto out; | |
480 | ||
481 | /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */ | |
482 | last_ext->extLocation.logicalBlockNum = 0; | |
4b11111a | 483 | last_ext->extLocation.partitionReferenceNum = 0; |
28de7948 | 484 | add = (1 << (30-sb->s_blocksize_bits)) - 1; |
4b11111a MS |
485 | last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED | |
486 | (add << sb->s_blocksize_bits); | |
28de7948 | 487 | |
31170b6a JK |
488 | /* Create enough extents to cover the whole hole */ |
489 | while (blocks > add) { | |
490 | blocks -= add; | |
7e49b6f2 JK |
491 | err = udf_add_aext(inode, last_pos, &last_ext->extLocation, |
492 | last_ext->extLength, 1); | |
493 | if (err) | |
494 | return err; | |
31170b6a JK |
495 | count++; |
496 | } | |
497 | if (blocks) { | |
498 | last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED | | |
28de7948 | 499 | (blocks << sb->s_blocksize_bits); |
7e49b6f2 JK |
500 | err = udf_add_aext(inode, last_pos, &last_ext->extLocation, |
501 | last_ext->extLength, 1); | |
502 | if (err) | |
503 | return err; | |
31170b6a JK |
504 | count++; |
505 | } | |
28de7948 CG |
506 | |
507 | out: | |
31170b6a JK |
508 | /* Do we have some preallocated blocks saved? */ |
509 | if (prealloc_len) { | |
7e49b6f2 JK |
510 | err = udf_add_aext(inode, last_pos, &prealloc_loc, |
511 | prealloc_len, 1); | |
512 | if (err) | |
513 | return err; | |
31170b6a JK |
514 | last_ext->extLocation = prealloc_loc; |
515 | last_ext->extLength = prealloc_len; | |
516 | count++; | |
517 | } | |
28de7948 | 518 | |
31170b6a | 519 | /* last_pos should point to the last written extent... */ |
48d6d8ff | 520 | if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT) |
5ca4e4be | 521 | last_pos->offset -= sizeof(struct short_ad); |
48d6d8ff | 522 | else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG) |
5ca4e4be | 523 | last_pos->offset -= sizeof(struct long_ad); |
31170b6a | 524 | else |
7e49b6f2 | 525 | return -EIO; |
28de7948 | 526 | |
31170b6a JK |
527 | return count; |
528 | } | |
529 | ||
7e49b6f2 JK |
530 | static int udf_extend_file(struct inode *inode, loff_t newsize) |
531 | { | |
532 | ||
533 | struct extent_position epos; | |
534 | struct kernel_lb_addr eloc; | |
535 | uint32_t elen; | |
536 | int8_t etype; | |
537 | struct super_block *sb = inode->i_sb; | |
538 | sector_t first_block = newsize >> sb->s_blocksize_bits, offset; | |
539 | int adsize; | |
540 | struct udf_inode_info *iinfo = UDF_I(inode); | |
541 | struct kernel_long_ad extent; | |
542 | int err; | |
543 | ||
544 | if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT) | |
545 | adsize = sizeof(struct short_ad); | |
546 | else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG) | |
547 | adsize = sizeof(struct long_ad); | |
548 | else | |
549 | BUG(); | |
550 | ||
551 | etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset); | |
552 | ||
553 | /* File has extent covering the new size (could happen when extending | |
554 | * inside a block)? */ | |
555 | if (etype != -1) | |
556 | return 0; | |
557 | if (newsize & (sb->s_blocksize - 1)) | |
558 | offset++; | |
559 | /* Extended file just to the boundary of the last file block? */ | |
560 | if (offset == 0) | |
561 | return 0; | |
562 | ||
563 | /* Truncate is extending the file by 'offset' blocks */ | |
564 | if ((!epos.bh && epos.offset == udf_file_entry_alloc_offset(inode)) || | |
565 | (epos.bh && epos.offset == sizeof(struct allocExtDesc))) { | |
566 | /* File has no extents at all or has empty last | |
567 | * indirect extent! Create a fake extent... */ | |
568 | extent.extLocation.logicalBlockNum = 0; | |
569 | extent.extLocation.partitionReferenceNum = 0; | |
570 | extent.extLength = EXT_NOT_RECORDED_NOT_ALLOCATED; | |
571 | } else { | |
572 | epos.offset -= adsize; | |
573 | etype = udf_next_aext(inode, &epos, &extent.extLocation, | |
574 | &extent.extLength, 0); | |
575 | extent.extLength |= etype << 30; | |
576 | } | |
577 | err = udf_do_extend_file(inode, &epos, &extent, offset); | |
578 | if (err < 0) | |
579 | goto out; | |
580 | err = 0; | |
581 | iinfo->i_lenExtents = newsize; | |
582 | out: | |
583 | brelse(epos.bh); | |
584 | return err; | |
585 | } | |
586 | ||
7b0b0933 JK |
587 | static sector_t inode_getblk(struct inode *inode, sector_t block, |
588 | int *err, int *new) | |
1da177e4 | 589 | { |
5ca4e4be | 590 | struct kernel_long_ad laarr[EXTENT_MERGE_SIZE]; |
ff116fc8 | 591 | struct extent_position prev_epos, cur_epos, next_epos; |
1da177e4 | 592 | int count = 0, startnum = 0, endnum = 0; |
85d71244 | 593 | uint32_t elen = 0, tmpelen; |
5ca4e4be | 594 | struct kernel_lb_addr eloc, tmpeloc; |
1da177e4 | 595 | int c = 1; |
60448b1d JK |
596 | loff_t lbcount = 0, b_off = 0; |
597 | uint32_t newblocknum, newblock; | |
598 | sector_t offset = 0; | |
1da177e4 | 599 | int8_t etype; |
48d6d8ff MS |
600 | struct udf_inode_info *iinfo = UDF_I(inode); |
601 | int goal = 0, pgoal = iinfo->i_location.logicalBlockNum; | |
31170b6a | 602 | int lastblock = 0; |
fb719c59 | 603 | bool isBeyondEOF; |
1da177e4 | 604 | |
7b0b0933 JK |
605 | *err = 0; |
606 | *new = 0; | |
ff116fc8 | 607 | prev_epos.offset = udf_file_entry_alloc_offset(inode); |
48d6d8ff | 608 | prev_epos.block = iinfo->i_location; |
ff116fc8 JK |
609 | prev_epos.bh = NULL; |
610 | cur_epos = next_epos = prev_epos; | |
28de7948 | 611 | b_off = (loff_t)block << inode->i_sb->s_blocksize_bits; |
1da177e4 LT |
612 | |
613 | /* find the extent which contains the block we are looking for. | |
cb00ea35 CG |
614 | alternate between laarr[0] and laarr[1] for locations of the |
615 | current extent, and the previous extent */ | |
616 | do { | |
617 | if (prev_epos.bh != cur_epos.bh) { | |
3bf25cb4 JK |
618 | brelse(prev_epos.bh); |
619 | get_bh(cur_epos.bh); | |
ff116fc8 | 620 | prev_epos.bh = cur_epos.bh; |
1da177e4 | 621 | } |
cb00ea35 | 622 | if (cur_epos.bh != next_epos.bh) { |
3bf25cb4 JK |
623 | brelse(cur_epos.bh); |
624 | get_bh(next_epos.bh); | |
ff116fc8 | 625 | cur_epos.bh = next_epos.bh; |
1da177e4 LT |
626 | } |
627 | ||
628 | lbcount += elen; | |
629 | ||
ff116fc8 JK |
630 | prev_epos.block = cur_epos.block; |
631 | cur_epos.block = next_epos.block; | |
1da177e4 | 632 | |
ff116fc8 JK |
633 | prev_epos.offset = cur_epos.offset; |
634 | cur_epos.offset = next_epos.offset; | |
1da177e4 | 635 | |
4b11111a MS |
636 | etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1); |
637 | if (etype == -1) | |
1da177e4 LT |
638 | break; |
639 | ||
640 | c = !c; | |
641 | ||
642 | laarr[c].extLength = (etype << 30) | elen; | |
643 | laarr[c].extLocation = eloc; | |
644 | ||
645 | if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) | |
646 | pgoal = eloc.logicalBlockNum + | |
28de7948 CG |
647 | ((elen + inode->i_sb->s_blocksize - 1) >> |
648 | inode->i_sb->s_blocksize_bits); | |
1da177e4 | 649 | |
cb00ea35 | 650 | count++; |
1da177e4 LT |
651 | } while (lbcount + elen <= b_off); |
652 | ||
653 | b_off -= lbcount; | |
654 | offset = b_off >> inode->i_sb->s_blocksize_bits; | |
85d71244 JK |
655 | /* |
656 | * Move prev_epos and cur_epos into indirect extent if we are at | |
657 | * the pointer to it | |
658 | */ | |
659 | udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0); | |
660 | udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0); | |
1da177e4 LT |
661 | |
662 | /* if the extent is allocated and recorded, return the block | |
cb00ea35 | 663 | if the extent is not a multiple of the blocksize, round up */ |
1da177e4 | 664 | |
cb00ea35 CG |
665 | if (etype == (EXT_RECORDED_ALLOCATED >> 30)) { |
666 | if (elen & (inode->i_sb->s_blocksize - 1)) { | |
1da177e4 | 667 | elen = EXT_RECORDED_ALLOCATED | |
28de7948 CG |
668 | ((elen + inode->i_sb->s_blocksize - 1) & |
669 | ~(inode->i_sb->s_blocksize - 1)); | |
7e49b6f2 | 670 | udf_write_aext(inode, &cur_epos, &eloc, elen, 1); |
1da177e4 | 671 | } |
3bf25cb4 JK |
672 | brelse(prev_epos.bh); |
673 | brelse(cur_epos.bh); | |
674 | brelse(next_epos.bh); | |
97e961fd | 675 | newblock = udf_get_lb_pblock(inode->i_sb, &eloc, offset); |
7b0b0933 | 676 | return newblock; |
1da177e4 LT |
677 | } |
678 | ||
31170b6a | 679 | /* Are we beyond EOF? */ |
cb00ea35 | 680 | if (etype == -1) { |
31170b6a | 681 | int ret; |
fb719c59 | 682 | isBeyondEOF = 1; |
31170b6a JK |
683 | if (count) { |
684 | if (c) | |
685 | laarr[0] = laarr[1]; | |
686 | startnum = 1; | |
cb00ea35 | 687 | } else { |
31170b6a | 688 | /* Create a fake extent when there's not one */ |
4b11111a | 689 | memset(&laarr[0].extLocation, 0x00, |
5ca4e4be | 690 | sizeof(struct kernel_lb_addr)); |
31170b6a | 691 | laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED; |
7e49b6f2 | 692 | /* Will udf_do_extend_file() create real extent from |
4b11111a | 693 | a fake one? */ |
31170b6a JK |
694 | startnum = (offset > 0); |
695 | } | |
696 | /* Create extents for the hole between EOF and offset */ | |
7e49b6f2 JK |
697 | ret = udf_do_extend_file(inode, &prev_epos, laarr, offset); |
698 | if (ret < 0) { | |
31170b6a JK |
699 | brelse(prev_epos.bh); |
700 | brelse(cur_epos.bh); | |
701 | brelse(next_epos.bh); | |
7e49b6f2 | 702 | *err = ret; |
7b0b0933 | 703 | return 0; |
31170b6a JK |
704 | } |
705 | c = 0; | |
706 | offset = 0; | |
707 | count += ret; | |
708 | /* We are not covered by a preallocated extent? */ | |
4b11111a MS |
709 | if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) != |
710 | EXT_NOT_RECORDED_ALLOCATED) { | |
31170b6a JK |
711 | /* Is there any real extent? - otherwise we overwrite |
712 | * the fake one... */ | |
713 | if (count) | |
714 | c = !c; | |
715 | laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED | | |
28de7948 | 716 | inode->i_sb->s_blocksize; |
4b11111a | 717 | memset(&laarr[c].extLocation, 0x00, |
5ca4e4be | 718 | sizeof(struct kernel_lb_addr)); |
cb00ea35 | 719 | count++; |
31170b6a | 720 | } |
cb00ea35 | 721 | endnum = c + 1; |
1da177e4 | 722 | lastblock = 1; |
cb00ea35 | 723 | } else { |
fb719c59 | 724 | isBeyondEOF = 0; |
1da177e4 LT |
725 | endnum = startnum = ((count > 2) ? 2 : count); |
726 | ||
4b11111a MS |
727 | /* if the current extent is in position 0, |
728 | swap it with the previous */ | |
cb00ea35 | 729 | if (!c && count != 1) { |
31170b6a JK |
730 | laarr[2] = laarr[0]; |
731 | laarr[0] = laarr[1]; | |
732 | laarr[1] = laarr[2]; | |
733 | c = 1; | |
734 | } | |
1da177e4 | 735 | |
4b11111a MS |
736 | /* if the current block is located in an extent, |
737 | read the next extent */ | |
738 | etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0); | |
739 | if (etype != -1) { | |
cb00ea35 CG |
740 | laarr[c + 1].extLength = (etype << 30) | elen; |
741 | laarr[c + 1].extLocation = eloc; | |
742 | count++; | |
743 | startnum++; | |
744 | endnum++; | |
4b11111a | 745 | } else |
1da177e4 LT |
746 | lastblock = 1; |
747 | } | |
1da177e4 LT |
748 | |
749 | /* if the current extent is not recorded but allocated, get the | |
28de7948 | 750 | * block in the extent corresponding to the requested block */ |
4b11111a | 751 | if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30)) |
1da177e4 | 752 | newblocknum = laarr[c].extLocation.logicalBlockNum + offset; |
4b11111a | 753 | else { /* otherwise, allocate a new block */ |
48d6d8ff MS |
754 | if (iinfo->i_next_alloc_block == block) |
755 | goal = iinfo->i_next_alloc_goal; | |
1da177e4 | 756 | |
cb00ea35 | 757 | if (!goal) { |
4b11111a | 758 | if (!(goal = pgoal)) /* XXX: what was intended here? */ |
48d6d8ff | 759 | goal = iinfo->i_location.logicalBlockNum + 1; |
1da177e4 LT |
760 | } |
761 | ||
4b11111a | 762 | newblocknum = udf_new_block(inode->i_sb, inode, |
48d6d8ff | 763 | iinfo->i_location.partitionReferenceNum, |
4b11111a MS |
764 | goal, err); |
765 | if (!newblocknum) { | |
3bf25cb4 | 766 | brelse(prev_epos.bh); |
2fb7d99d NJ |
767 | brelse(cur_epos.bh); |
768 | brelse(next_epos.bh); | |
1da177e4 | 769 | *err = -ENOSPC; |
7b0b0933 | 770 | return 0; |
1da177e4 | 771 | } |
fb719c59 NJ |
772 | if (isBeyondEOF) |
773 | iinfo->i_lenExtents += inode->i_sb->s_blocksize; | |
1da177e4 LT |
774 | } |
775 | ||
4b11111a MS |
776 | /* if the extent the requsted block is located in contains multiple |
777 | * blocks, split the extent into at most three extents. blocks prior | |
778 | * to requested block, requested block, and blocks after requested | |
779 | * block */ | |
1da177e4 LT |
780 | udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum); |
781 | ||
782 | #ifdef UDF_PREALLOCATE | |
81056dd0 JK |
783 | /* We preallocate blocks only for regular files. It also makes sense |
784 | * for directories but there's a problem when to drop the | |
785 | * preallocation. We might use some delayed work for that but I feel | |
786 | * it's overengineering for a filesystem like UDF. */ | |
787 | if (S_ISREG(inode->i_mode)) | |
788 | udf_prealloc_extents(inode, c, lastblock, laarr, &endnum); | |
1da177e4 LT |
789 | #endif |
790 | ||
791 | /* merge any continuous blocks in laarr */ | |
792 | udf_merge_extents(inode, laarr, &endnum); | |
793 | ||
794 | /* write back the new extents, inserting new extents if the new number | |
28de7948 CG |
795 | * of extents is greater than the old number, and deleting extents if |
796 | * the new number of extents is less than the old number */ | |
ff116fc8 | 797 | udf_update_extents(inode, laarr, startnum, endnum, &prev_epos); |
1da177e4 | 798 | |
3bf25cb4 | 799 | brelse(prev_epos.bh); |
2fb7d99d NJ |
800 | brelse(cur_epos.bh); |
801 | brelse(next_epos.bh); | |
1da177e4 | 802 | |
4b11111a | 803 | newblock = udf_get_pblock(inode->i_sb, newblocknum, |
48d6d8ff | 804 | iinfo->i_location.partitionReferenceNum, 0); |
7b0b0933 JK |
805 | if (!newblock) { |
806 | *err = -EIO; | |
807 | return 0; | |
808 | } | |
1da177e4 | 809 | *new = 1; |
48d6d8ff MS |
810 | iinfo->i_next_alloc_block = block; |
811 | iinfo->i_next_alloc_goal = newblocknum; | |
1da177e4 LT |
812 | inode->i_ctime = current_fs_time(inode->i_sb); |
813 | ||
814 | if (IS_SYNC(inode)) | |
815 | udf_sync_inode(inode); | |
816 | else | |
817 | mark_inode_dirty(inode); | |
28de7948 | 818 | |
7b0b0933 | 819 | return newblock; |
1da177e4 LT |
820 | } |
821 | ||
cb00ea35 CG |
822 | static void udf_split_extents(struct inode *inode, int *c, int offset, |
823 | int newblocknum, | |
5ca4e4be | 824 | struct kernel_long_ad laarr[EXTENT_MERGE_SIZE], |
cb00ea35 | 825 | int *endnum) |
1da177e4 | 826 | { |
4b11111a MS |
827 | unsigned long blocksize = inode->i_sb->s_blocksize; |
828 | unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits; | |
829 | ||
1da177e4 | 830 | if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) || |
4b11111a MS |
831 | (laarr[*c].extLength >> 30) == |
832 | (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) { | |
1da177e4 LT |
833 | int curr = *c; |
834 | int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) + | |
4b11111a | 835 | blocksize - 1) >> blocksize_bits; |
1da177e4 LT |
836 | int8_t etype = (laarr[curr].extLength >> 30); |
837 | ||
4b11111a | 838 | if (blen == 1) |
28de7948 | 839 | ; |
4b11111a | 840 | else if (!offset || blen == offset + 1) { |
cb00ea35 CG |
841 | laarr[curr + 2] = laarr[curr + 1]; |
842 | laarr[curr + 1] = laarr[curr]; | |
843 | } else { | |
844 | laarr[curr + 3] = laarr[curr + 1]; | |
845 | laarr[curr + 2] = laarr[curr + 1] = laarr[curr]; | |
846 | } | |
847 | ||
848 | if (offset) { | |
849 | if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) { | |
4b11111a | 850 | udf_free_blocks(inode->i_sb, inode, |
97e961fd | 851 | &laarr[curr].extLocation, |
4b11111a MS |
852 | 0, offset); |
853 | laarr[curr].extLength = | |
854 | EXT_NOT_RECORDED_NOT_ALLOCATED | | |
855 | (offset << blocksize_bits); | |
1da177e4 | 856 | laarr[curr].extLocation.logicalBlockNum = 0; |
4b11111a MS |
857 | laarr[curr].extLocation. |
858 | partitionReferenceNum = 0; | |
859 | } else | |
1da177e4 | 860 | laarr[curr].extLength = (etype << 30) | |
4b11111a | 861 | (offset << blocksize_bits); |
cb00ea35 CG |
862 | curr++; |
863 | (*c)++; | |
864 | (*endnum)++; | |
1da177e4 | 865 | } |
647bd61a | 866 | |
1da177e4 LT |
867 | laarr[curr].extLocation.logicalBlockNum = newblocknum; |
868 | if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) | |
869 | laarr[curr].extLocation.partitionReferenceNum = | |
c0b34438 | 870 | UDF_I(inode)->i_location.partitionReferenceNum; |
1da177e4 | 871 | laarr[curr].extLength = EXT_RECORDED_ALLOCATED | |
4b11111a | 872 | blocksize; |
cb00ea35 | 873 | curr++; |
1da177e4 | 874 | |
cb00ea35 | 875 | if (blen != offset + 1) { |
1da177e4 | 876 | if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) |
4b11111a MS |
877 | laarr[curr].extLocation.logicalBlockNum += |
878 | offset + 1; | |
28de7948 | 879 | laarr[curr].extLength = (etype << 30) | |
4b11111a | 880 | ((blen - (offset + 1)) << blocksize_bits); |
cb00ea35 CG |
881 | curr++; |
882 | (*endnum)++; | |
1da177e4 LT |
883 | } |
884 | } | |
885 | } | |
886 | ||
887 | static void udf_prealloc_extents(struct inode *inode, int c, int lastblock, | |
5ca4e4be | 888 | struct kernel_long_ad laarr[EXTENT_MERGE_SIZE], |
cb00ea35 | 889 | int *endnum) |
1da177e4 LT |
890 | { |
891 | int start, length = 0, currlength = 0, i; | |
892 | ||
cb00ea35 | 893 | if (*endnum >= (c + 1)) { |
1da177e4 LT |
894 | if (!lastblock) |
895 | return; | |
896 | else | |
897 | start = c; | |
cb00ea35 | 898 | } else { |
4b11111a MS |
899 | if ((laarr[c + 1].extLength >> 30) == |
900 | (EXT_NOT_RECORDED_ALLOCATED >> 30)) { | |
cb00ea35 | 901 | start = c + 1; |
4b11111a MS |
902 | length = currlength = |
903 | (((laarr[c + 1].extLength & | |
904 | UDF_EXTENT_LENGTH_MASK) + | |
905 | inode->i_sb->s_blocksize - 1) >> | |
906 | inode->i_sb->s_blocksize_bits); | |
907 | } else | |
1da177e4 LT |
908 | start = c; |
909 | } | |
910 | ||
cb00ea35 CG |
911 | for (i = start + 1; i <= *endnum; i++) { |
912 | if (i == *endnum) { | |
1da177e4 LT |
913 | if (lastblock) |
914 | length += UDF_DEFAULT_PREALLOC_BLOCKS; | |
4b11111a MS |
915 | } else if ((laarr[i].extLength >> 30) == |
916 | (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) { | |
917 | length += (((laarr[i].extLength & | |
918 | UDF_EXTENT_LENGTH_MASK) + | |
919 | inode->i_sb->s_blocksize - 1) >> | |
920 | inode->i_sb->s_blocksize_bits); | |
921 | } else | |
1da177e4 LT |
922 | break; |
923 | } | |
924 | ||
cb00ea35 | 925 | if (length) { |
1da177e4 | 926 | int next = laarr[start].extLocation.logicalBlockNum + |
28de7948 | 927 | (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) + |
4b11111a MS |
928 | inode->i_sb->s_blocksize - 1) >> |
929 | inode->i_sb->s_blocksize_bits); | |
1da177e4 | 930 | int numalloc = udf_prealloc_blocks(inode->i_sb, inode, |
4b11111a MS |
931 | laarr[start].extLocation.partitionReferenceNum, |
932 | next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ? | |
933 | length : UDF_DEFAULT_PREALLOC_BLOCKS) - | |
934 | currlength); | |
28de7948 | 935 | if (numalloc) { |
4b11111a | 936 | if (start == (c + 1)) |
1da177e4 | 937 | laarr[start].extLength += |
4b11111a MS |
938 | (numalloc << |
939 | inode->i_sb->s_blocksize_bits); | |
940 | else { | |
cb00ea35 | 941 | memmove(&laarr[c + 2], &laarr[c + 1], |
5ca4e4be | 942 | sizeof(struct long_ad) * (*endnum - (c + 1))); |
cb00ea35 CG |
943 | (*endnum)++; |
944 | laarr[c + 1].extLocation.logicalBlockNum = next; | |
945 | laarr[c + 1].extLocation.partitionReferenceNum = | |
4b11111a MS |
946 | laarr[c].extLocation. |
947 | partitionReferenceNum; | |
948 | laarr[c + 1].extLength = | |
949 | EXT_NOT_RECORDED_ALLOCATED | | |
950 | (numalloc << | |
951 | inode->i_sb->s_blocksize_bits); | |
cb00ea35 | 952 | start = c + 1; |
1da177e4 LT |
953 | } |
954 | ||
cb00ea35 | 955 | for (i = start + 1; numalloc && i < *endnum; i++) { |
4b11111a MS |
956 | int elen = ((laarr[i].extLength & |
957 | UDF_EXTENT_LENGTH_MASK) + | |
958 | inode->i_sb->s_blocksize - 1) >> | |
959 | inode->i_sb->s_blocksize_bits; | |
1da177e4 | 960 | |
cb00ea35 | 961 | if (elen > numalloc) { |
1da177e4 | 962 | laarr[i].extLength -= |
4b11111a MS |
963 | (numalloc << |
964 | inode->i_sb->s_blocksize_bits); | |
1da177e4 | 965 | numalloc = 0; |
cb00ea35 | 966 | } else { |
1da177e4 | 967 | numalloc -= elen; |
cb00ea35 | 968 | if (*endnum > (i + 1)) |
4b11111a MS |
969 | memmove(&laarr[i], |
970 | &laarr[i + 1], | |
5ca4e4be | 971 | sizeof(struct long_ad) * |
4b11111a | 972 | (*endnum - (i + 1))); |
cb00ea35 CG |
973 | i--; |
974 | (*endnum)--; | |
1da177e4 LT |
975 | } |
976 | } | |
c0b34438 | 977 | UDF_I(inode)->i_lenExtents += |
4b11111a | 978 | numalloc << inode->i_sb->s_blocksize_bits; |
1da177e4 LT |
979 | } |
980 | } | |
981 | } | |
982 | ||
983 | static void udf_merge_extents(struct inode *inode, | |
5ca4e4be | 984 | struct kernel_long_ad laarr[EXTENT_MERGE_SIZE], |
cb00ea35 | 985 | int *endnum) |
1da177e4 LT |
986 | { |
987 | int i; | |
4b11111a MS |
988 | unsigned long blocksize = inode->i_sb->s_blocksize; |
989 | unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits; | |
1da177e4 | 990 | |
cb00ea35 | 991 | for (i = 0; i < (*endnum - 1); i++) { |
5ca4e4be PE |
992 | struct kernel_long_ad *li /*l[i]*/ = &laarr[i]; |
993 | struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1]; | |
4b11111a MS |
994 | |
995 | if (((li->extLength >> 30) == (lip1->extLength >> 30)) && | |
996 | (((li->extLength >> 30) == | |
997 | (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) || | |
998 | ((lip1->extLocation.logicalBlockNum - | |
999 | li->extLocation.logicalBlockNum) == | |
1000 | (((li->extLength & UDF_EXTENT_LENGTH_MASK) + | |
1001 | blocksize - 1) >> blocksize_bits)))) { | |
1002 | ||
1003 | if (((li->extLength & UDF_EXTENT_LENGTH_MASK) + | |
1004 | (lip1->extLength & UDF_EXTENT_LENGTH_MASK) + | |
1005 | blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) { | |
1006 | lip1->extLength = (lip1->extLength - | |
1007 | (li->extLength & | |
1008 | UDF_EXTENT_LENGTH_MASK) + | |
1009 | UDF_EXTENT_LENGTH_MASK) & | |
1010 | ~(blocksize - 1); | |
1011 | li->extLength = (li->extLength & | |
1012 | UDF_EXTENT_FLAG_MASK) + | |
1013 | (UDF_EXTENT_LENGTH_MASK + 1) - | |
1014 | blocksize; | |
1015 | lip1->extLocation.logicalBlockNum = | |
1016 | li->extLocation.logicalBlockNum + | |
1017 | ((li->extLength & | |
1018 | UDF_EXTENT_LENGTH_MASK) >> | |
1019 | blocksize_bits); | |
1020 | } else { | |
1021 | li->extLength = lip1->extLength + | |
1022 | (((li->extLength & | |
1023 | UDF_EXTENT_LENGTH_MASK) + | |
1024 | blocksize - 1) & ~(blocksize - 1)); | |
1025 | if (*endnum > (i + 2)) | |
1026 | memmove(&laarr[i + 1], &laarr[i + 2], | |
5ca4e4be | 1027 | sizeof(struct long_ad) * |
4b11111a MS |
1028 | (*endnum - (i + 2))); |
1029 | i--; | |
1030 | (*endnum)--; | |
1da177e4 | 1031 | } |
4b11111a MS |
1032 | } else if (((li->extLength >> 30) == |
1033 | (EXT_NOT_RECORDED_ALLOCATED >> 30)) && | |
1034 | ((lip1->extLength >> 30) == | |
1035 | (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) { | |
97e961fd | 1036 | udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0, |
4b11111a MS |
1037 | ((li->extLength & |
1038 | UDF_EXTENT_LENGTH_MASK) + | |
1039 | blocksize - 1) >> blocksize_bits); | |
1040 | li->extLocation.logicalBlockNum = 0; | |
1041 | li->extLocation.partitionReferenceNum = 0; | |
1042 | ||
1043 | if (((li->extLength & UDF_EXTENT_LENGTH_MASK) + | |
1044 | (lip1->extLength & UDF_EXTENT_LENGTH_MASK) + | |
1045 | blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) { | |
1046 | lip1->extLength = (lip1->extLength - | |
1047 | (li->extLength & | |
1048 | UDF_EXTENT_LENGTH_MASK) + | |
1049 | UDF_EXTENT_LENGTH_MASK) & | |
1050 | ~(blocksize - 1); | |
1051 | li->extLength = (li->extLength & | |
1052 | UDF_EXTENT_FLAG_MASK) + | |
1053 | (UDF_EXTENT_LENGTH_MASK + 1) - | |
1054 | blocksize; | |
cb00ea35 | 1055 | } else { |
4b11111a MS |
1056 | li->extLength = lip1->extLength + |
1057 | (((li->extLength & | |
1058 | UDF_EXTENT_LENGTH_MASK) + | |
1059 | blocksize - 1) & ~(blocksize - 1)); | |
cb00ea35 CG |
1060 | if (*endnum > (i + 2)) |
1061 | memmove(&laarr[i + 1], &laarr[i + 2], | |
5ca4e4be | 1062 | sizeof(struct long_ad) * |
4b11111a | 1063 | (*endnum - (i + 2))); |
cb00ea35 CG |
1064 | i--; |
1065 | (*endnum)--; | |
1da177e4 | 1066 | } |
4b11111a MS |
1067 | } else if ((li->extLength >> 30) == |
1068 | (EXT_NOT_RECORDED_ALLOCATED >> 30)) { | |
1069 | udf_free_blocks(inode->i_sb, inode, | |
97e961fd | 1070 | &li->extLocation, 0, |
4b11111a MS |
1071 | ((li->extLength & |
1072 | UDF_EXTENT_LENGTH_MASK) + | |
1073 | blocksize - 1) >> blocksize_bits); | |
1074 | li->extLocation.logicalBlockNum = 0; | |
1075 | li->extLocation.partitionReferenceNum = 0; | |
1076 | li->extLength = (li->extLength & | |
1077 | UDF_EXTENT_LENGTH_MASK) | | |
1078 | EXT_NOT_RECORDED_NOT_ALLOCATED; | |
1da177e4 LT |
1079 | } |
1080 | } | |
1081 | } | |
1082 | ||
1083 | static void udf_update_extents(struct inode *inode, | |
5ca4e4be | 1084 | struct kernel_long_ad laarr[EXTENT_MERGE_SIZE], |
cb00ea35 CG |
1085 | int startnum, int endnum, |
1086 | struct extent_position *epos) | |
1da177e4 LT |
1087 | { |
1088 | int start = 0, i; | |
5ca4e4be | 1089 | struct kernel_lb_addr tmploc; |
1da177e4 LT |
1090 | uint32_t tmplen; |
1091 | ||
cb00ea35 CG |
1092 | if (startnum > endnum) { |
1093 | for (i = 0; i < (startnum - endnum); i++) | |
ff116fc8 | 1094 | udf_delete_aext(inode, *epos, laarr[i].extLocation, |
cb00ea35 CG |
1095 | laarr[i].extLength); |
1096 | } else if (startnum < endnum) { | |
1097 | for (i = 0; i < (endnum - startnum); i++) { | |
ff116fc8 | 1098 | udf_insert_aext(inode, *epos, laarr[i].extLocation, |
cb00ea35 | 1099 | laarr[i].extLength); |
ff116fc8 | 1100 | udf_next_aext(inode, epos, &laarr[i].extLocation, |
cb00ea35 CG |
1101 | &laarr[i].extLength, 1); |
1102 | start++; | |
1da177e4 LT |
1103 | } |
1104 | } | |
1105 | ||
cb00ea35 | 1106 | for (i = start; i < endnum; i++) { |
ff116fc8 | 1107 | udf_next_aext(inode, epos, &tmploc, &tmplen, 0); |
97e961fd | 1108 | udf_write_aext(inode, epos, &laarr[i].extLocation, |
cb00ea35 | 1109 | laarr[i].extLength, 1); |
1da177e4 LT |
1110 | } |
1111 | } | |
1112 | ||
cb00ea35 CG |
1113 | struct buffer_head *udf_bread(struct inode *inode, int block, |
1114 | int create, int *err) | |
1da177e4 | 1115 | { |
cb00ea35 | 1116 | struct buffer_head *bh = NULL; |
1da177e4 LT |
1117 | |
1118 | bh = udf_getblk(inode, block, create, err); | |
1119 | if (!bh) | |
1120 | return NULL; | |
1121 | ||
1122 | if (buffer_uptodate(bh)) | |
1123 | return bh; | |
28de7948 | 1124 | |
1da177e4 | 1125 | ll_rw_block(READ, 1, &bh); |
28de7948 | 1126 | |
1da177e4 LT |
1127 | wait_on_buffer(bh); |
1128 | if (buffer_uptodate(bh)) | |
1129 | return bh; | |
28de7948 | 1130 | |
1da177e4 LT |
1131 | brelse(bh); |
1132 | *err = -EIO; | |
1133 | return NULL; | |
1134 | } | |
1135 | ||
7e49b6f2 | 1136 | int udf_setsize(struct inode *inode, loff_t newsize) |
1da177e4 | 1137 | { |
1da177e4 | 1138 | int err; |
48d6d8ff | 1139 | struct udf_inode_info *iinfo; |
7e49b6f2 | 1140 | int bsize = 1 << inode->i_blkbits; |
1da177e4 LT |
1141 | |
1142 | if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || | |
cb00ea35 | 1143 | S_ISLNK(inode->i_mode))) |
7e49b6f2 | 1144 | return -EINVAL; |
1da177e4 | 1145 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) |
7e49b6f2 | 1146 | return -EPERM; |
1da177e4 | 1147 | |
48d6d8ff | 1148 | iinfo = UDF_I(inode); |
7e49b6f2 | 1149 | if (newsize > inode->i_size) { |
4d0fb621 | 1150 | down_write(&iinfo->i_data_sem); |
7e49b6f2 JK |
1151 | if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) { |
1152 | if (bsize < | |
1153 | (udf_file_entry_alloc_offset(inode) + newsize)) { | |
1154 | err = udf_expand_file_adinicb(inode); | |
d2eb8c35 | 1155 | if (err) |
7e49b6f2 | 1156 | return err; |
d2eb8c35 | 1157 | down_write(&iinfo->i_data_sem); |
bb2b6d19 | 1158 | } else { |
7e49b6f2 | 1159 | iinfo->i_lenAlloc = newsize; |
bb2b6d19 IA |
1160 | goto set_size; |
1161 | } | |
7e49b6f2 JK |
1162 | } |
1163 | err = udf_extend_file(inode, newsize); | |
1164 | if (err) { | |
1165 | up_write(&iinfo->i_data_sem); | |
1166 | return err; | |
1da177e4 | 1167 | } |
bb2b6d19 | 1168 | set_size: |
7e49b6f2 | 1169 | truncate_setsize(inode, newsize); |
4d0fb621 | 1170 | up_write(&iinfo->i_data_sem); |
cb00ea35 | 1171 | } else { |
7e49b6f2 JK |
1172 | if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) { |
1173 | down_write(&iinfo->i_data_sem); | |
1174 | memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr + newsize, | |
1175 | 0x00, bsize - newsize - | |
1176 | udf_file_entry_alloc_offset(inode)); | |
1177 | iinfo->i_lenAlloc = newsize; | |
1178 | truncate_setsize(inode, newsize); | |
1179 | up_write(&iinfo->i_data_sem); | |
1180 | goto update_time; | |
1181 | } | |
1182 | err = block_truncate_page(inode->i_mapping, newsize, | |
1183 | udf_get_block); | |
1184 | if (err) | |
1185 | return err; | |
4d0fb621 | 1186 | down_write(&iinfo->i_data_sem); |
7e49b6f2 | 1187 | truncate_setsize(inode, newsize); |
1da177e4 | 1188 | udf_truncate_extents(inode); |
4d0fb621 | 1189 | up_write(&iinfo->i_data_sem); |
647bd61a | 1190 | } |
7e49b6f2 | 1191 | update_time: |
1da177e4 LT |
1192 | inode->i_mtime = inode->i_ctime = current_fs_time(inode->i_sb); |
1193 | if (IS_SYNC(inode)) | |
cb00ea35 | 1194 | udf_sync_inode(inode); |
1da177e4 LT |
1195 | else |
1196 | mark_inode_dirty(inode); | |
7e49b6f2 | 1197 | return 0; |
1da177e4 LT |
1198 | } |
1199 | ||
cb00ea35 | 1200 | static void __udf_read_inode(struct inode *inode) |
1da177e4 LT |
1201 | { |
1202 | struct buffer_head *bh = NULL; | |
1203 | struct fileEntry *fe; | |
1204 | uint16_t ident; | |
48d6d8ff | 1205 | struct udf_inode_info *iinfo = UDF_I(inode); |
1da177e4 LT |
1206 | |
1207 | /* | |
1208 | * Set defaults, but the inode is still incomplete! | |
1209 | * Note: get_new_inode() sets the following on a new inode: | |
1210 | * i_sb = sb | |
1211 | * i_no = ino | |
1212 | * i_flags = sb->s_flags | |
1213 | * i_state = 0 | |
1214 | * clean_inode(): zero fills and sets | |
1215 | * i_count = 1 | |
1216 | * i_nlink = 1 | |
1217 | * i_op = NULL; | |
1218 | */ | |
97e961fd | 1219 | bh = udf_read_ptagged(inode->i_sb, &iinfo->i_location, 0, &ident); |
cb00ea35 | 1220 | if (!bh) { |
78ace70c | 1221 | udf_err(inode->i_sb, "(ino %ld) failed !bh\n", inode->i_ino); |
1da177e4 LT |
1222 | make_bad_inode(inode); |
1223 | return; | |
1224 | } | |
1225 | ||
1226 | if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE && | |
cb00ea35 | 1227 | ident != TAG_IDENT_USE) { |
78ace70c JP |
1228 | udf_err(inode->i_sb, "(ino %ld) failed ident=%d\n", |
1229 | inode->i_ino, ident); | |
3bf25cb4 | 1230 | brelse(bh); |
1da177e4 LT |
1231 | make_bad_inode(inode); |
1232 | return; | |
1233 | } | |
1234 | ||
1235 | fe = (struct fileEntry *)bh->b_data; | |
1236 | ||
5e0f0017 | 1237 | if (fe->icbTag.strategyType == cpu_to_le16(4096)) { |
1ab92785 | 1238 | struct buffer_head *ibh; |
1da177e4 | 1239 | |
97e961fd | 1240 | ibh = udf_read_ptagged(inode->i_sb, &iinfo->i_location, 1, |
4b11111a | 1241 | &ident); |
1ab92785 | 1242 | if (ident == TAG_IDENT_IE && ibh) { |
1243 | struct buffer_head *nbh = NULL; | |
5ca4e4be | 1244 | struct kernel_lb_addr loc; |
1ab92785 | 1245 | struct indirectEntry *ie; |
1246 | ||
1247 | ie = (struct indirectEntry *)ibh->b_data; | |
1248 | loc = lelb_to_cpu(ie->indirectICB.extLocation); | |
1249 | ||
1250 | if (ie->indirectICB.extLength && | |
97e961fd | 1251 | (nbh = udf_read_ptagged(inode->i_sb, &loc, 0, |
1ab92785 | 1252 | &ident))) { |
1253 | if (ident == TAG_IDENT_FE || | |
1254 | ident == TAG_IDENT_EFE) { | |
1255 | memcpy(&iinfo->i_location, | |
1256 | &loc, | |
5ca4e4be | 1257 | sizeof(struct kernel_lb_addr)); |
1ab92785 | 1258 | brelse(bh); |
3bf25cb4 | 1259 | brelse(ibh); |
1ab92785 | 1260 | brelse(nbh); |
1261 | __udf_read_inode(inode); | |
1262 | return; | |
28de7948 | 1263 | } |
1ab92785 | 1264 | brelse(nbh); |
1da177e4 | 1265 | } |
28de7948 | 1266 | } |
1ab92785 | 1267 | brelse(ibh); |
5e0f0017 | 1268 | } else if (fe->icbTag.strategyType != cpu_to_le16(4)) { |
78ace70c JP |
1269 | udf_err(inode->i_sb, "unsupported strategy type: %d\n", |
1270 | le16_to_cpu(fe->icbTag.strategyType)); | |
3bf25cb4 | 1271 | brelse(bh); |
1da177e4 LT |
1272 | make_bad_inode(inode); |
1273 | return; | |
1274 | } | |
1275 | udf_fill_inode(inode, bh); | |
31170b6a | 1276 | |
3bf25cb4 | 1277 | brelse(bh); |
1da177e4 LT |
1278 | } |
1279 | ||
1280 | static void udf_fill_inode(struct inode *inode, struct buffer_head *bh) | |
1281 | { | |
1282 | struct fileEntry *fe; | |
1283 | struct extendedFileEntry *efe; | |
6c79e987 | 1284 | struct udf_sb_info *sbi = UDF_SB(inode->i_sb); |
48d6d8ff | 1285 | struct udf_inode_info *iinfo = UDF_I(inode); |
bfe86848 | 1286 | unsigned int link_count; |
1da177e4 LT |
1287 | |
1288 | fe = (struct fileEntry *)bh->b_data; | |
1289 | efe = (struct extendedFileEntry *)bh->b_data; | |
1290 | ||
5e0f0017 | 1291 | if (fe->icbTag.strategyType == cpu_to_le16(4)) |
48d6d8ff | 1292 | iinfo->i_strat4096 = 0; |
5e0f0017 | 1293 | else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */ |
48d6d8ff | 1294 | iinfo->i_strat4096 = 1; |
1da177e4 | 1295 | |
48d6d8ff | 1296 | iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) & |
4b11111a | 1297 | ICBTAG_FLAG_AD_MASK; |
48d6d8ff MS |
1298 | iinfo->i_unique = 0; |
1299 | iinfo->i_lenEAttr = 0; | |
1300 | iinfo->i_lenExtents = 0; | |
1301 | iinfo->i_lenAlloc = 0; | |
1302 | iinfo->i_next_alloc_block = 0; | |
1303 | iinfo->i_next_alloc_goal = 0; | |
5e0f0017 | 1304 | if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) { |
48d6d8ff MS |
1305 | iinfo->i_efe = 1; |
1306 | iinfo->i_use = 0; | |
4b11111a MS |
1307 | if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize - |
1308 | sizeof(struct extendedFileEntry))) { | |
647bd61a CG |
1309 | make_bad_inode(inode); |
1310 | return; | |
1311 | } | |
48d6d8ff | 1312 | memcpy(iinfo->i_ext.i_data, |
4b11111a MS |
1313 | bh->b_data + sizeof(struct extendedFileEntry), |
1314 | inode->i_sb->s_blocksize - | |
1315 | sizeof(struct extendedFileEntry)); | |
5e0f0017 | 1316 | } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) { |
48d6d8ff MS |
1317 | iinfo->i_efe = 0; |
1318 | iinfo->i_use = 0; | |
4b11111a MS |
1319 | if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize - |
1320 | sizeof(struct fileEntry))) { | |
647bd61a CG |
1321 | make_bad_inode(inode); |
1322 | return; | |
1323 | } | |
48d6d8ff | 1324 | memcpy(iinfo->i_ext.i_data, |
c0b34438 | 1325 | bh->b_data + sizeof(struct fileEntry), |
cb00ea35 | 1326 | inode->i_sb->s_blocksize - sizeof(struct fileEntry)); |
5e0f0017 | 1327 | } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) { |
48d6d8ff MS |
1328 | iinfo->i_efe = 0; |
1329 | iinfo->i_use = 1; | |
1330 | iinfo->i_lenAlloc = le32_to_cpu( | |
4b11111a MS |
1331 | ((struct unallocSpaceEntry *)bh->b_data)-> |
1332 | lengthAllocDescs); | |
1333 | if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize - | |
1334 | sizeof(struct unallocSpaceEntry))) { | |
647bd61a CG |
1335 | make_bad_inode(inode); |
1336 | return; | |
1337 | } | |
48d6d8ff | 1338 | memcpy(iinfo->i_ext.i_data, |
4b11111a MS |
1339 | bh->b_data + sizeof(struct unallocSpaceEntry), |
1340 | inode->i_sb->s_blocksize - | |
1341 | sizeof(struct unallocSpaceEntry)); | |
1da177e4 LT |
1342 | return; |
1343 | } | |
1344 | ||
c03cad24 | 1345 | read_lock(&sbi->s_cred_lock); |
c2ba138a EB |
1346 | i_uid_write(inode, le32_to_cpu(fe->uid)); |
1347 | if (!uid_valid(inode->i_uid) || | |
ca76d2d8 CG |
1348 | UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_IGNORE) || |
1349 | UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET)) | |
4d6660eb | 1350 | inode->i_uid = UDF_SB(inode->i_sb)->s_uid; |
1da177e4 | 1351 | |
c2ba138a EB |
1352 | i_gid_write(inode, le32_to_cpu(fe->gid)); |
1353 | if (!gid_valid(inode->i_gid) || | |
ca76d2d8 CG |
1354 | UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_IGNORE) || |
1355 | UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET)) | |
4d6660eb | 1356 | inode->i_gid = UDF_SB(inode->i_sb)->s_gid; |
1da177e4 | 1357 | |
7ac9bcd5 | 1358 | if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY && |
87bc730c | 1359 | sbi->s_fmode != UDF_INVALID_MODE) |
7ac9bcd5 MS |
1360 | inode->i_mode = sbi->s_fmode; |
1361 | else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY && | |
87bc730c | 1362 | sbi->s_dmode != UDF_INVALID_MODE) |
7ac9bcd5 MS |
1363 | inode->i_mode = sbi->s_dmode; |
1364 | else | |
1365 | inode->i_mode = udf_convert_permissions(fe); | |
1366 | inode->i_mode &= ~sbi->s_umask; | |
c03cad24 JK |
1367 | read_unlock(&sbi->s_cred_lock); |
1368 | ||
bfe86848 MS |
1369 | link_count = le16_to_cpu(fe->fileLinkCount); |
1370 | if (!link_count) | |
1371 | link_count = 1; | |
1372 | set_nlink(inode, link_count); | |
c03cad24 JK |
1373 | |
1374 | inode->i_size = le64_to_cpu(fe->informationLength); | |
1375 | iinfo->i_lenExtents = inode->i_size; | |
1da177e4 | 1376 | |
48d6d8ff | 1377 | if (iinfo->i_efe == 0) { |
1da177e4 | 1378 | inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) << |
28de7948 | 1379 | (inode->i_sb->s_blocksize_bits - 9); |
1da177e4 | 1380 | |
56774805 | 1381 | if (!udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime)) |
cbf5676a | 1382 | inode->i_atime = sbi->s_record_time; |
1383 | ||
56774805 MS |
1384 | if (!udf_disk_stamp_to_time(&inode->i_mtime, |
1385 | fe->modificationTime)) | |
cbf5676a | 1386 | inode->i_mtime = sbi->s_record_time; |
1387 | ||
56774805 | 1388 | if (!udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime)) |
cbf5676a | 1389 | inode->i_ctime = sbi->s_record_time; |
1da177e4 | 1390 | |
48d6d8ff MS |
1391 | iinfo->i_unique = le64_to_cpu(fe->uniqueID); |
1392 | iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr); | |
1393 | iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs); | |
d5e2cf07 | 1394 | iinfo->i_checkpoint = le32_to_cpu(fe->checkpoint); |
cb00ea35 | 1395 | } else { |
647bd61a | 1396 | inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) << |
cb00ea35 | 1397 | (inode->i_sb->s_blocksize_bits - 9); |
1da177e4 | 1398 | |
56774805 | 1399 | if (!udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime)) |
cbf5676a | 1400 | inode->i_atime = sbi->s_record_time; |
1401 | ||
56774805 MS |
1402 | if (!udf_disk_stamp_to_time(&inode->i_mtime, |
1403 | efe->modificationTime)) | |
cbf5676a | 1404 | inode->i_mtime = sbi->s_record_time; |
1405 | ||
56774805 | 1406 | if (!udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime)) |
cbf5676a | 1407 | iinfo->i_crtime = sbi->s_record_time; |
1408 | ||
56774805 | 1409 | if (!udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime)) |
cbf5676a | 1410 | inode->i_ctime = sbi->s_record_time; |
1da177e4 | 1411 | |
48d6d8ff MS |
1412 | iinfo->i_unique = le64_to_cpu(efe->uniqueID); |
1413 | iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr); | |
1414 | iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs); | |
d5e2cf07 | 1415 | iinfo->i_checkpoint = le32_to_cpu(efe->checkpoint); |
1da177e4 LT |
1416 | } |
1417 | ||
cb00ea35 CG |
1418 | switch (fe->icbTag.fileType) { |
1419 | case ICBTAG_FILE_TYPE_DIRECTORY: | |
28de7948 CG |
1420 | inode->i_op = &udf_dir_inode_operations; |
1421 | inode->i_fop = &udf_dir_operations; | |
1422 | inode->i_mode |= S_IFDIR; | |
1423 | inc_nlink(inode); | |
1424 | break; | |
cb00ea35 CG |
1425 | case ICBTAG_FILE_TYPE_REALTIME: |
1426 | case ICBTAG_FILE_TYPE_REGULAR: | |
1427 | case ICBTAG_FILE_TYPE_UNDEF: | |
742e1795 | 1428 | case ICBTAG_FILE_TYPE_VAT20: |
48d6d8ff | 1429 | if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) |
28de7948 CG |
1430 | inode->i_data.a_ops = &udf_adinicb_aops; |
1431 | else | |
1432 | inode->i_data.a_ops = &udf_aops; | |
1433 | inode->i_op = &udf_file_inode_operations; | |
1434 | inode->i_fop = &udf_file_operations; | |
1435 | inode->i_mode |= S_IFREG; | |
1436 | break; | |
cb00ea35 | 1437 | case ICBTAG_FILE_TYPE_BLOCK: |
28de7948 CG |
1438 | inode->i_mode |= S_IFBLK; |
1439 | break; | |
cb00ea35 | 1440 | case ICBTAG_FILE_TYPE_CHAR: |
28de7948 CG |
1441 | inode->i_mode |= S_IFCHR; |
1442 | break; | |
cb00ea35 | 1443 | case ICBTAG_FILE_TYPE_FIFO: |
28de7948 CG |
1444 | init_special_inode(inode, inode->i_mode | S_IFIFO, 0); |
1445 | break; | |
cb00ea35 | 1446 | case ICBTAG_FILE_TYPE_SOCKET: |
28de7948 CG |
1447 | init_special_inode(inode, inode->i_mode | S_IFSOCK, 0); |
1448 | break; | |
cb00ea35 | 1449 | case ICBTAG_FILE_TYPE_SYMLINK: |
28de7948 | 1450 | inode->i_data.a_ops = &udf_symlink_aops; |
c15d0fc0 | 1451 | inode->i_op = &udf_symlink_inode_operations; |
28de7948 CG |
1452 | inode->i_mode = S_IFLNK | S_IRWXUGO; |
1453 | break; | |
bfb257a5 JK |
1454 | case ICBTAG_FILE_TYPE_MAIN: |
1455 | udf_debug("METADATA FILE-----\n"); | |
1456 | break; | |
1457 | case ICBTAG_FILE_TYPE_MIRROR: | |
1458 | udf_debug("METADATA MIRROR FILE-----\n"); | |
1459 | break; | |
1460 | case ICBTAG_FILE_TYPE_BITMAP: | |
1461 | udf_debug("METADATA BITMAP FILE-----\n"); | |
1462 | break; | |
cb00ea35 | 1463 | default: |
78ace70c JP |
1464 | udf_err(inode->i_sb, "(ino %ld) failed unknown file type=%d\n", |
1465 | inode->i_ino, fe->icbTag.fileType); | |
28de7948 CG |
1466 | make_bad_inode(inode); |
1467 | return; | |
1da177e4 | 1468 | } |
cb00ea35 | 1469 | if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { |
4b11111a MS |
1470 | struct deviceSpec *dsea = |
1471 | (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1); | |
cb00ea35 CG |
1472 | if (dsea) { |
1473 | init_special_inode(inode, inode->i_mode, | |
4b11111a MS |
1474 | MKDEV(le32_to_cpu(dsea->majorDeviceIdent), |
1475 | le32_to_cpu(dsea->minorDeviceIdent))); | |
1da177e4 | 1476 | /* Developer ID ??? */ |
4b11111a | 1477 | } else |
1da177e4 | 1478 | make_bad_inode(inode); |
1da177e4 LT |
1479 | } |
1480 | } | |
1481 | ||
647bd61a CG |
1482 | static int udf_alloc_i_data(struct inode *inode, size_t size) |
1483 | { | |
48d6d8ff MS |
1484 | struct udf_inode_info *iinfo = UDF_I(inode); |
1485 | iinfo->i_ext.i_data = kmalloc(size, GFP_KERNEL); | |
647bd61a | 1486 | |
48d6d8ff | 1487 | if (!iinfo->i_ext.i_data) { |
78ace70c JP |
1488 | udf_err(inode->i_sb, "(ino %ld) no free memory\n", |
1489 | inode->i_ino); | |
647bd61a CG |
1490 | return -ENOMEM; |
1491 | } | |
1492 | ||
1493 | return 0; | |
1494 | } | |
1495 | ||
faa17292 | 1496 | static umode_t udf_convert_permissions(struct fileEntry *fe) |
1da177e4 | 1497 | { |
faa17292 | 1498 | umode_t mode; |
1da177e4 LT |
1499 | uint32_t permissions; |
1500 | uint32_t flags; | |
1501 | ||
1502 | permissions = le32_to_cpu(fe->permissions); | |
1503 | flags = le16_to_cpu(fe->icbTag.flags); | |
1504 | ||
4b11111a MS |
1505 | mode = ((permissions) & S_IRWXO) | |
1506 | ((permissions >> 2) & S_IRWXG) | | |
1507 | ((permissions >> 4) & S_IRWXU) | | |
1508 | ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) | | |
1509 | ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) | | |
1510 | ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0); | |
1da177e4 LT |
1511 | |
1512 | return mode; | |
1513 | } | |
1514 | ||
a9185b41 | 1515 | int udf_write_inode(struct inode *inode, struct writeback_control *wbc) |
1da177e4 | 1516 | { |
49521de1 | 1517 | return udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL); |
1da177e4 LT |
1518 | } |
1519 | ||
49521de1 | 1520 | static int udf_sync_inode(struct inode *inode) |
1da177e4 LT |
1521 | { |
1522 | return udf_update_inode(inode, 1); | |
1523 | } | |
1524 | ||
cb00ea35 | 1525 | static int udf_update_inode(struct inode *inode, int do_sync) |
1da177e4 LT |
1526 | { |
1527 | struct buffer_head *bh = NULL; | |
1528 | struct fileEntry *fe; | |
1529 | struct extendedFileEntry *efe; | |
b2527bfa | 1530 | uint64_t lb_recorded; |
1da177e4 LT |
1531 | uint32_t udfperms; |
1532 | uint16_t icbflags; | |
1533 | uint16_t crclen; | |
1da177e4 | 1534 | int err = 0; |
6c79e987 | 1535 | struct udf_sb_info *sbi = UDF_SB(inode->i_sb); |
4b11111a | 1536 | unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits; |
48d6d8ff | 1537 | struct udf_inode_info *iinfo = UDF_I(inode); |
1da177e4 | 1538 | |
5833ded9 JK |
1539 | bh = udf_tgetblk(inode->i_sb, |
1540 | udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0)); | |
cb00ea35 | 1541 | if (!bh) { |
aae917cd JK |
1542 | udf_debug("getblk failure\n"); |
1543 | return -ENOMEM; | |
1da177e4 LT |
1544 | } |
1545 | ||
aae917cd JK |
1546 | lock_buffer(bh); |
1547 | memset(bh->b_data, 0, inode->i_sb->s_blocksize); | |
1da177e4 LT |
1548 | fe = (struct fileEntry *)bh->b_data; |
1549 | efe = (struct extendedFileEntry *)bh->b_data; | |
1550 | ||
aae917cd | 1551 | if (iinfo->i_use) { |
1da177e4 | 1552 | struct unallocSpaceEntry *use = |
28de7948 | 1553 | (struct unallocSpaceEntry *)bh->b_data; |
1da177e4 | 1554 | |
48d6d8ff | 1555 | use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc); |
4b11111a | 1556 | memcpy(bh->b_data + sizeof(struct unallocSpaceEntry), |
48d6d8ff | 1557 | iinfo->i_ext.i_data, inode->i_sb->s_blocksize - |
4b11111a | 1558 | sizeof(struct unallocSpaceEntry)); |
aae917cd JK |
1559 | use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE); |
1560 | use->descTag.tagLocation = | |
1561 | cpu_to_le32(iinfo->i_location.logicalBlockNum); | |
4b11111a | 1562 | crclen = sizeof(struct unallocSpaceEntry) + |
5ca4e4be | 1563 | iinfo->i_lenAlloc - sizeof(struct tag); |
1da177e4 | 1564 | use->descTag.descCRCLength = cpu_to_le16(crclen); |
f845fced | 1565 | use->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)use + |
5ca4e4be | 1566 | sizeof(struct tag), |
f845fced | 1567 | crclen)); |
3f2587bb | 1568 | use->descTag.tagChecksum = udf_tag_checksum(&use->descTag); |
1da177e4 | 1569 | |
aae917cd | 1570 | goto out; |
1da177e4 LT |
1571 | } |
1572 | ||
4d6660eb PS |
1573 | if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET)) |
1574 | fe->uid = cpu_to_le32(-1); | |
cb00ea35 | 1575 | else |
c2ba138a | 1576 | fe->uid = cpu_to_le32(i_uid_read(inode)); |
1da177e4 | 1577 | |
4d6660eb PS |
1578 | if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET)) |
1579 | fe->gid = cpu_to_le32(-1); | |
cb00ea35 | 1580 | else |
c2ba138a | 1581 | fe->gid = cpu_to_le32(i_gid_read(inode)); |
1da177e4 | 1582 | |
4b11111a MS |
1583 | udfperms = ((inode->i_mode & S_IRWXO)) | |
1584 | ((inode->i_mode & S_IRWXG) << 2) | | |
1585 | ((inode->i_mode & S_IRWXU) << 4); | |
1da177e4 | 1586 | |
4b11111a MS |
1587 | udfperms |= (le32_to_cpu(fe->permissions) & |
1588 | (FE_PERM_O_DELETE | FE_PERM_O_CHATTR | | |
1589 | FE_PERM_G_DELETE | FE_PERM_G_CHATTR | | |
1590 | FE_PERM_U_DELETE | FE_PERM_U_CHATTR)); | |
1da177e4 LT |
1591 | fe->permissions = cpu_to_le32(udfperms); |
1592 | ||
1593 | if (S_ISDIR(inode->i_mode)) | |
1594 | fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1); | |
1595 | else | |
1596 | fe->fileLinkCount = cpu_to_le16(inode->i_nlink); | |
1597 | ||
1598 | fe->informationLength = cpu_to_le64(inode->i_size); | |
1599 | ||
cb00ea35 | 1600 | if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { |
5ca4e4be | 1601 | struct regid *eid; |
28de7948 CG |
1602 | struct deviceSpec *dsea = |
1603 | (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1); | |
cb00ea35 | 1604 | if (!dsea) { |
1da177e4 | 1605 | dsea = (struct deviceSpec *) |
28de7948 CG |
1606 | udf_add_extendedattr(inode, |
1607 | sizeof(struct deviceSpec) + | |
5ca4e4be | 1608 | sizeof(struct regid), 12, 0x3); |
1da177e4 LT |
1609 | dsea->attrType = cpu_to_le32(12); |
1610 | dsea->attrSubtype = 1; | |
4b11111a MS |
1611 | dsea->attrLength = cpu_to_le32( |
1612 | sizeof(struct deviceSpec) + | |
5ca4e4be PE |
1613 | sizeof(struct regid)); |
1614 | dsea->impUseLength = cpu_to_le32(sizeof(struct regid)); | |
1da177e4 | 1615 | } |
5ca4e4be PE |
1616 | eid = (struct regid *)dsea->impUse; |
1617 | memset(eid, 0, sizeof(struct regid)); | |
1da177e4 LT |
1618 | strcpy(eid->ident, UDF_ID_DEVELOPER); |
1619 | eid->identSuffix[0] = UDF_OS_CLASS_UNIX; | |
1620 | eid->identSuffix[1] = UDF_OS_ID_LINUX; | |
1621 | dsea->majorDeviceIdent = cpu_to_le32(imajor(inode)); | |
1622 | dsea->minorDeviceIdent = cpu_to_le32(iminor(inode)); | |
1623 | } | |
1624 | ||
b2527bfa SN |
1625 | if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) |
1626 | lb_recorded = 0; /* No extents => no blocks! */ | |
1627 | else | |
1628 | lb_recorded = | |
1629 | (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >> | |
1630 | (blocksize_bits - 9); | |
1631 | ||
48d6d8ff | 1632 | if (iinfo->i_efe == 0) { |
c0b34438 | 1633 | memcpy(bh->b_data + sizeof(struct fileEntry), |
48d6d8ff | 1634 | iinfo->i_ext.i_data, |
cb00ea35 | 1635 | inode->i_sb->s_blocksize - sizeof(struct fileEntry)); |
b2527bfa | 1636 | fe->logicalBlocksRecorded = cpu_to_le64(lb_recorded); |
1da177e4 | 1637 | |
56774805 MS |
1638 | udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime); |
1639 | udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime); | |
1640 | udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime); | |
5ca4e4be | 1641 | memset(&(fe->impIdent), 0, sizeof(struct regid)); |
1da177e4 LT |
1642 | strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER); |
1643 | fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX; | |
1644 | fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX; | |
48d6d8ff MS |
1645 | fe->uniqueID = cpu_to_le64(iinfo->i_unique); |
1646 | fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr); | |
1647 | fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc); | |
d5e2cf07 | 1648 | fe->checkpoint = cpu_to_le32(iinfo->i_checkpoint); |
1da177e4 LT |
1649 | fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE); |
1650 | crclen = sizeof(struct fileEntry); | |
cb00ea35 | 1651 | } else { |
4b11111a | 1652 | memcpy(bh->b_data + sizeof(struct extendedFileEntry), |
48d6d8ff | 1653 | iinfo->i_ext.i_data, |
4b11111a MS |
1654 | inode->i_sb->s_blocksize - |
1655 | sizeof(struct extendedFileEntry)); | |
1da177e4 | 1656 | efe->objectSize = cpu_to_le64(inode->i_size); |
b2527bfa | 1657 | efe->logicalBlocksRecorded = cpu_to_le64(lb_recorded); |
1da177e4 | 1658 | |
48d6d8ff MS |
1659 | if (iinfo->i_crtime.tv_sec > inode->i_atime.tv_sec || |
1660 | (iinfo->i_crtime.tv_sec == inode->i_atime.tv_sec && | |
1661 | iinfo->i_crtime.tv_nsec > inode->i_atime.tv_nsec)) | |
1662 | iinfo->i_crtime = inode->i_atime; | |
4b11111a | 1663 | |
48d6d8ff MS |
1664 | if (iinfo->i_crtime.tv_sec > inode->i_mtime.tv_sec || |
1665 | (iinfo->i_crtime.tv_sec == inode->i_mtime.tv_sec && | |
1666 | iinfo->i_crtime.tv_nsec > inode->i_mtime.tv_nsec)) | |
1667 | iinfo->i_crtime = inode->i_mtime; | |
4b11111a | 1668 | |
48d6d8ff MS |
1669 | if (iinfo->i_crtime.tv_sec > inode->i_ctime.tv_sec || |
1670 | (iinfo->i_crtime.tv_sec == inode->i_ctime.tv_sec && | |
1671 | iinfo->i_crtime.tv_nsec > inode->i_ctime.tv_nsec)) | |
1672 | iinfo->i_crtime = inode->i_ctime; | |
1da177e4 | 1673 | |
56774805 MS |
1674 | udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime); |
1675 | udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime); | |
1676 | udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime); | |
1677 | udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime); | |
1da177e4 | 1678 | |
5ca4e4be | 1679 | memset(&(efe->impIdent), 0, sizeof(struct regid)); |
1da177e4 LT |
1680 | strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER); |
1681 | efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX; | |
1682 | efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX; | |
48d6d8ff MS |
1683 | efe->uniqueID = cpu_to_le64(iinfo->i_unique); |
1684 | efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr); | |
1685 | efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc); | |
d5e2cf07 | 1686 | efe->checkpoint = cpu_to_le32(iinfo->i_checkpoint); |
1da177e4 LT |
1687 | efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE); |
1688 | crclen = sizeof(struct extendedFileEntry); | |
1689 | } | |
48d6d8ff | 1690 | if (iinfo->i_strat4096) { |
1da177e4 LT |
1691 | fe->icbTag.strategyType = cpu_to_le16(4096); |
1692 | fe->icbTag.strategyParameter = cpu_to_le16(1); | |
1693 | fe->icbTag.numEntries = cpu_to_le16(2); | |
cb00ea35 | 1694 | } else { |
1da177e4 LT |
1695 | fe->icbTag.strategyType = cpu_to_le16(4); |
1696 | fe->icbTag.numEntries = cpu_to_le16(1); | |
1697 | } | |
1698 | ||
1699 | if (S_ISDIR(inode->i_mode)) | |
1700 | fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY; | |
1701 | else if (S_ISREG(inode->i_mode)) | |
1702 | fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR; | |
1703 | else if (S_ISLNK(inode->i_mode)) | |
1704 | fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK; | |
1705 | else if (S_ISBLK(inode->i_mode)) | |
1706 | fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK; | |
1707 | else if (S_ISCHR(inode->i_mode)) | |
1708 | fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR; | |
1709 | else if (S_ISFIFO(inode->i_mode)) | |
1710 | fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO; | |
1711 | else if (S_ISSOCK(inode->i_mode)) | |
1712 | fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET; | |
1713 | ||
48d6d8ff | 1714 | icbflags = iinfo->i_alloc_type | |
28de7948 CG |
1715 | ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) | |
1716 | ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) | | |
1717 | ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) | | |
1718 | (le16_to_cpu(fe->icbTag.flags) & | |
1719 | ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID | | |
1720 | ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY)); | |
1da177e4 LT |
1721 | |
1722 | fe->icbTag.flags = cpu_to_le16(icbflags); | |
6c79e987 | 1723 | if (sbi->s_udfrev >= 0x0200) |
1da177e4 LT |
1724 | fe->descTag.descVersion = cpu_to_le16(3); |
1725 | else | |
1726 | fe->descTag.descVersion = cpu_to_le16(2); | |
6c79e987 | 1727 | fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number); |
4b11111a | 1728 | fe->descTag.tagLocation = cpu_to_le32( |
48d6d8ff | 1729 | iinfo->i_location.logicalBlockNum); |
aae917cd | 1730 | crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag); |
1da177e4 | 1731 | fe->descTag.descCRCLength = cpu_to_le16(crclen); |
5ca4e4be | 1732 | fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag), |
f845fced | 1733 | crclen)); |
3f2587bb | 1734 | fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag); |
1da177e4 | 1735 | |
aae917cd | 1736 | out: |
5833ded9 | 1737 | set_buffer_uptodate(bh); |
aae917cd JK |
1738 | unlock_buffer(bh); |
1739 | ||
1da177e4 LT |
1740 | /* write the data blocks */ |
1741 | mark_buffer_dirty(bh); | |
cb00ea35 | 1742 | if (do_sync) { |
1da177e4 | 1743 | sync_dirty_buffer(bh); |
aae917cd | 1744 | if (buffer_write_io_error(bh)) { |
78ace70c JP |
1745 | udf_warn(inode->i_sb, "IO error syncing udf inode [%08lx]\n", |
1746 | inode->i_ino); | |
1da177e4 LT |
1747 | err = -EIO; |
1748 | } | |
1749 | } | |
3bf25cb4 | 1750 | brelse(bh); |
28de7948 | 1751 | |
1da177e4 LT |
1752 | return err; |
1753 | } | |
1754 | ||
97e961fd | 1755 | struct inode *udf_iget(struct super_block *sb, struct kernel_lb_addr *ino) |
1da177e4 LT |
1756 | { |
1757 | unsigned long block = udf_get_lb_pblock(sb, ino, 0); | |
1758 | struct inode *inode = iget_locked(sb, block); | |
1759 | ||
1760 | if (!inode) | |
1761 | return NULL; | |
1762 | ||
1763 | if (inode->i_state & I_NEW) { | |
97e961fd | 1764 | memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr)); |
1da177e4 LT |
1765 | __udf_read_inode(inode); |
1766 | unlock_new_inode(inode); | |
1767 | } | |
1768 | ||
1769 | if (is_bad_inode(inode)) | |
1770 | goto out_iput; | |
1771 | ||
97e961fd PE |
1772 | if (ino->logicalBlockNum >= UDF_SB(sb)-> |
1773 | s_partmaps[ino->partitionReferenceNum].s_partition_len) { | |
1da177e4 | 1774 | udf_debug("block=%d, partition=%d out of range\n", |
97e961fd | 1775 | ino->logicalBlockNum, ino->partitionReferenceNum); |
1da177e4 LT |
1776 | make_bad_inode(inode); |
1777 | goto out_iput; | |
1778 | } | |
1779 | ||
1780 | return inode; | |
1781 | ||
28de7948 | 1782 | out_iput: |
1da177e4 LT |
1783 | iput(inode); |
1784 | return NULL; | |
1785 | } | |
1786 | ||
7e49b6f2 JK |
1787 | int udf_add_aext(struct inode *inode, struct extent_position *epos, |
1788 | struct kernel_lb_addr *eloc, uint32_t elen, int inc) | |
1da177e4 LT |
1789 | { |
1790 | int adsize; | |
5ca4e4be PE |
1791 | struct short_ad *sad = NULL; |
1792 | struct long_ad *lad = NULL; | |
1da177e4 | 1793 | struct allocExtDesc *aed; |
1da177e4 | 1794 | uint8_t *ptr; |
48d6d8ff | 1795 | struct udf_inode_info *iinfo = UDF_I(inode); |
1da177e4 | 1796 | |
ff116fc8 | 1797 | if (!epos->bh) |
48d6d8ff | 1798 | ptr = iinfo->i_ext.i_data + epos->offset - |
4b11111a | 1799 | udf_file_entry_alloc_offset(inode) + |
48d6d8ff | 1800 | iinfo->i_lenEAttr; |
1da177e4 | 1801 | else |
ff116fc8 | 1802 | ptr = epos->bh->b_data + epos->offset; |
1da177e4 | 1803 | |
48d6d8ff | 1804 | if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT) |
5ca4e4be | 1805 | adsize = sizeof(struct short_ad); |
48d6d8ff | 1806 | else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG) |
5ca4e4be | 1807 | adsize = sizeof(struct long_ad); |
1da177e4 | 1808 | else |
7e49b6f2 | 1809 | return -EIO; |
1da177e4 | 1810 | |
cb00ea35 | 1811 | if (epos->offset + (2 * adsize) > inode->i_sb->s_blocksize) { |
391e8bbd | 1812 | unsigned char *sptr, *dptr; |
1da177e4 LT |
1813 | struct buffer_head *nbh; |
1814 | int err, loffset; | |
5ca4e4be | 1815 | struct kernel_lb_addr obloc = epos->block; |
1da177e4 | 1816 | |
4b11111a MS |
1817 | epos->block.logicalBlockNum = udf_new_block(inode->i_sb, NULL, |
1818 | obloc.partitionReferenceNum, | |
1819 | obloc.logicalBlockNum, &err); | |
1820 | if (!epos->block.logicalBlockNum) | |
7e49b6f2 | 1821 | return -ENOSPC; |
4b11111a | 1822 | nbh = udf_tgetblk(inode->i_sb, udf_get_lb_pblock(inode->i_sb, |
97e961fd | 1823 | &epos->block, |
4b11111a MS |
1824 | 0)); |
1825 | if (!nbh) | |
7e49b6f2 | 1826 | return -EIO; |
1da177e4 LT |
1827 | lock_buffer(nbh); |
1828 | memset(nbh->b_data, 0x00, inode->i_sb->s_blocksize); | |
1829 | set_buffer_uptodate(nbh); | |
1830 | unlock_buffer(nbh); | |
1831 | mark_buffer_dirty_inode(nbh, inode); | |
1832 | ||
1833 | aed = (struct allocExtDesc *)(nbh->b_data); | |
1834 | if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT)) | |
4b11111a MS |
1835 | aed->previousAllocExtLocation = |
1836 | cpu_to_le32(obloc.logicalBlockNum); | |
cb00ea35 | 1837 | if (epos->offset + adsize > inode->i_sb->s_blocksize) { |
ff116fc8 | 1838 | loffset = epos->offset; |
1da177e4 LT |
1839 | aed->lengthAllocDescs = cpu_to_le32(adsize); |
1840 | sptr = ptr - adsize; | |
1841 | dptr = nbh->b_data + sizeof(struct allocExtDesc); | |
1842 | memcpy(dptr, sptr, adsize); | |
ff116fc8 | 1843 | epos->offset = sizeof(struct allocExtDesc) + adsize; |
cb00ea35 | 1844 | } else { |
ff116fc8 | 1845 | loffset = epos->offset + adsize; |
1da177e4 LT |
1846 | aed->lengthAllocDescs = cpu_to_le32(0); |
1847 | sptr = ptr; | |
ff116fc8 | 1848 | epos->offset = sizeof(struct allocExtDesc); |
1da177e4 | 1849 | |
cb00ea35 | 1850 | if (epos->bh) { |
ff116fc8 | 1851 | aed = (struct allocExtDesc *)epos->bh->b_data; |
c2104fda | 1852 | le32_add_cpu(&aed->lengthAllocDescs, adsize); |
cb00ea35 | 1853 | } else { |
48d6d8ff | 1854 | iinfo->i_lenAlloc += adsize; |
1da177e4 LT |
1855 | mark_inode_dirty(inode); |
1856 | } | |
1857 | } | |
6c79e987 | 1858 | if (UDF_SB(inode->i_sb)->s_udfrev >= 0x0200) |
1da177e4 | 1859 | udf_new_tag(nbh->b_data, TAG_IDENT_AED, 3, 1, |
5ca4e4be | 1860 | epos->block.logicalBlockNum, sizeof(struct tag)); |
1da177e4 LT |
1861 | else |
1862 | udf_new_tag(nbh->b_data, TAG_IDENT_AED, 2, 1, | |
5ca4e4be | 1863 | epos->block.logicalBlockNum, sizeof(struct tag)); |
48d6d8ff | 1864 | switch (iinfo->i_alloc_type) { |
cb00ea35 | 1865 | case ICBTAG_FLAG_AD_SHORT: |
5ca4e4be | 1866 | sad = (struct short_ad *)sptr; |
28de7948 CG |
1867 | sad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS | |
1868 | inode->i_sb->s_blocksize); | |
4b11111a MS |
1869 | sad->extPosition = |
1870 | cpu_to_le32(epos->block.logicalBlockNum); | |
28de7948 | 1871 | break; |
cb00ea35 | 1872 | case ICBTAG_FLAG_AD_LONG: |
5ca4e4be | 1873 | lad = (struct long_ad *)sptr; |
28de7948 CG |
1874 | lad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS | |
1875 | inode->i_sb->s_blocksize); | |
1876 | lad->extLocation = cpu_to_lelb(epos->block); | |
1877 | memset(lad->impUse, 0x00, sizeof(lad->impUse)); | |
1878 | break; | |
1da177e4 | 1879 | } |
cb00ea35 | 1880 | if (epos->bh) { |
28de7948 | 1881 | if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) || |
6c79e987 | 1882 | UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) |
ff116fc8 | 1883 | udf_update_tag(epos->bh->b_data, loffset); |
1da177e4 | 1884 | else |
4b11111a MS |
1885 | udf_update_tag(epos->bh->b_data, |
1886 | sizeof(struct allocExtDesc)); | |
ff116fc8 | 1887 | mark_buffer_dirty_inode(epos->bh, inode); |
3bf25cb4 | 1888 | brelse(epos->bh); |
28de7948 | 1889 | } else { |
1da177e4 | 1890 | mark_inode_dirty(inode); |
28de7948 | 1891 | } |
ff116fc8 | 1892 | epos->bh = nbh; |
1da177e4 LT |
1893 | } |
1894 | ||
7e49b6f2 | 1895 | udf_write_aext(inode, epos, eloc, elen, inc); |
1da177e4 | 1896 | |
cb00ea35 | 1897 | if (!epos->bh) { |
48d6d8ff | 1898 | iinfo->i_lenAlloc += adsize; |
1da177e4 | 1899 | mark_inode_dirty(inode); |
cb00ea35 | 1900 | } else { |
ff116fc8 | 1901 | aed = (struct allocExtDesc *)epos->bh->b_data; |
c2104fda | 1902 | le32_add_cpu(&aed->lengthAllocDescs, adsize); |
4b11111a MS |
1903 | if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) || |
1904 | UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) | |
1905 | udf_update_tag(epos->bh->b_data, | |
1906 | epos->offset + (inc ? 0 : adsize)); | |
1da177e4 | 1907 | else |
4b11111a MS |
1908 | udf_update_tag(epos->bh->b_data, |
1909 | sizeof(struct allocExtDesc)); | |
ff116fc8 | 1910 | mark_buffer_dirty_inode(epos->bh, inode); |
1da177e4 LT |
1911 | } |
1912 | ||
7e49b6f2 | 1913 | return 0; |
1da177e4 LT |
1914 | } |
1915 | ||
7e49b6f2 JK |
1916 | void udf_write_aext(struct inode *inode, struct extent_position *epos, |
1917 | struct kernel_lb_addr *eloc, uint32_t elen, int inc) | |
1da177e4 LT |
1918 | { |
1919 | int adsize; | |
1920 | uint8_t *ptr; | |
5ca4e4be PE |
1921 | struct short_ad *sad; |
1922 | struct long_ad *lad; | |
48d6d8ff | 1923 | struct udf_inode_info *iinfo = UDF_I(inode); |
1da177e4 | 1924 | |
ff116fc8 | 1925 | if (!epos->bh) |
48d6d8ff | 1926 | ptr = iinfo->i_ext.i_data + epos->offset - |
4b11111a | 1927 | udf_file_entry_alloc_offset(inode) + |
48d6d8ff | 1928 | iinfo->i_lenEAttr; |
1da177e4 | 1929 | else |
ff116fc8 | 1930 | ptr = epos->bh->b_data + epos->offset; |
1da177e4 | 1931 | |
48d6d8ff | 1932 | switch (iinfo->i_alloc_type) { |
cb00ea35 | 1933 | case ICBTAG_FLAG_AD_SHORT: |
5ca4e4be | 1934 | sad = (struct short_ad *)ptr; |
28de7948 | 1935 | sad->extLength = cpu_to_le32(elen); |
97e961fd | 1936 | sad->extPosition = cpu_to_le32(eloc->logicalBlockNum); |
5ca4e4be | 1937 | adsize = sizeof(struct short_ad); |
28de7948 | 1938 | break; |
cb00ea35 | 1939 | case ICBTAG_FLAG_AD_LONG: |
5ca4e4be | 1940 | lad = (struct long_ad *)ptr; |
28de7948 | 1941 | lad->extLength = cpu_to_le32(elen); |
97e961fd | 1942 | lad->extLocation = cpu_to_lelb(*eloc); |
28de7948 | 1943 | memset(lad->impUse, 0x00, sizeof(lad->impUse)); |
5ca4e4be | 1944 | adsize = sizeof(struct long_ad); |
28de7948 | 1945 | break; |
cb00ea35 | 1946 | default: |
7e49b6f2 | 1947 | return; |
1da177e4 LT |
1948 | } |
1949 | ||
cb00ea35 | 1950 | if (epos->bh) { |
28de7948 | 1951 | if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) || |
6c79e987 | 1952 | UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) { |
4b11111a MS |
1953 | struct allocExtDesc *aed = |
1954 | (struct allocExtDesc *)epos->bh->b_data; | |
ff116fc8 | 1955 | udf_update_tag(epos->bh->b_data, |
4b11111a MS |
1956 | le32_to_cpu(aed->lengthAllocDescs) + |
1957 | sizeof(struct allocExtDesc)); | |
1da177e4 | 1958 | } |
ff116fc8 | 1959 | mark_buffer_dirty_inode(epos->bh, inode); |
28de7948 | 1960 | } else { |
1da177e4 | 1961 | mark_inode_dirty(inode); |
28de7948 | 1962 | } |
1da177e4 LT |
1963 | |
1964 | if (inc) | |
ff116fc8 | 1965 | epos->offset += adsize; |
1da177e4 LT |
1966 | } |
1967 | ||
4b11111a | 1968 | int8_t udf_next_aext(struct inode *inode, struct extent_position *epos, |
5ca4e4be | 1969 | struct kernel_lb_addr *eloc, uint32_t *elen, int inc) |
1da177e4 LT |
1970 | { |
1971 | int8_t etype; | |
1972 | ||
ff116fc8 | 1973 | while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) == |
cb00ea35 | 1974 | (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) { |
4b11111a | 1975 | int block; |
ff116fc8 JK |
1976 | epos->block = *eloc; |
1977 | epos->offset = sizeof(struct allocExtDesc); | |
3bf25cb4 | 1978 | brelse(epos->bh); |
97e961fd | 1979 | block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0); |
4b11111a MS |
1980 | epos->bh = udf_tread(inode->i_sb, block); |
1981 | if (!epos->bh) { | |
1982 | udf_debug("reading block %d failed!\n", block); | |
1da177e4 LT |
1983 | return -1; |
1984 | } | |
1985 | } | |
1986 | ||
1987 | return etype; | |
1988 | } | |
1989 | ||
4b11111a | 1990 | int8_t udf_current_aext(struct inode *inode, struct extent_position *epos, |
5ca4e4be | 1991 | struct kernel_lb_addr *eloc, uint32_t *elen, int inc) |
1da177e4 LT |
1992 | { |
1993 | int alen; | |
1994 | int8_t etype; | |
1995 | uint8_t *ptr; | |
5ca4e4be PE |
1996 | struct short_ad *sad; |
1997 | struct long_ad *lad; | |
48d6d8ff | 1998 | struct udf_inode_info *iinfo = UDF_I(inode); |
28de7948 | 1999 | |
cb00ea35 | 2000 | if (!epos->bh) { |
ff116fc8 JK |
2001 | if (!epos->offset) |
2002 | epos->offset = udf_file_entry_alloc_offset(inode); | |
48d6d8ff | 2003 | ptr = iinfo->i_ext.i_data + epos->offset - |
4b11111a | 2004 | udf_file_entry_alloc_offset(inode) + |
48d6d8ff | 2005 | iinfo->i_lenEAttr; |
4b11111a | 2006 | alen = udf_file_entry_alloc_offset(inode) + |
48d6d8ff | 2007 | iinfo->i_lenAlloc; |
cb00ea35 | 2008 | } else { |
ff116fc8 JK |
2009 | if (!epos->offset) |
2010 | epos->offset = sizeof(struct allocExtDesc); | |
2011 | ptr = epos->bh->b_data + epos->offset; | |
28de7948 | 2012 | alen = sizeof(struct allocExtDesc) + |
4b11111a MS |
2013 | le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)-> |
2014 | lengthAllocDescs); | |
1da177e4 LT |
2015 | } |
2016 | ||
48d6d8ff | 2017 | switch (iinfo->i_alloc_type) { |
cb00ea35 | 2018 | case ICBTAG_FLAG_AD_SHORT: |
4b11111a MS |
2019 | sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc); |
2020 | if (!sad) | |
28de7948 CG |
2021 | return -1; |
2022 | etype = le32_to_cpu(sad->extLength) >> 30; | |
2023 | eloc->logicalBlockNum = le32_to_cpu(sad->extPosition); | |
4b11111a | 2024 | eloc->partitionReferenceNum = |
48d6d8ff | 2025 | iinfo->i_location.partitionReferenceNum; |
28de7948 CG |
2026 | *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK; |
2027 | break; | |
cb00ea35 | 2028 | case ICBTAG_FLAG_AD_LONG: |
4b11111a MS |
2029 | lad = udf_get_filelongad(ptr, alen, &epos->offset, inc); |
2030 | if (!lad) | |
1da177e4 | 2031 | return -1; |
28de7948 CG |
2032 | etype = le32_to_cpu(lad->extLength) >> 30; |
2033 | *eloc = lelb_to_cpu(lad->extLocation); | |
2034 | *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK; | |
2035 | break; | |
2036 | default: | |
a983f368 | 2037 | udf_debug("alloc_type = %d unsupported\n", iinfo->i_alloc_type); |
28de7948 | 2038 | return -1; |
1da177e4 LT |
2039 | } |
2040 | ||
2041 | return etype; | |
2042 | } | |
2043 | ||
28de7948 | 2044 | static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos, |
5ca4e4be | 2045 | struct kernel_lb_addr neloc, uint32_t nelen) |
1da177e4 | 2046 | { |
5ca4e4be | 2047 | struct kernel_lb_addr oeloc; |
1da177e4 LT |
2048 | uint32_t oelen; |
2049 | int8_t etype; | |
2050 | ||
ff116fc8 | 2051 | if (epos.bh) |
3bf25cb4 | 2052 | get_bh(epos.bh); |
1da177e4 | 2053 | |
cb00ea35 | 2054 | while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) { |
97e961fd | 2055 | udf_write_aext(inode, &epos, &neloc, nelen, 1); |
1da177e4 LT |
2056 | neloc = oeloc; |
2057 | nelen = (etype << 30) | oelen; | |
2058 | } | |
97e961fd | 2059 | udf_add_aext(inode, &epos, &neloc, nelen, 1); |
3bf25cb4 | 2060 | brelse(epos.bh); |
28de7948 | 2061 | |
1da177e4 LT |
2062 | return (nelen >> 30); |
2063 | } | |
2064 | ||
4b11111a | 2065 | int8_t udf_delete_aext(struct inode *inode, struct extent_position epos, |
5ca4e4be | 2066 | struct kernel_lb_addr eloc, uint32_t elen) |
1da177e4 | 2067 | { |
ff116fc8 JK |
2068 | struct extent_position oepos; |
2069 | int adsize; | |
1da177e4 LT |
2070 | int8_t etype; |
2071 | struct allocExtDesc *aed; | |
48d6d8ff | 2072 | struct udf_inode_info *iinfo; |
1da177e4 | 2073 | |
cb00ea35 | 2074 | if (epos.bh) { |
3bf25cb4 JK |
2075 | get_bh(epos.bh); |
2076 | get_bh(epos.bh); | |
1da177e4 LT |
2077 | } |
2078 | ||
48d6d8ff MS |
2079 | iinfo = UDF_I(inode); |
2080 | if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT) | |
5ca4e4be | 2081 | adsize = sizeof(struct short_ad); |
48d6d8ff | 2082 | else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG) |
5ca4e4be | 2083 | adsize = sizeof(struct long_ad); |
1da177e4 LT |
2084 | else |
2085 | adsize = 0; | |
2086 | ||
ff116fc8 JK |
2087 | oepos = epos; |
2088 | if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1) | |
1da177e4 LT |
2089 | return -1; |
2090 | ||
cb00ea35 | 2091 | while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) { |
97e961fd | 2092 | udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1); |
cb00ea35 | 2093 | if (oepos.bh != epos.bh) { |
ff116fc8 | 2094 | oepos.block = epos.block; |
3bf25cb4 JK |
2095 | brelse(oepos.bh); |
2096 | get_bh(epos.bh); | |
ff116fc8 JK |
2097 | oepos.bh = epos.bh; |
2098 | oepos.offset = epos.offset - adsize; | |
1da177e4 LT |
2099 | } |
2100 | } | |
5ca4e4be | 2101 | memset(&eloc, 0x00, sizeof(struct kernel_lb_addr)); |
1da177e4 LT |
2102 | elen = 0; |
2103 | ||
cb00ea35 | 2104 | if (epos.bh != oepos.bh) { |
97e961fd PE |
2105 | udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1); |
2106 | udf_write_aext(inode, &oepos, &eloc, elen, 1); | |
2107 | udf_write_aext(inode, &oepos, &eloc, elen, 1); | |
cb00ea35 | 2108 | if (!oepos.bh) { |
48d6d8ff | 2109 | iinfo->i_lenAlloc -= (adsize * 2); |
1da177e4 | 2110 | mark_inode_dirty(inode); |
cb00ea35 | 2111 | } else { |
ff116fc8 | 2112 | aed = (struct allocExtDesc *)oepos.bh->b_data; |
c2104fda | 2113 | le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize)); |
28de7948 | 2114 | if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) || |
6c79e987 | 2115 | UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) |
4b11111a MS |
2116 | udf_update_tag(oepos.bh->b_data, |
2117 | oepos.offset - (2 * adsize)); | |
1da177e4 | 2118 | else |
4b11111a MS |
2119 | udf_update_tag(oepos.bh->b_data, |
2120 | sizeof(struct allocExtDesc)); | |
ff116fc8 | 2121 | mark_buffer_dirty_inode(oepos.bh, inode); |
1da177e4 | 2122 | } |
cb00ea35 | 2123 | } else { |
97e961fd | 2124 | udf_write_aext(inode, &oepos, &eloc, elen, 1); |
cb00ea35 | 2125 | if (!oepos.bh) { |
48d6d8ff | 2126 | iinfo->i_lenAlloc -= adsize; |
1da177e4 | 2127 | mark_inode_dirty(inode); |
cb00ea35 | 2128 | } else { |
ff116fc8 | 2129 | aed = (struct allocExtDesc *)oepos.bh->b_data; |
c2104fda | 2130 | le32_add_cpu(&aed->lengthAllocDescs, -adsize); |
28de7948 | 2131 | if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) || |
6c79e987 | 2132 | UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) |
4b11111a MS |
2133 | udf_update_tag(oepos.bh->b_data, |
2134 | epos.offset - adsize); | |
1da177e4 | 2135 | else |
4b11111a MS |
2136 | udf_update_tag(oepos.bh->b_data, |
2137 | sizeof(struct allocExtDesc)); | |
ff116fc8 | 2138 | mark_buffer_dirty_inode(oepos.bh, inode); |
1da177e4 LT |
2139 | } |
2140 | } | |
647bd61a | 2141 | |
3bf25cb4 JK |
2142 | brelse(epos.bh); |
2143 | brelse(oepos.bh); | |
28de7948 | 2144 | |
1da177e4 LT |
2145 | return (elen >> 30); |
2146 | } | |
2147 | ||
4b11111a | 2148 | int8_t inode_bmap(struct inode *inode, sector_t block, |
5ca4e4be | 2149 | struct extent_position *pos, struct kernel_lb_addr *eloc, |
4b11111a | 2150 | uint32_t *elen, sector_t *offset) |
1da177e4 | 2151 | { |
4b11111a | 2152 | unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits; |
cb00ea35 | 2153 | loff_t lbcount = 0, bcount = |
4b11111a | 2154 | (loff_t) block << blocksize_bits; |
1da177e4 | 2155 | int8_t etype; |
48d6d8ff | 2156 | struct udf_inode_info *iinfo; |
1da177e4 | 2157 | |
48d6d8ff | 2158 | iinfo = UDF_I(inode); |
ff116fc8 | 2159 | pos->offset = 0; |
48d6d8ff | 2160 | pos->block = iinfo->i_location; |
ff116fc8 | 2161 | pos->bh = NULL; |
1da177e4 | 2162 | *elen = 0; |
1da177e4 | 2163 | |
cb00ea35 | 2164 | do { |
4b11111a MS |
2165 | etype = udf_next_aext(inode, pos, eloc, elen, 1); |
2166 | if (etype == -1) { | |
2167 | *offset = (bcount - lbcount) >> blocksize_bits; | |
48d6d8ff | 2168 | iinfo->i_lenExtents = lbcount; |
1da177e4 LT |
2169 | return -1; |
2170 | } | |
2171 | lbcount += *elen; | |
2172 | } while (lbcount <= bcount); | |
2173 | ||
4b11111a | 2174 | *offset = (bcount + *elen - lbcount) >> blocksize_bits; |
1da177e4 LT |
2175 | |
2176 | return etype; | |
2177 | } | |
2178 | ||
60448b1d | 2179 | long udf_block_map(struct inode *inode, sector_t block) |
1da177e4 | 2180 | { |
5ca4e4be | 2181 | struct kernel_lb_addr eloc; |
ff116fc8 | 2182 | uint32_t elen; |
60448b1d | 2183 | sector_t offset; |
28de7948 | 2184 | struct extent_position epos = {}; |
1da177e4 LT |
2185 | int ret; |
2186 | ||
4d0fb621 | 2187 | down_read(&UDF_I(inode)->i_data_sem); |
1da177e4 | 2188 | |
4b11111a MS |
2189 | if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) == |
2190 | (EXT_RECORDED_ALLOCATED >> 30)) | |
97e961fd | 2191 | ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset); |
1da177e4 LT |
2192 | else |
2193 | ret = 0; | |
2194 | ||
4d0fb621 | 2195 | up_read(&UDF_I(inode)->i_data_sem); |
3bf25cb4 | 2196 | brelse(epos.bh); |
1da177e4 LT |
2197 | |
2198 | if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV)) | |
2199 | return udf_fixed_to_variable(ret); | |
2200 | else | |
2201 | return ret; | |
2202 | } |