Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * linux/fs/ufs/util.c |
| 4 | * |
| 5 | * Copyright (C) 1998 |
| 6 | * Daniel Pirkl <daniel.pirkl@email.cz> |
| 7 | * Charles University, Faculty of Mathematics and Physics |
| 8 | */ |
| 9 | |
| 10 | #include <linux/string.h> |
| 11 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/buffer_head.h> |
| 13 | |
Mike Frysinger | e542059 | 2008-02-08 04:21:31 -0800 | [diff] [blame] | 14 | #include "ufs_fs.h" |
Christoph Hellwig | bcd6d4e | 2007-10-16 23:26:51 -0700 | [diff] [blame] | 15 | #include "ufs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include "swab.h" |
| 17 | #include "util.h" |
| 18 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | struct ufs_buffer_head * _ubh_bread_ (struct ufs_sb_private_info * uspi, |
| 20 | struct super_block *sb, u64 fragment, u64 size) |
| 21 | { |
| 22 | struct ufs_buffer_head * ubh; |
| 23 | unsigned i, j ; |
| 24 | u64 count = 0; |
| 25 | if (size & ~uspi->s_fmask) |
| 26 | return NULL; |
| 27 | count = size >> uspi->s_fshift; |
| 28 | if (count > UFS_MAXFRAG) |
| 29 | return NULL; |
Zhang Yanfei | 194c876 | 2013-03-12 13:37:05 +0800 | [diff] [blame] | 30 | ubh = kmalloc (sizeof (struct ufs_buffer_head), GFP_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | if (!ubh) |
| 32 | return NULL; |
| 33 | ubh->fragment = fragment; |
| 34 | ubh->count = count; |
| 35 | for (i = 0; i < count; i++) |
| 36 | if (!(ubh->bh[i] = sb_bread(sb, fragment + i))) |
| 37 | goto failed; |
| 38 | for (; i < UFS_MAXFRAG; i++) |
| 39 | ubh->bh[i] = NULL; |
| 40 | return ubh; |
| 41 | failed: |
| 42 | for (j = 0; j < i; j++) |
| 43 | brelse (ubh->bh[j]); |
| 44 | kfree(ubh); |
| 45 | return NULL; |
| 46 | } |
| 47 | |
| 48 | struct ufs_buffer_head * ubh_bread_uspi (struct ufs_sb_private_info * uspi, |
| 49 | struct super_block *sb, u64 fragment, u64 size) |
| 50 | { |
| 51 | unsigned i, j; |
| 52 | u64 count = 0; |
| 53 | if (size & ~uspi->s_fmask) |
| 54 | return NULL; |
| 55 | count = size >> uspi->s_fshift; |
| 56 | if (count <= 0 || count > UFS_MAXFRAG) |
| 57 | return NULL; |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 58 | USPI_UBH(uspi)->fragment = fragment; |
| 59 | USPI_UBH(uspi)->count = count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | for (i = 0; i < count; i++) |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 61 | if (!(USPI_UBH(uspi)->bh[i] = sb_bread(sb, fragment + i))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | goto failed; |
| 63 | for (; i < UFS_MAXFRAG; i++) |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 64 | USPI_UBH(uspi)->bh[i] = NULL; |
| 65 | return USPI_UBH(uspi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | failed: |
| 67 | for (j = 0; j < i; j++) |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 68 | brelse (USPI_UBH(uspi)->bh[j]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | return NULL; |
| 70 | } |
| 71 | |
| 72 | void ubh_brelse (struct ufs_buffer_head * ubh) |
| 73 | { |
| 74 | unsigned i; |
| 75 | if (!ubh) |
| 76 | return; |
| 77 | for (i = 0; i < ubh->count; i++) |
| 78 | brelse (ubh->bh[i]); |
| 79 | kfree (ubh); |
| 80 | } |
| 81 | |
| 82 | void ubh_brelse_uspi (struct ufs_sb_private_info * uspi) |
| 83 | { |
| 84 | unsigned i; |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 85 | if (!USPI_UBH(uspi)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | return; |
Evgeniy Dushistov | 9695ef1 | 2006-06-25 05:47:22 -0700 | [diff] [blame] | 87 | for ( i = 0; i < USPI_UBH(uspi)->count; i++ ) { |
| 88 | brelse (USPI_UBH(uspi)->bh[i]); |
| 89 | USPI_UBH(uspi)->bh[i] = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
| 93 | void ubh_mark_buffer_dirty (struct ufs_buffer_head * ubh) |
| 94 | { |
| 95 | unsigned i; |
| 96 | if (!ubh) |
| 97 | return; |
| 98 | for ( i = 0; i < ubh->count; i++ ) |
| 99 | mark_buffer_dirty (ubh->bh[i]); |
| 100 | } |
| 101 | |
| 102 | void ubh_mark_buffer_uptodate (struct ufs_buffer_head * ubh, int flag) |
| 103 | { |
| 104 | unsigned i; |
| 105 | if (!ubh) |
| 106 | return; |
| 107 | if (flag) { |
| 108 | for ( i = 0; i < ubh->count; i++ ) |
| 109 | set_buffer_uptodate (ubh->bh[i]); |
| 110 | } else { |
| 111 | for ( i = 0; i < ubh->count; i++ ) |
| 112 | clear_buffer_uptodate (ubh->bh[i]); |
| 113 | } |
| 114 | } |
| 115 | |
Christoph Hellwig | 9cb569d | 2010-08-11 17:06:24 +0200 | [diff] [blame] | 116 | void ubh_sync_block(struct ufs_buffer_head *ubh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | { |
Christoph Hellwig | 9cb569d | 2010-08-11 17:06:24 +0200 | [diff] [blame] | 118 | if (ubh) { |
| 119 | unsigned i; |
Evgeniy Dushistov | 098d5af | 2006-06-25 05:47:31 -0700 | [diff] [blame] | 120 | |
Christoph Hellwig | 9cb569d | 2010-08-11 17:06:24 +0200 | [diff] [blame] | 121 | for (i = 0; i < ubh->count; i++) |
Mike Christie | 2a222ca | 2016-06-05 14:31:43 -0500 | [diff] [blame] | 122 | write_dirty_buffer(ubh->bh[i], 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | |
Christoph Hellwig | 9cb569d | 2010-08-11 17:06:24 +0200 | [diff] [blame] | 124 | for (i = 0; i < ubh->count; i++) |
| 125 | wait_on_buffer(ubh->bh[i]); |
| 126 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | void ubh_bforget (struct ufs_buffer_head * ubh) |
| 130 | { |
| 131 | unsigned i; |
| 132 | if (!ubh) |
| 133 | return; |
| 134 | for ( i = 0; i < ubh->count; i++ ) if ( ubh->bh[i] ) |
| 135 | bforget (ubh->bh[i]); |
| 136 | } |
| 137 | |
| 138 | int ubh_buffer_dirty (struct ufs_buffer_head * ubh) |
| 139 | { |
| 140 | unsigned i; |
| 141 | unsigned result = 0; |
| 142 | if (!ubh) |
| 143 | return 0; |
| 144 | for ( i = 0; i < ubh->count; i++ ) |
| 145 | result |= buffer_dirty(ubh->bh[i]); |
| 146 | return result; |
| 147 | } |
| 148 | |
| 149 | void _ubh_ubhcpymem_(struct ufs_sb_private_info * uspi, |
| 150 | unsigned char * mem, struct ufs_buffer_head * ubh, unsigned size) |
| 151 | { |
| 152 | unsigned len, bhno; |
| 153 | if (size > (ubh->count << uspi->s_fshift)) |
| 154 | size = ubh->count << uspi->s_fshift; |
| 155 | bhno = 0; |
| 156 | while (size) { |
| 157 | len = min_t(unsigned int, size, uspi->s_fsize); |
| 158 | memcpy (mem, ubh->bh[bhno]->b_data, len); |
| 159 | mem += uspi->s_fsize; |
| 160 | size -= len; |
| 161 | bhno++; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | void _ubh_memcpyubh_(struct ufs_sb_private_info * uspi, |
| 166 | struct ufs_buffer_head * ubh, unsigned char * mem, unsigned size) |
| 167 | { |
| 168 | unsigned len, bhno; |
| 169 | if (size > (ubh->count << uspi->s_fshift)) |
| 170 | size = ubh->count << uspi->s_fshift; |
| 171 | bhno = 0; |
| 172 | while (size) { |
| 173 | len = min_t(unsigned int, size, uspi->s_fsize); |
| 174 | memcpy (ubh->bh[bhno]->b_data, mem, len); |
| 175 | mem += uspi->s_fsize; |
| 176 | size -= len; |
| 177 | bhno++; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | dev_t |
| 182 | ufs_get_inode_dev(struct super_block *sb, struct ufs_inode_info *ufsi) |
| 183 | { |
Al Viro | 44aa535 | 2006-08-13 01:54:30 -0400 | [diff] [blame] | 184 | __u32 fs32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | dev_t dev; |
| 186 | |
| 187 | if ((UFS_SB(sb)->s_flags & UFS_ST_MASK) == UFS_ST_SUNx86) |
Al Viro | 44aa535 | 2006-08-13 01:54:30 -0400 | [diff] [blame] | 188 | fs32 = fs32_to_cpu(sb, ufsi->i_u1.i_data[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | else |
Al Viro | 44aa535 | 2006-08-13 01:54:30 -0400 | [diff] [blame] | 190 | fs32 = fs32_to_cpu(sb, ufsi->i_u1.i_data[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | switch (UFS_SB(sb)->s_flags & UFS_ST_MASK) { |
| 192 | case UFS_ST_SUNx86: |
| 193 | case UFS_ST_SUN: |
| 194 | if ((fs32 & 0xffff0000) == 0 || |
| 195 | (fs32 & 0xffff0000) == 0xffff0000) |
| 196 | dev = old_decode_dev(fs32 & 0x7fff); |
| 197 | else |
| 198 | dev = MKDEV(sysv_major(fs32), sysv_minor(fs32)); |
| 199 | break; |
| 200 | |
| 201 | default: |
| 202 | dev = old_decode_dev(fs32); |
| 203 | break; |
| 204 | } |
| 205 | return dev; |
| 206 | } |
| 207 | |
| 208 | void |
| 209 | ufs_set_inode_dev(struct super_block *sb, struct ufs_inode_info *ufsi, dev_t dev) |
| 210 | { |
Al Viro | 44aa535 | 2006-08-13 01:54:30 -0400 | [diff] [blame] | 211 | __u32 fs32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
| 213 | switch (UFS_SB(sb)->s_flags & UFS_ST_MASK) { |
| 214 | case UFS_ST_SUNx86: |
| 215 | case UFS_ST_SUN: |
| 216 | fs32 = sysv_encode_dev(dev); |
| 217 | if ((fs32 & 0xffff8000) == 0) { |
| 218 | fs32 = old_encode_dev(dev); |
| 219 | } |
| 220 | break; |
| 221 | |
| 222 | default: |
| 223 | fs32 = old_encode_dev(dev); |
| 224 | break; |
| 225 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | if ((UFS_SB(sb)->s_flags & UFS_ST_MASK) == UFS_ST_SUNx86) |
Al Viro | 44aa535 | 2006-08-13 01:54:30 -0400 | [diff] [blame] | 227 | ufsi->i_u1.i_data[1] = cpu_to_fs32(sb, fs32); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | else |
Al Viro | 44aa535 | 2006-08-13 01:54:30 -0400 | [diff] [blame] | 229 | ufsi->i_u1.i_data[0] = cpu_to_fs32(sb, fs32); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | } |
Evgeniy Dushistov | 10e5dce | 2006-07-01 04:36:24 -0700 | [diff] [blame] | 231 | |
| 232 | /** |
| 233 | * ufs_get_locked_page() - locate, pin and lock a pagecache page, if not exist |
| 234 | * read it from disk. |
| 235 | * @mapping: the address_space to search |
| 236 | * @index: the page index |
| 237 | * |
| 238 | * Locates the desired pagecache page, if not exist we'll read it, |
| 239 | * locks it, increments its reference |
| 240 | * count and returns its address. |
| 241 | * |
| 242 | */ |
| 243 | |
| 244 | struct page *ufs_get_locked_page(struct address_space *mapping, |
| 245 | pgoff_t index) |
| 246 | { |
Al Viro | 267309f | 2017-06-14 23:32:19 -0400 | [diff] [blame] | 247 | struct inode *inode = mapping->host; |
| 248 | struct page *page = find_lock_page(mapping, index); |
Evgeniy Dushistov | 10e5dce | 2006-07-01 04:36:24 -0700 | [diff] [blame] | 249 | if (!page) { |
Nick Piggin | 6fe6900 | 2007-05-06 14:49:04 -0700 | [diff] [blame] | 250 | page = read_mapping_page(mapping, index, NULL); |
Evgeniy Dushistov | 1fb32b7 | 2006-08-05 12:13:55 -0700 | [diff] [blame] | 251 | |
Evgeniy Dushistov | 10e5dce | 2006-07-01 04:36:24 -0700 | [diff] [blame] | 252 | if (IS_ERR(page)) { |
| 253 | printk(KERN_ERR "ufs_change_blocknr: " |
Nick Piggin | 6fe6900 | 2007-05-06 14:49:04 -0700 | [diff] [blame] | 254 | "read_mapping_page error: ino %lu, index: %lu\n", |
Evgeniy Dushistov | 10e5dce | 2006-07-01 04:36:24 -0700 | [diff] [blame] | 255 | mapping->host->i_ino, index); |
Al Viro | 267309f | 2017-06-14 23:32:19 -0400 | [diff] [blame] | 256 | return page; |
Evgeniy Dushistov | 10e5dce | 2006-07-01 04:36:24 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | lock_page(page); |
| 260 | |
Evgeniy Dushistov | 1fb32b7 | 2006-08-05 12:13:55 -0700 | [diff] [blame] | 261 | if (unlikely(page->mapping == NULL)) { |
| 262 | /* Truncate got there first */ |
| 263 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 264 | put_page(page); |
Al Viro | 267309f | 2017-06-14 23:32:19 -0400 | [diff] [blame] | 265 | return NULL; |
Evgeniy Dushistov | 1fb32b7 | 2006-08-05 12:13:55 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Evgeniy Dushistov | 10e5dce | 2006-07-01 04:36:24 -0700 | [diff] [blame] | 268 | if (!PageUptodate(page) || PageError(page)) { |
| 269 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 270 | put_page(page); |
Evgeniy Dushistov | 10e5dce | 2006-07-01 04:36:24 -0700 | [diff] [blame] | 271 | |
| 272 | printk(KERN_ERR "ufs_change_blocknr: " |
| 273 | "can not read page: ino %lu, index: %lu\n", |
Al Viro | 267309f | 2017-06-14 23:32:19 -0400 | [diff] [blame] | 274 | inode->i_ino, index); |
Evgeniy Dushistov | 10e5dce | 2006-07-01 04:36:24 -0700 | [diff] [blame] | 275 | |
Al Viro | 267309f | 2017-06-14 23:32:19 -0400 | [diff] [blame] | 276 | return ERR_PTR(-EIO); |
Evgeniy Dushistov | 10e5dce | 2006-07-01 04:36:24 -0700 | [diff] [blame] | 277 | } |
| 278 | } |
Al Viro | 267309f | 2017-06-14 23:32:19 -0400 | [diff] [blame] | 279 | if (!page_has_buffers(page)) |
| 280 | create_empty_buffers(page, 1 << inode->i_blkbits, 0); |
Evgeniy Dushistov | 10e5dce | 2006-07-01 04:36:24 -0700 | [diff] [blame] | 281 | return page; |
| 282 | } |