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