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