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