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