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