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