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