OSDN Git Service

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