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