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