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