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