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