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