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