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