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