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