1 // SPDX-License-Identifier: GPL-2.0 1 // SPDX-License-Identifier: GPL-2.0 2 /* 2 /* 3 * linux/fs/ufs/inode.c 3 * linux/fs/ufs/inode.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 * from 9 * from 10 * 10 * 11 * linux/fs/ext2/inode.c 11 * linux/fs/ext2/inode.c 12 * 12 * 13 * Copyright (C) 1992, 1993, 1994, 1995 13 * Copyright (C) 1992, 1993, 1994, 1995 14 * Remy Card (card@masi.ibp.fr) 14 * Remy Card (card@masi.ibp.fr) 15 * Laboratoire MASI - Institut Blaise Pascal 15 * Laboratoire MASI - Institut Blaise Pascal 16 * Universite Pierre et Marie Curie (Paris VI) 16 * Universite Pierre et Marie Curie (Paris VI) 17 * 17 * 18 * from 18 * from 19 * 19 * 20 * linux/fs/minix/inode.c 20 * linux/fs/minix/inode.c 21 * 21 * 22 * Copyright (C) 1991, 1992 Linus Torvalds 22 * Copyright (C) 1991, 1992 Linus Torvalds 23 * 23 * 24 * Goal-directed block allocation by Stephen 24 * Goal-directed block allocation by Stephen Tweedie (sct@dcs.ed.ac.uk), 1993 25 * Big-endian to little-endian byte-swapping/ 25 * Big-endian to little-endian byte-swapping/bitmaps by 26 * David S. Miller (davem@caip.rutgers. 26 * David S. Miller (davem@caip.rutgers.edu), 1995 27 */ 27 */ 28 28 29 #include <linux/uaccess.h> 29 #include <linux/uaccess.h> 30 30 31 #include <linux/errno.h> 31 #include <linux/errno.h> 32 #include <linux/fs.h> 32 #include <linux/fs.h> 33 #include <linux/time.h> 33 #include <linux/time.h> 34 #include <linux/stat.h> 34 #include <linux/stat.h> 35 #include <linux/string.h> 35 #include <linux/string.h> 36 #include <linux/mm.h> 36 #include <linux/mm.h> 37 #include <linux/buffer_head.h> 37 #include <linux/buffer_head.h> 38 #include <linux/mpage.h> << 39 #include <linux/writeback.h> 38 #include <linux/writeback.h> 40 #include <linux/iversion.h> 39 #include <linux/iversion.h> 41 40 42 #include "ufs_fs.h" 41 #include "ufs_fs.h" 43 #include "ufs.h" 42 #include "ufs.h" 44 #include "swab.h" 43 #include "swab.h" 45 #include "util.h" 44 #include "util.h" 46 45 47 static int ufs_block_to_path(struct inode *ino 46 static int ufs_block_to_path(struct inode *inode, sector_t i_block, unsigned offsets[4]) 48 { 47 { 49 struct ufs_sb_private_info *uspi = UFS 48 struct ufs_sb_private_info *uspi = UFS_SB(inode->i_sb)->s_uspi; 50 int ptrs = uspi->s_apb; 49 int ptrs = uspi->s_apb; 51 int ptrs_bits = uspi->s_apbshift; 50 int ptrs_bits = uspi->s_apbshift; 52 const long direct_blocks = UFS_NDADDR, 51 const long direct_blocks = UFS_NDADDR, 53 indirect_blocks = ptrs, 52 indirect_blocks = ptrs, 54 double_blocks = (1 << (ptrs_bi 53 double_blocks = (1 << (ptrs_bits * 2)); 55 int n = 0; 54 int n = 0; 56 55 57 56 58 UFSD("ptrs=uspi->s_apb = %d,double_blo 57 UFSD("ptrs=uspi->s_apb = %d,double_blocks=%ld \n",ptrs,double_blocks); 59 if (i_block < direct_blocks) { 58 if (i_block < direct_blocks) { 60 offsets[n++] = i_block; 59 offsets[n++] = i_block; 61 } else if ((i_block -= direct_blocks) 60 } else if ((i_block -= direct_blocks) < indirect_blocks) { 62 offsets[n++] = UFS_IND_BLOCK; 61 offsets[n++] = UFS_IND_BLOCK; 63 offsets[n++] = i_block; 62 offsets[n++] = i_block; 64 } else if ((i_block -= indirect_blocks 63 } else if ((i_block -= indirect_blocks) < double_blocks) { 65 offsets[n++] = UFS_DIND_BLOCK; 64 offsets[n++] = UFS_DIND_BLOCK; 66 offsets[n++] = i_block >> ptrs 65 offsets[n++] = i_block >> ptrs_bits; 67 offsets[n++] = i_block & (ptrs 66 offsets[n++] = i_block & (ptrs - 1); 68 } else if (((i_block -= double_blocks) 67 } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) { 69 offsets[n++] = UFS_TIND_BLOCK; 68 offsets[n++] = UFS_TIND_BLOCK; 70 offsets[n++] = i_block >> (ptr 69 offsets[n++] = i_block >> (ptrs_bits * 2); 71 offsets[n++] = (i_block >> ptr 70 offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1); 72 offsets[n++] = i_block & (ptrs 71 offsets[n++] = i_block & (ptrs - 1); 73 } else { 72 } else { 74 ufs_warning(inode->i_sb, "ufs_ 73 ufs_warning(inode->i_sb, "ufs_block_to_path", "block > big"); 75 } 74 } 76 return n; 75 return n; 77 } 76 } 78 77 79 typedef struct { 78 typedef struct { 80 void *p; 79 void *p; 81 union { 80 union { 82 __fs32 key32; 81 __fs32 key32; 83 __fs64 key64; 82 __fs64 key64; 84 }; 83 }; 85 struct buffer_head *bh; 84 struct buffer_head *bh; 86 } Indirect; 85 } Indirect; 87 86 88 static inline int grow_chain32(struct ufs_inod 87 static inline int grow_chain32(struct ufs_inode_info *ufsi, 89 struct buffer_h 88 struct buffer_head *bh, __fs32 *v, 90 Indirect *from, 89 Indirect *from, Indirect *to) 91 { 90 { 92 Indirect *p; 91 Indirect *p; 93 unsigned seq; 92 unsigned seq; 94 to->bh = bh; 93 to->bh = bh; 95 do { 94 do { 96 seq = read_seqbegin(&ufsi->met 95 seq = read_seqbegin(&ufsi->meta_lock); 97 to->key32 = *(__fs32 *)(to->p 96 to->key32 = *(__fs32 *)(to->p = v); 98 for (p = from; p <= to && p->k 97 for (p = from; p <= to && p->key32 == *(__fs32 *)p->p; p++) 99 ; 98 ; 100 } while (read_seqretry(&ufsi->meta_loc 99 } while (read_seqretry(&ufsi->meta_lock, seq)); 101 return (p > to); 100 return (p > to); 102 } 101 } 103 102 104 static inline int grow_chain64(struct ufs_inod 103 static inline int grow_chain64(struct ufs_inode_info *ufsi, 105 struct buffer_h 104 struct buffer_head *bh, __fs64 *v, 106 Indirect *from, 105 Indirect *from, Indirect *to) 107 { 106 { 108 Indirect *p; 107 Indirect *p; 109 unsigned seq; 108 unsigned seq; 110 to->bh = bh; 109 to->bh = bh; 111 do { 110 do { 112 seq = read_seqbegin(&ufsi->met 111 seq = read_seqbegin(&ufsi->meta_lock); 113 to->key64 = *(__fs64 *)(to->p 112 to->key64 = *(__fs64 *)(to->p = v); 114 for (p = from; p <= to && p->k 113 for (p = from; p <= to && p->key64 == *(__fs64 *)p->p; p++) 115 ; 114 ; 116 } while (read_seqretry(&ufsi->meta_loc 115 } while (read_seqretry(&ufsi->meta_lock, seq)); 117 return (p > to); 116 return (p > to); 118 } 117 } 119 118 120 /* 119 /* 121 * Returns the location of the fragment from 120 * Returns the location of the fragment from 122 * the beginning of the filesystem. 121 * the beginning of the filesystem. 123 */ 122 */ 124 123 125 static u64 ufs_frag_map(struct inode *inode, u 124 static u64 ufs_frag_map(struct inode *inode, unsigned offsets[4], int depth) 126 { 125 { 127 struct ufs_inode_info *ufsi = UFS_I(in 126 struct ufs_inode_info *ufsi = UFS_I(inode); 128 struct super_block *sb = inode->i_sb; 127 struct super_block *sb = inode->i_sb; 129 struct ufs_sb_private_info *uspi = UFS 128 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; 130 u64 mask = (u64) uspi->s_apbmask>>uspi 129 u64 mask = (u64) uspi->s_apbmask>>uspi->s_fpbshift; 131 int shift = uspi->s_apbshift-uspi->s_f 130 int shift = uspi->s_apbshift-uspi->s_fpbshift; 132 Indirect chain[4], *q = chain; 131 Indirect chain[4], *q = chain; 133 unsigned *p; 132 unsigned *p; 134 unsigned flags = UFS_SB(sb)->s_flags; 133 unsigned flags = UFS_SB(sb)->s_flags; 135 u64 res = 0; 134 u64 res = 0; 136 135 137 UFSD(": uspi->s_fpbshift = %d ,uspi->s 136 UFSD(": uspi->s_fpbshift = %d ,uspi->s_apbmask = %x, mask=%llx\n", 138 uspi->s_fpbshift, uspi->s_apbm 137 uspi->s_fpbshift, uspi->s_apbmask, 139 (unsigned long long)mask); 138 (unsigned long long)mask); 140 139 141 if (depth == 0) 140 if (depth == 0) 142 goto no_block; 141 goto no_block; 143 142 144 again: 143 again: 145 p = offsets; 144 p = offsets; 146 145 147 if ((flags & UFS_TYPE_MASK) == UFS_TYP 146 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) 148 goto ufs2; 147 goto ufs2; 149 148 150 if (!grow_chain32(ufsi, NULL, &ufsi->i 149 if (!grow_chain32(ufsi, NULL, &ufsi->i_u1.i_data[*p++], chain, q)) 151 goto changed; 150 goto changed; 152 if (!q->key32) 151 if (!q->key32) 153 goto no_block; 152 goto no_block; 154 while (--depth) { 153 while (--depth) { 155 __fs32 *ptr; 154 __fs32 *ptr; 156 struct buffer_head *bh; 155 struct buffer_head *bh; 157 unsigned n = *p++; 156 unsigned n = *p++; 158 157 159 bh = sb_bread(sb, uspi->s_sbba 158 bh = sb_bread(sb, uspi->s_sbbase + 160 fs32_to_cpu( 159 fs32_to_cpu(sb, q->key32) + (n>>shift)); 161 if (!bh) 160 if (!bh) 162 goto no_block; 161 goto no_block; 163 ptr = (__fs32 *)bh->b_data + ( 162 ptr = (__fs32 *)bh->b_data + (n & mask); 164 if (!grow_chain32(ufsi, bh, pt 163 if (!grow_chain32(ufsi, bh, ptr, chain, ++q)) 165 goto changed; 164 goto changed; 166 if (!q->key32) 165 if (!q->key32) 167 goto no_block; 166 goto no_block; 168 } 167 } 169 res = fs32_to_cpu(sb, q->key32); 168 res = fs32_to_cpu(sb, q->key32); 170 goto found; 169 goto found; 171 170 172 ufs2: 171 ufs2: 173 if (!grow_chain64(ufsi, NULL, &ufsi->i 172 if (!grow_chain64(ufsi, NULL, &ufsi->i_u1.u2_i_data[*p++], chain, q)) 174 goto changed; 173 goto changed; 175 if (!q->key64) 174 if (!q->key64) 176 goto no_block; 175 goto no_block; 177 176 178 while (--depth) { 177 while (--depth) { 179 __fs64 *ptr; 178 __fs64 *ptr; 180 struct buffer_head *bh; 179 struct buffer_head *bh; 181 unsigned n = *p++; 180 unsigned n = *p++; 182 181 183 bh = sb_bread(sb, uspi->s_sbba 182 bh = sb_bread(sb, uspi->s_sbbase + 184 fs64_to_cpu( 183 fs64_to_cpu(sb, q->key64) + (n>>shift)); 185 if (!bh) 184 if (!bh) 186 goto no_block; 185 goto no_block; 187 ptr = (__fs64 *)bh->b_data + ( 186 ptr = (__fs64 *)bh->b_data + (n & mask); 188 if (!grow_chain64(ufsi, bh, pt 187 if (!grow_chain64(ufsi, bh, ptr, chain, ++q)) 189 goto changed; 188 goto changed; 190 if (!q->key64) 189 if (!q->key64) 191 goto no_block; 190 goto no_block; 192 } 191 } 193 res = fs64_to_cpu(sb, q->key64); 192 res = fs64_to_cpu(sb, q->key64); 194 found: 193 found: 195 res += uspi->s_sbbase; 194 res += uspi->s_sbbase; 196 no_block: 195 no_block: 197 while (q > chain) { 196 while (q > chain) { 198 brelse(q->bh); 197 brelse(q->bh); 199 q--; 198 q--; 200 } 199 } 201 return res; 200 return res; 202 201 203 changed: 202 changed: 204 while (q > chain) { 203 while (q > chain) { 205 brelse(q->bh); 204 brelse(q->bh); 206 q--; 205 q--; 207 } 206 } 208 goto again; 207 goto again; 209 } 208 } 210 209 211 /* 210 /* 212 * Unpacking tails: we have a file with partia 211 * Unpacking tails: we have a file with partial final block and 213 * we had been asked to extend it. If the fra 212 * we had been asked to extend it. If the fragment being written 214 * is within the same block, we need to extend 213 * is within the same block, we need to extend the tail just to cover 215 * that fragment. Otherwise the tail is exten 214 * that fragment. Otherwise the tail is extended to full block. 216 * 215 * 217 * Note that we might need to create a _new_ t 216 * Note that we might need to create a _new_ tail, but that will 218 * be handled elsewhere; this is strictly for 217 * be handled elsewhere; this is strictly for resizing old 219 * ones. 218 * ones. 220 */ 219 */ 221 static bool 220 static bool 222 ufs_extend_tail(struct inode *inode, u64 write 221 ufs_extend_tail(struct inode *inode, u64 writes_to, 223 int *err, struct page *locke 222 int *err, struct page *locked_page) 224 { 223 { 225 struct ufs_inode_info *ufsi = UFS_I(in 224 struct ufs_inode_info *ufsi = UFS_I(inode); 226 struct super_block *sb = inode->i_sb; 225 struct super_block *sb = inode->i_sb; 227 struct ufs_sb_private_info *uspi = UFS 226 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; 228 unsigned lastfrag = ufsi->i_lastfrag; 227 unsigned lastfrag = ufsi->i_lastfrag; /* it's a short file, so unsigned is enough */ 229 unsigned block = ufs_fragstoblks(lastf 228 unsigned block = ufs_fragstoblks(lastfrag); 230 unsigned new_size; 229 unsigned new_size; 231 void *p; 230 void *p; 232 u64 tmp; 231 u64 tmp; 233 232 234 if (writes_to < (lastfrag | uspi->s_fp 233 if (writes_to < (lastfrag | uspi->s_fpbmask)) 235 new_size = (writes_to & uspi-> 234 new_size = (writes_to & uspi->s_fpbmask) + 1; 236 else 235 else 237 new_size = uspi->s_fpb; 236 new_size = uspi->s_fpb; 238 237 239 p = ufs_get_direct_data_ptr(uspi, ufsi 238 p = ufs_get_direct_data_ptr(uspi, ufsi, block); 240 tmp = ufs_new_fragments(inode, p, last 239 tmp = ufs_new_fragments(inode, p, lastfrag, ufs_data_ptr_to_cpu(sb, p), 241 new_size - (la 240 new_size - (lastfrag & uspi->s_fpbmask), err, 242 locked_page); 241 locked_page); 243 return tmp != 0; 242 return tmp != 0; 244 } 243 } 245 244 246 /** 245 /** 247 * ufs_inode_getfrag() - allocate new fragment 246 * ufs_inode_getfrag() - allocate new fragment(s) 248 * @inode: pointer to inode 247 * @inode: pointer to inode 249 * @index: number of block pointer within the 248 * @index: number of block pointer within the inode's array. 250 * @new_fragment: number of new allocated frag 249 * @new_fragment: number of new allocated fragment(s) 251 * @err: we set it if something wrong 250 * @err: we set it if something wrong 252 * @new: we set it if we allocate new block 251 * @new: we set it if we allocate new block 253 * @locked_page: for ufs_new_fragments() 252 * @locked_page: for ufs_new_fragments() 254 */ 253 */ 255 static u64 254 static u64 256 ufs_inode_getfrag(struct inode *inode, unsigne 255 ufs_inode_getfrag(struct inode *inode, unsigned index, 257 sector_t new_fragment, int * 256 sector_t new_fragment, int *err, 258 int *new, struct page *locke 257 int *new, struct page *locked_page) 259 { 258 { 260 struct ufs_inode_info *ufsi = UFS_I(in 259 struct ufs_inode_info *ufsi = UFS_I(inode); 261 struct super_block *sb = inode->i_sb; 260 struct super_block *sb = inode->i_sb; 262 struct ufs_sb_private_info *uspi = UFS 261 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; 263 u64 tmp, goal, lastfrag; 262 u64 tmp, goal, lastfrag; 264 unsigned nfrags = uspi->s_fpb; 263 unsigned nfrags = uspi->s_fpb; 265 void *p; 264 void *p; 266 265 267 /* TODO : to be done for write support 266 /* TODO : to be done for write support 268 if ( (flags & UFS_TYPE_MASK) == UFS_TY 267 if ( (flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) 269 goto ufs2; 268 goto ufs2; 270 */ 269 */ 271 270 272 p = ufs_get_direct_data_ptr(uspi, ufsi 271 p = ufs_get_direct_data_ptr(uspi, ufsi, index); 273 tmp = ufs_data_ptr_to_cpu(sb, p); 272 tmp = ufs_data_ptr_to_cpu(sb, p); 274 if (tmp) 273 if (tmp) 275 goto out; 274 goto out; 276 275 277 lastfrag = ufsi->i_lastfrag; 276 lastfrag = ufsi->i_lastfrag; 278 277 279 /* will that be a new tail? */ 278 /* will that be a new tail? */ 280 if (new_fragment < UFS_NDIR_FRAGMENT & 279 if (new_fragment < UFS_NDIR_FRAGMENT && new_fragment >= lastfrag) 281 nfrags = (new_fragment & uspi- 280 nfrags = (new_fragment & uspi->s_fpbmask) + 1; 282 281 283 goal = 0; 282 goal = 0; 284 if (index) { 283 if (index) { 285 goal = ufs_data_ptr_to_cpu(sb, 284 goal = ufs_data_ptr_to_cpu(sb, 286 ufs_get_direc 285 ufs_get_direct_data_ptr(uspi, ufsi, index - 1)); 287 if (goal) 286 if (goal) 288 goal += uspi->s_fpb; 287 goal += uspi->s_fpb; 289 } 288 } 290 tmp = ufs_new_fragments(inode, p, ufs_ 289 tmp = ufs_new_fragments(inode, p, ufs_blknum(new_fragment), 291 goal, nfrags, 290 goal, nfrags, err, locked_page); 292 291 293 if (!tmp) { 292 if (!tmp) { 294 *err = -ENOSPC; 293 *err = -ENOSPC; 295 return 0; 294 return 0; 296 } 295 } 297 296 298 if (new) 297 if (new) 299 *new = 1; 298 *new = 1; 300 inode_set_ctime_current(inode); !! 299 inode->i_ctime = current_time(inode); 301 if (IS_SYNC(inode)) 300 if (IS_SYNC(inode)) 302 ufs_sync_inode (inode); 301 ufs_sync_inode (inode); 303 mark_inode_dirty(inode); 302 mark_inode_dirty(inode); 304 out: 303 out: 305 return tmp + uspi->s_sbbase; 304 return tmp + uspi->s_sbbase; 306 305 307 /* This part : To be implemented .... 306 /* This part : To be implemented .... 308 Required only for writing, not require 307 Required only for writing, not required for READ-ONLY. 309 ufs2: 308 ufs2: 310 309 311 u2_block = ufs_fragstoblks(fragment); 310 u2_block = ufs_fragstoblks(fragment); 312 u2_blockoff = ufs_fragnum(fragment); 311 u2_blockoff = ufs_fragnum(fragment); 313 p = ufsi->i_u1.u2_i_data + block; 312 p = ufsi->i_u1.u2_i_data + block; 314 goal = 0; 313 goal = 0; 315 314 316 repeat2: 315 repeat2: 317 tmp = fs32_to_cpu(sb, *p); 316 tmp = fs32_to_cpu(sb, *p); 318 lastfrag = ufsi->i_lastfrag; 317 lastfrag = ufsi->i_lastfrag; 319 318 320 */ 319 */ 321 } 320 } 322 321 323 /** 322 /** 324 * ufs_inode_getblock() - allocate new block 323 * ufs_inode_getblock() - allocate new block 325 * @inode: pointer to inode 324 * @inode: pointer to inode 326 * @ind_block: block number of the indirect bl 325 * @ind_block: block number of the indirect block 327 * @index: number of pointer within the indire 326 * @index: number of pointer within the indirect block 328 * @new_fragment: number of new allocated frag 327 * @new_fragment: number of new allocated fragment 329 * (block will hold this fragment and also us 328 * (block will hold this fragment and also uspi->s_fpb-1) 330 * @err: see ufs_inode_getfrag() 329 * @err: see ufs_inode_getfrag() 331 * @new: see ufs_inode_getfrag() 330 * @new: see ufs_inode_getfrag() 332 * @locked_page: see ufs_inode_getfrag() 331 * @locked_page: see ufs_inode_getfrag() 333 */ 332 */ 334 static u64 333 static u64 335 ufs_inode_getblock(struct inode *inode, u64 in 334 ufs_inode_getblock(struct inode *inode, u64 ind_block, 336 unsigned index, sector_t new 335 unsigned index, sector_t new_fragment, int *err, 337 int *new, struct page *locke 336 int *new, struct page *locked_page) 338 { 337 { 339 struct super_block *sb = inode->i_sb; 338 struct super_block *sb = inode->i_sb; 340 struct ufs_sb_private_info *uspi = UFS 339 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; 341 int shift = uspi->s_apbshift - uspi->s 340 int shift = uspi->s_apbshift - uspi->s_fpbshift; 342 u64 tmp = 0, goal; 341 u64 tmp = 0, goal; 343 struct buffer_head *bh; 342 struct buffer_head *bh; 344 void *p; 343 void *p; 345 344 346 if (!ind_block) 345 if (!ind_block) 347 return 0; 346 return 0; 348 347 349 bh = sb_bread(sb, ind_block + (index > 348 bh = sb_bread(sb, ind_block + (index >> shift)); 350 if (unlikely(!bh)) { 349 if (unlikely(!bh)) { 351 *err = -EIO; 350 *err = -EIO; 352 return 0; 351 return 0; 353 } 352 } 354 353 355 index &= uspi->s_apbmask >> uspi->s_fp 354 index &= uspi->s_apbmask >> uspi->s_fpbshift; 356 if (uspi->fs_magic == UFS2_MAGIC) 355 if (uspi->fs_magic == UFS2_MAGIC) 357 p = (__fs64 *)bh->b_data + ind 356 p = (__fs64 *)bh->b_data + index; 358 else 357 else 359 p = (__fs32 *)bh->b_data + ind 358 p = (__fs32 *)bh->b_data + index; 360 359 361 tmp = ufs_data_ptr_to_cpu(sb, p); 360 tmp = ufs_data_ptr_to_cpu(sb, p); 362 if (tmp) 361 if (tmp) 363 goto out; 362 goto out; 364 363 365 if (index && (uspi->fs_magic == UFS2_M 364 if (index && (uspi->fs_magic == UFS2_MAGIC ? 366 (tmp = fs64_to_cpu(sb, ( 365 (tmp = fs64_to_cpu(sb, ((__fs64 *)bh->b_data)[index-1])) : 367 (tmp = fs32_to_cpu(sb, ( 366 (tmp = fs32_to_cpu(sb, ((__fs32 *)bh->b_data)[index-1])))) 368 goal = tmp + uspi->s_fpb; 367 goal = tmp + uspi->s_fpb; 369 else 368 else 370 goal = bh->b_blocknr + uspi->s 369 goal = bh->b_blocknr + uspi->s_fpb; 371 tmp = ufs_new_fragments(inode, p, ufs_ 370 tmp = ufs_new_fragments(inode, p, ufs_blknum(new_fragment), goal, 372 uspi->s_fpb, e 371 uspi->s_fpb, err, locked_page); 373 if (!tmp) 372 if (!tmp) 374 goto out; 373 goto out; 375 374 376 if (new) 375 if (new) 377 *new = 1; 376 *new = 1; 378 377 379 mark_buffer_dirty(bh); 378 mark_buffer_dirty(bh); 380 if (IS_SYNC(inode)) 379 if (IS_SYNC(inode)) 381 sync_dirty_buffer(bh); 380 sync_dirty_buffer(bh); 382 inode_set_ctime_current(inode); !! 381 inode->i_ctime = current_time(inode); 383 mark_inode_dirty(inode); 382 mark_inode_dirty(inode); 384 out: 383 out: 385 brelse (bh); 384 brelse (bh); 386 UFSD("EXIT\n"); 385 UFSD("EXIT\n"); 387 if (tmp) 386 if (tmp) 388 tmp += uspi->s_sbbase; 387 tmp += uspi->s_sbbase; 389 return tmp; 388 return tmp; 390 } 389 } 391 390 392 /** 391 /** 393 * ufs_getfrag_block() - `get_block_t' functio 392 * ufs_getfrag_block() - `get_block_t' function, interface between UFS and 394 * read_folio, writepages and so on !! 393 * readpage, writepage and so on 395 */ 394 */ 396 395 397 static int ufs_getfrag_block(struct inode *ino 396 static int ufs_getfrag_block(struct inode *inode, sector_t fragment, struct buffer_head *bh_result, int create) 398 { 397 { 399 struct super_block *sb = inode->i_sb; 398 struct super_block *sb = inode->i_sb; 400 struct ufs_sb_private_info *uspi = UFS 399 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; 401 int err = 0, new = 0; 400 int err = 0, new = 0; 402 unsigned offsets[4]; 401 unsigned offsets[4]; 403 int depth = ufs_block_to_path(inode, f 402 int depth = ufs_block_to_path(inode, fragment >> uspi->s_fpbshift, offsets); 404 u64 phys64 = 0; 403 u64 phys64 = 0; 405 unsigned frag = fragment & uspi->s_fpb 404 unsigned frag = fragment & uspi->s_fpbmask; 406 405 407 phys64 = ufs_frag_map(inode, offsets, 406 phys64 = ufs_frag_map(inode, offsets, depth); 408 if (!create) 407 if (!create) 409 goto done; 408 goto done; 410 409 411 if (phys64) { 410 if (phys64) { 412 if (fragment >= UFS_NDIR_FRAGM 411 if (fragment >= UFS_NDIR_FRAGMENT) 413 goto done; 412 goto done; 414 read_seqlock_excl(&UFS_I(inode 413 read_seqlock_excl(&UFS_I(inode)->meta_lock); 415 if (fragment < UFS_I(inode)->i 414 if (fragment < UFS_I(inode)->i_lastfrag) { 416 read_sequnlock_excl(&U 415 read_sequnlock_excl(&UFS_I(inode)->meta_lock); 417 goto done; 416 goto done; 418 } 417 } 419 read_sequnlock_excl(&UFS_I(ino 418 read_sequnlock_excl(&UFS_I(inode)->meta_lock); 420 } 419 } 421 /* This code entered only while writin 420 /* This code entered only while writing ....? */ 422 421 423 mutex_lock(&UFS_I(inode)->truncate_mut 422 mutex_lock(&UFS_I(inode)->truncate_mutex); 424 423 425 UFSD("ENTER, ino %lu, fragment %llu\n" 424 UFSD("ENTER, ino %lu, fragment %llu\n", inode->i_ino, (unsigned long long)fragment); 426 if (unlikely(!depth)) { 425 if (unlikely(!depth)) { 427 ufs_warning(sb, "ufs_get_block 426 ufs_warning(sb, "ufs_get_block", "block > big"); 428 err = -EIO; 427 err = -EIO; 429 goto out; 428 goto out; 430 } 429 } 431 430 432 if (UFS_I(inode)->i_lastfrag < UFS_NDI 431 if (UFS_I(inode)->i_lastfrag < UFS_NDIR_FRAGMENT) { 433 unsigned lastfrag = UFS_I(inod 432 unsigned lastfrag = UFS_I(inode)->i_lastfrag; 434 unsigned tailfrags = lastfrag 433 unsigned tailfrags = lastfrag & uspi->s_fpbmask; 435 if (tailfrags && fragment >= l 434 if (tailfrags && fragment >= lastfrag) { 436 if (!ufs_extend_tail(i 435 if (!ufs_extend_tail(inode, fragment, 437 & 436 &err, bh_result->b_page)) 438 goto out; 437 goto out; 439 } 438 } 440 } 439 } 441 440 442 if (depth == 1) { 441 if (depth == 1) { 443 phys64 = ufs_inode_getfrag(ino 442 phys64 = ufs_inode_getfrag(inode, offsets[0], fragment, 444 &er 443 &err, &new, bh_result->b_page); 445 } else { 444 } else { 446 int i; 445 int i; 447 phys64 = ufs_inode_getfrag(ino 446 phys64 = ufs_inode_getfrag(inode, offsets[0], fragment, 448 &er 447 &err, NULL, NULL); 449 for (i = 1; i < depth - 1; i++ 448 for (i = 1; i < depth - 1; i++) 450 phys64 = ufs_inode_get 449 phys64 = ufs_inode_getblock(inode, phys64, offsets[i], 451 450 fragment, &err, NULL, NULL); 452 phys64 = ufs_inode_getblock(in 451 phys64 = ufs_inode_getblock(inode, phys64, offsets[depth - 1], 453 fragme 452 fragment, &err, &new, bh_result->b_page); 454 } 453 } 455 out: 454 out: 456 if (phys64) { 455 if (phys64) { 457 phys64 += frag; 456 phys64 += frag; 458 map_bh(bh_result, sb, phys64); 457 map_bh(bh_result, sb, phys64); 459 if (new) 458 if (new) 460 set_buffer_new(bh_resu 459 set_buffer_new(bh_result); 461 } 460 } 462 mutex_unlock(&UFS_I(inode)->truncate_m 461 mutex_unlock(&UFS_I(inode)->truncate_mutex); 463 return err; 462 return err; 464 463 465 done: 464 done: 466 if (phys64) 465 if (phys64) 467 map_bh(bh_result, sb, phys64 + 466 map_bh(bh_result, sb, phys64 + frag); 468 return 0; 467 return 0; 469 } 468 } 470 469 471 static int ufs_writepages(struct address_space !! 470 static int ufs_writepage(struct page *page, struct writeback_control *wbc) 472 struct writeback_control *wbc) << 473 { 471 { 474 return mpage_writepages(mapping, wbc, !! 472 return block_write_full_page(page,ufs_getfrag_block,wbc); 475 } 473 } 476 474 477 static int ufs_read_folio(struct file *file, s !! 475 static int ufs_readpage(struct file *file, struct page *page) 478 { 476 { 479 return block_read_full_folio(folio, uf !! 477 return block_read_full_page(page,ufs_getfrag_block); 480 } 478 } 481 479 482 int ufs_prepare_chunk(struct page *page, loff_ 480 int ufs_prepare_chunk(struct page *page, loff_t pos, unsigned len) 483 { 481 { 484 return __block_write_begin(page, pos, 482 return __block_write_begin(page, pos, len, ufs_getfrag_block); 485 } 483 } 486 484 487 static void ufs_truncate_blocks(struct inode * 485 static void ufs_truncate_blocks(struct inode *); 488 486 489 static void ufs_write_failed(struct address_sp 487 static void ufs_write_failed(struct address_space *mapping, loff_t to) 490 { 488 { 491 struct inode *inode = mapping->host; 489 struct inode *inode = mapping->host; 492 490 493 if (to > inode->i_size) { 491 if (to > inode->i_size) { 494 truncate_pagecache(inode, inod 492 truncate_pagecache(inode, inode->i_size); 495 ufs_truncate_blocks(inode); 493 ufs_truncate_blocks(inode); 496 } 494 } 497 } 495 } 498 496 499 static int ufs_write_begin(struct file *file, 497 static int ufs_write_begin(struct file *file, struct address_space *mapping, 500 loff_t pos, unsigned l !! 498 loff_t pos, unsigned len, unsigned flags, 501 struct page **pagep, v 499 struct page **pagep, void **fsdata) 502 { 500 { 503 int ret; 501 int ret; 504 502 505 ret = block_write_begin(mapping, pos, !! 503 ret = block_write_begin(mapping, pos, len, flags, pagep, >> 504 ufs_getfrag_block); 506 if (unlikely(ret)) 505 if (unlikely(ret)) 507 ufs_write_failed(mapping, pos 506 ufs_write_failed(mapping, pos + len); 508 507 509 return ret; 508 return ret; 510 } 509 } 511 510 512 static int ufs_write_end(struct file *file, st 511 static int ufs_write_end(struct file *file, struct address_space *mapping, 513 loff_t pos, unsigned l 512 loff_t pos, unsigned len, unsigned copied, 514 struct page *page, voi 513 struct page *page, void *fsdata) 515 { 514 { 516 int ret; 515 int ret; 517 516 518 ret = generic_write_end(file, mapping, 517 ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata); 519 if (ret < len) 518 if (ret < len) 520 ufs_write_failed(mapping, pos 519 ufs_write_failed(mapping, pos + len); 521 return ret; 520 return ret; 522 } 521 } 523 522 524 static sector_t ufs_bmap(struct address_space 523 static sector_t ufs_bmap(struct address_space *mapping, sector_t block) 525 { 524 { 526 return generic_block_bmap(mapping,bloc 525 return generic_block_bmap(mapping,block,ufs_getfrag_block); 527 } 526 } 528 527 529 const struct address_space_operations ufs_aops 528 const struct address_space_operations ufs_aops = { 530 .dirty_folio = block_dirty_folio, 529 .dirty_folio = block_dirty_folio, 531 .invalidate_folio = block_invalidate_f 530 .invalidate_folio = block_invalidate_folio, 532 .read_folio = ufs_read_folio, !! 531 .readpage = ufs_readpage, 533 .writepages = ufs_writepages, !! 532 .writepage = ufs_writepage, 534 .write_begin = ufs_write_begin, 533 .write_begin = ufs_write_begin, 535 .write_end = ufs_write_end, 534 .write_end = ufs_write_end, 536 .migrate_folio = buffer_migrate_folio, << 537 .bmap = ufs_bmap 535 .bmap = ufs_bmap 538 }; 536 }; 539 537 540 static void ufs_set_inode_ops(struct inode *in 538 static void ufs_set_inode_ops(struct inode *inode) 541 { 539 { 542 if (S_ISREG(inode->i_mode)) { 540 if (S_ISREG(inode->i_mode)) { 543 inode->i_op = &ufs_file_inode_ 541 inode->i_op = &ufs_file_inode_operations; 544 inode->i_fop = &ufs_file_opera 542 inode->i_fop = &ufs_file_operations; 545 inode->i_mapping->a_ops = &ufs 543 inode->i_mapping->a_ops = &ufs_aops; 546 } else if (S_ISDIR(inode->i_mode)) { 544 } else if (S_ISDIR(inode->i_mode)) { 547 inode->i_op = &ufs_dir_inode_o 545 inode->i_op = &ufs_dir_inode_operations; 548 inode->i_fop = &ufs_dir_operat 546 inode->i_fop = &ufs_dir_operations; 549 inode->i_mapping->a_ops = &ufs 547 inode->i_mapping->a_ops = &ufs_aops; 550 } else if (S_ISLNK(inode->i_mode)) { 548 } else if (S_ISLNK(inode->i_mode)) { 551 if (!inode->i_blocks) { 549 if (!inode->i_blocks) { 552 inode->i_link = (char 550 inode->i_link = (char *)UFS_I(inode)->i_u1.i_symlink; 553 inode->i_op = &simple_ 551 inode->i_op = &simple_symlink_inode_operations; 554 } else { 552 } else { 555 inode->i_mapping->a_op 553 inode->i_mapping->a_ops = &ufs_aops; 556 inode->i_op = &page_sy 554 inode->i_op = &page_symlink_inode_operations; 557 inode_nohighmem(inode) 555 inode_nohighmem(inode); 558 } 556 } 559 } else 557 } else 560 init_special_inode(inode, inod 558 init_special_inode(inode, inode->i_mode, 561 ufs_get_ino 559 ufs_get_inode_dev(inode->i_sb, UFS_I(inode))); 562 } 560 } 563 561 564 static int ufs1_read_inode(struct inode *inode 562 static int ufs1_read_inode(struct inode *inode, struct ufs_inode *ufs_inode) 565 { 563 { 566 struct ufs_inode_info *ufsi = UFS_I(in 564 struct ufs_inode_info *ufsi = UFS_I(inode); 567 struct super_block *sb = inode->i_sb; 565 struct super_block *sb = inode->i_sb; 568 umode_t mode; 566 umode_t mode; 569 567 570 /* 568 /* 571 * Copy data to the in-core inode. 569 * Copy data to the in-core inode. 572 */ 570 */ 573 inode->i_mode = mode = fs16_to_cpu(sb, 571 inode->i_mode = mode = fs16_to_cpu(sb, ufs_inode->ui_mode); 574 set_nlink(inode, fs16_to_cpu(sb, ufs_i 572 set_nlink(inode, fs16_to_cpu(sb, ufs_inode->ui_nlink)); 575 if (inode->i_nlink == 0) 573 if (inode->i_nlink == 0) 576 return -ESTALE; 574 return -ESTALE; 577 575 578 /* 576 /* 579 * Linux now has 32-bit uid and gid, s 577 * Linux now has 32-bit uid and gid, so we can support EFT. 580 */ 578 */ 581 i_uid_write(inode, ufs_get_inode_uid(s 579 i_uid_write(inode, ufs_get_inode_uid(sb, ufs_inode)); 582 i_gid_write(inode, ufs_get_inode_gid(s 580 i_gid_write(inode, ufs_get_inode_gid(sb, ufs_inode)); 583 581 584 inode->i_size = fs64_to_cpu(sb, ufs_in 582 inode->i_size = fs64_to_cpu(sb, ufs_inode->ui_size); 585 inode_set_atime(inode, !! 583 inode->i_atime.tv_sec = (signed)fs32_to_cpu(sb, ufs_inode->ui_atime.tv_sec); 586 (signed)fs32_to_cpu(sb !! 584 inode->i_ctime.tv_sec = (signed)fs32_to_cpu(sb, ufs_inode->ui_ctime.tv_sec); 587 0); !! 585 inode->i_mtime.tv_sec = (signed)fs32_to_cpu(sb, ufs_inode->ui_mtime.tv_sec); 588 inode_set_ctime(inode, !! 586 inode->i_mtime.tv_nsec = 0; 589 (signed)fs32_to_cpu(sb !! 587 inode->i_atime.tv_nsec = 0; 590 0); !! 588 inode->i_ctime.tv_nsec = 0; 591 inode_set_mtime(inode, << 592 (signed)fs32_to_cpu(sb << 593 0); << 594 inode->i_blocks = fs32_to_cpu(sb, ufs_ 589 inode->i_blocks = fs32_to_cpu(sb, ufs_inode->ui_blocks); 595 inode->i_generation = fs32_to_cpu(sb, 590 inode->i_generation = fs32_to_cpu(sb, ufs_inode->ui_gen); 596 ufsi->i_flags = fs32_to_cpu(sb, ufs_in 591 ufsi->i_flags = fs32_to_cpu(sb, ufs_inode->ui_flags); 597 ufsi->i_shadow = fs32_to_cpu(sb, ufs_i 592 ufsi->i_shadow = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_shadow); 598 ufsi->i_oeftflag = fs32_to_cpu(sb, ufs 593 ufsi->i_oeftflag = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_oeftflag); 599 594 600 595 601 if (S_ISCHR(mode) || S_ISBLK(mode) || 596 if (S_ISCHR(mode) || S_ISBLK(mode) || inode->i_blocks) { 602 memcpy(ufsi->i_u1.i_data, &ufs 597 memcpy(ufsi->i_u1.i_data, &ufs_inode->ui_u2.ui_addr, 603 sizeof(ufs_inode->ui_u2 598 sizeof(ufs_inode->ui_u2.ui_addr)); 604 } else { 599 } else { 605 memcpy(ufsi->i_u1.i_symlink, u 600 memcpy(ufsi->i_u1.i_symlink, ufs_inode->ui_u2.ui_symlink, 606 sizeof(ufs_inode->ui_u2 601 sizeof(ufs_inode->ui_u2.ui_symlink) - 1); 607 ufsi->i_u1.i_symlink[sizeof(uf 602 ufsi->i_u1.i_symlink[sizeof(ufs_inode->ui_u2.ui_symlink) - 1] = 0; 608 } 603 } 609 return 0; 604 return 0; 610 } 605 } 611 606 612 static int ufs2_read_inode(struct inode *inode 607 static int ufs2_read_inode(struct inode *inode, struct ufs2_inode *ufs2_inode) 613 { 608 { 614 struct ufs_inode_info *ufsi = UFS_I(in 609 struct ufs_inode_info *ufsi = UFS_I(inode); 615 struct super_block *sb = inode->i_sb; 610 struct super_block *sb = inode->i_sb; 616 umode_t mode; 611 umode_t mode; 617 612 618 UFSD("Reading ufs2 inode, ino %lu\n", 613 UFSD("Reading ufs2 inode, ino %lu\n", inode->i_ino); 619 /* 614 /* 620 * Copy data to the in-core inode. 615 * Copy data to the in-core inode. 621 */ 616 */ 622 inode->i_mode = mode = fs16_to_cpu(sb, 617 inode->i_mode = mode = fs16_to_cpu(sb, ufs2_inode->ui_mode); 623 set_nlink(inode, fs16_to_cpu(sb, ufs2_ 618 set_nlink(inode, fs16_to_cpu(sb, ufs2_inode->ui_nlink)); 624 if (inode->i_nlink == 0) 619 if (inode->i_nlink == 0) 625 return -ESTALE; 620 return -ESTALE; 626 621 627 /* 622 /* 628 * Linux now has 32-bit uid and gid, s 623 * Linux now has 32-bit uid and gid, so we can support EFT. 629 */ 624 */ 630 i_uid_write(inode, fs32_to_cpu(sb, ufs 625 i_uid_write(inode, fs32_to_cpu(sb, ufs2_inode->ui_uid)); 631 i_gid_write(inode, fs32_to_cpu(sb, ufs 626 i_gid_write(inode, fs32_to_cpu(sb, ufs2_inode->ui_gid)); 632 627 633 inode->i_size = fs64_to_cpu(sb, ufs2_i 628 inode->i_size = fs64_to_cpu(sb, ufs2_inode->ui_size); 634 inode_set_atime(inode, fs64_to_cpu(sb, !! 629 inode->i_atime.tv_sec = fs64_to_cpu(sb, ufs2_inode->ui_atime); 635 fs32_to_cpu(sb, ufs2_i !! 630 inode->i_ctime.tv_sec = fs64_to_cpu(sb, ufs2_inode->ui_ctime); 636 inode_set_ctime(inode, fs64_to_cpu(sb, !! 631 inode->i_mtime.tv_sec = fs64_to_cpu(sb, ufs2_inode->ui_mtime); 637 fs32_to_cpu(sb, ufs2_i !! 632 inode->i_atime.tv_nsec = fs32_to_cpu(sb, ufs2_inode->ui_atimensec); 638 inode_set_mtime(inode, fs64_to_cpu(sb, !! 633 inode->i_ctime.tv_nsec = fs32_to_cpu(sb, ufs2_inode->ui_ctimensec); 639 fs32_to_cpu(sb, ufs2_i !! 634 inode->i_mtime.tv_nsec = fs32_to_cpu(sb, ufs2_inode->ui_mtimensec); 640 inode->i_blocks = fs64_to_cpu(sb, ufs2 635 inode->i_blocks = fs64_to_cpu(sb, ufs2_inode->ui_blocks); 641 inode->i_generation = fs32_to_cpu(sb, 636 inode->i_generation = fs32_to_cpu(sb, ufs2_inode->ui_gen); 642 ufsi->i_flags = fs32_to_cpu(sb, ufs2_i 637 ufsi->i_flags = fs32_to_cpu(sb, ufs2_inode->ui_flags); 643 /* 638 /* 644 ufsi->i_shadow = fs32_to_cpu(sb, ufs_i 639 ufsi->i_shadow = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_shadow); 645 ufsi->i_oeftflag = fs32_to_cpu(sb, ufs 640 ufsi->i_oeftflag = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_oeftflag); 646 */ 641 */ 647 642 648 if (S_ISCHR(mode) || S_ISBLK(mode) || 643 if (S_ISCHR(mode) || S_ISBLK(mode) || inode->i_blocks) { 649 memcpy(ufsi->i_u1.u2_i_data, & 644 memcpy(ufsi->i_u1.u2_i_data, &ufs2_inode->ui_u2.ui_addr, 650 sizeof(ufs2_inode->ui_u 645 sizeof(ufs2_inode->ui_u2.ui_addr)); 651 } else { 646 } else { 652 memcpy(ufsi->i_u1.i_symlink, u 647 memcpy(ufsi->i_u1.i_symlink, ufs2_inode->ui_u2.ui_symlink, 653 sizeof(ufs2_inode->ui_u 648 sizeof(ufs2_inode->ui_u2.ui_symlink) - 1); 654 ufsi->i_u1.i_symlink[sizeof(uf 649 ufsi->i_u1.i_symlink[sizeof(ufs2_inode->ui_u2.ui_symlink) - 1] = 0; 655 } 650 } 656 return 0; 651 return 0; 657 } 652 } 658 653 659 struct inode *ufs_iget(struct super_block *sb, 654 struct inode *ufs_iget(struct super_block *sb, unsigned long ino) 660 { 655 { 661 struct ufs_inode_info *ufsi; 656 struct ufs_inode_info *ufsi; 662 struct ufs_sb_private_info *uspi = UFS 657 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; 663 struct buffer_head * bh; 658 struct buffer_head * bh; 664 struct inode *inode; 659 struct inode *inode; 665 int err = -EIO; 660 int err = -EIO; 666 661 667 UFSD("ENTER, ino %lu\n", ino); 662 UFSD("ENTER, ino %lu\n", ino); 668 663 669 if (ino < UFS_ROOTINO || ino > (uspi-> 664 if (ino < UFS_ROOTINO || ino > (uspi->s_ncg * uspi->s_ipg)) { 670 ufs_warning(sb, "ufs_read_inod 665 ufs_warning(sb, "ufs_read_inode", "bad inode number (%lu)\n", 671 ino); 666 ino); 672 return ERR_PTR(-EIO); 667 return ERR_PTR(-EIO); 673 } 668 } 674 669 675 inode = iget_locked(sb, ino); 670 inode = iget_locked(sb, ino); 676 if (!inode) 671 if (!inode) 677 return ERR_PTR(-ENOMEM); 672 return ERR_PTR(-ENOMEM); 678 if (!(inode->i_state & I_NEW)) 673 if (!(inode->i_state & I_NEW)) 679 return inode; 674 return inode; 680 675 681 ufsi = UFS_I(inode); 676 ufsi = UFS_I(inode); 682 677 683 bh = sb_bread(sb, uspi->s_sbbase + ufs 678 bh = sb_bread(sb, uspi->s_sbbase + ufs_inotofsba(inode->i_ino)); 684 if (!bh) { 679 if (!bh) { 685 ufs_warning(sb, "ufs_read_inod 680 ufs_warning(sb, "ufs_read_inode", "unable to read inode %lu\n", 686 inode->i_ino); 681 inode->i_ino); 687 goto bad_inode; 682 goto bad_inode; 688 } 683 } 689 if ((UFS_SB(sb)->s_flags & UFS_TYPE_MA 684 if ((UFS_SB(sb)->s_flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) { 690 struct ufs2_inode *ufs2_inode 685 struct ufs2_inode *ufs2_inode = (struct ufs2_inode *)bh->b_data; 691 686 692 err = ufs2_read_inode(inode, 687 err = ufs2_read_inode(inode, 693 ufs2_ino 688 ufs2_inode + ufs_inotofsbo(inode->i_ino)); 694 } else { 689 } else { 695 struct ufs_inode *ufs_inode = 690 struct ufs_inode *ufs_inode = (struct ufs_inode *)bh->b_data; 696 691 697 err = ufs1_read_inode(inode, 692 err = ufs1_read_inode(inode, 698 ufs_inod 693 ufs_inode + ufs_inotofsbo(inode->i_ino)); 699 } 694 } 700 brelse(bh); 695 brelse(bh); 701 if (err) 696 if (err) 702 goto bad_inode; 697 goto bad_inode; 703 698 704 inode_inc_iversion(inode); 699 inode_inc_iversion(inode); 705 ufsi->i_lastfrag = 700 ufsi->i_lastfrag = 706 (inode->i_size + uspi->s_fsize 701 (inode->i_size + uspi->s_fsize - 1) >> uspi->s_fshift; 707 ufsi->i_dir_start_lookup = 0; 702 ufsi->i_dir_start_lookup = 0; 708 ufsi->i_osync = 0; 703 ufsi->i_osync = 0; 709 704 710 ufs_set_inode_ops(inode); 705 ufs_set_inode_ops(inode); 711 706 712 UFSD("EXIT\n"); 707 UFSD("EXIT\n"); 713 unlock_new_inode(inode); 708 unlock_new_inode(inode); 714 return inode; 709 return inode; 715 710 716 bad_inode: 711 bad_inode: 717 iget_failed(inode); 712 iget_failed(inode); 718 return ERR_PTR(err); 713 return ERR_PTR(err); 719 } 714 } 720 715 721 static void ufs1_update_inode(struct inode *in 716 static void ufs1_update_inode(struct inode *inode, struct ufs_inode *ufs_inode) 722 { 717 { 723 struct super_block *sb = inode->i_sb; 718 struct super_block *sb = inode->i_sb; 724 struct ufs_inode_info *ufsi = UFS_I(in 719 struct ufs_inode_info *ufsi = UFS_I(inode); 725 720 726 ufs_inode->ui_mode = cpu_to_fs16(sb, i 721 ufs_inode->ui_mode = cpu_to_fs16(sb, inode->i_mode); 727 ufs_inode->ui_nlink = cpu_to_fs16(sb, 722 ufs_inode->ui_nlink = cpu_to_fs16(sb, inode->i_nlink); 728 723 729 ufs_set_inode_uid(sb, ufs_inode, i_uid 724 ufs_set_inode_uid(sb, ufs_inode, i_uid_read(inode)); 730 ufs_set_inode_gid(sb, ufs_inode, i_gid 725 ufs_set_inode_gid(sb, ufs_inode, i_gid_read(inode)); 731 726 732 ufs_inode->ui_size = cpu_to_fs64(sb, i 727 ufs_inode->ui_size = cpu_to_fs64(sb, inode->i_size); 733 ufs_inode->ui_atime.tv_sec = cpu_to_fs !! 728 ufs_inode->ui_atime.tv_sec = cpu_to_fs32(sb, inode->i_atime.tv_sec); 734 << 735 ufs_inode->ui_atime.tv_usec = 0; 729 ufs_inode->ui_atime.tv_usec = 0; 736 ufs_inode->ui_ctime.tv_sec = cpu_to_fs !! 730 ufs_inode->ui_ctime.tv_sec = cpu_to_fs32(sb, inode->i_ctime.tv_sec); 737 << 738 ufs_inode->ui_ctime.tv_usec = 0; 731 ufs_inode->ui_ctime.tv_usec = 0; 739 ufs_inode->ui_mtime.tv_sec = cpu_to_fs !! 732 ufs_inode->ui_mtime.tv_sec = cpu_to_fs32(sb, inode->i_mtime.tv_sec); 740 << 741 ufs_inode->ui_mtime.tv_usec = 0; 733 ufs_inode->ui_mtime.tv_usec = 0; 742 ufs_inode->ui_blocks = cpu_to_fs32(sb, 734 ufs_inode->ui_blocks = cpu_to_fs32(sb, inode->i_blocks); 743 ufs_inode->ui_flags = cpu_to_fs32(sb, 735 ufs_inode->ui_flags = cpu_to_fs32(sb, ufsi->i_flags); 744 ufs_inode->ui_gen = cpu_to_fs32(sb, in 736 ufs_inode->ui_gen = cpu_to_fs32(sb, inode->i_generation); 745 737 746 if ((UFS_SB(sb)->s_flags & UFS_UID_MAS 738 if ((UFS_SB(sb)->s_flags & UFS_UID_MASK) == UFS_UID_EFT) { 747 ufs_inode->ui_u3.ui_sun.ui_sha 739 ufs_inode->ui_u3.ui_sun.ui_shadow = cpu_to_fs32(sb, ufsi->i_shadow); 748 ufs_inode->ui_u3.ui_sun.ui_oef 740 ufs_inode->ui_u3.ui_sun.ui_oeftflag = cpu_to_fs32(sb, ufsi->i_oeftflag); 749 } 741 } 750 742 751 if (S_ISCHR(inode->i_mode) || S_ISBLK( 743 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { 752 /* ufs_inode->ui_u2.ui_addr.ui 744 /* ufs_inode->ui_u2.ui_addr.ui_db[0] = cpu_to_fs32(sb, inode->i_rdev); */ 753 ufs_inode->ui_u2.ui_addr.ui_db 745 ufs_inode->ui_u2.ui_addr.ui_db[0] = ufsi->i_u1.i_data[0]; 754 } else if (inode->i_blocks) { 746 } else if (inode->i_blocks) { 755 memcpy(&ufs_inode->ui_u2.ui_ad 747 memcpy(&ufs_inode->ui_u2.ui_addr, ufsi->i_u1.i_data, 756 sizeof(ufs_inode->ui_u2 748 sizeof(ufs_inode->ui_u2.ui_addr)); 757 } 749 } 758 else { 750 else { 759 memcpy(&ufs_inode->ui_u2.ui_sy 751 memcpy(&ufs_inode->ui_u2.ui_symlink, ufsi->i_u1.i_symlink, 760 sizeof(ufs_inode->ui_u2 752 sizeof(ufs_inode->ui_u2.ui_symlink)); 761 } 753 } 762 754 763 if (!inode->i_nlink) 755 if (!inode->i_nlink) 764 memset (ufs_inode, 0, sizeof(s 756 memset (ufs_inode, 0, sizeof(struct ufs_inode)); 765 } 757 } 766 758 767 static void ufs2_update_inode(struct inode *in 759 static void ufs2_update_inode(struct inode *inode, struct ufs2_inode *ufs_inode) 768 { 760 { 769 struct super_block *sb = inode->i_sb; 761 struct super_block *sb = inode->i_sb; 770 struct ufs_inode_info *ufsi = UFS_I(in 762 struct ufs_inode_info *ufsi = UFS_I(inode); 771 763 772 UFSD("ENTER\n"); 764 UFSD("ENTER\n"); 773 ufs_inode->ui_mode = cpu_to_fs16(sb, i 765 ufs_inode->ui_mode = cpu_to_fs16(sb, inode->i_mode); 774 ufs_inode->ui_nlink = cpu_to_fs16(sb, 766 ufs_inode->ui_nlink = cpu_to_fs16(sb, inode->i_nlink); 775 767 776 ufs_inode->ui_uid = cpu_to_fs32(sb, i_ 768 ufs_inode->ui_uid = cpu_to_fs32(sb, i_uid_read(inode)); 777 ufs_inode->ui_gid = cpu_to_fs32(sb, i_ 769 ufs_inode->ui_gid = cpu_to_fs32(sb, i_gid_read(inode)); 778 770 779 ufs_inode->ui_size = cpu_to_fs64(sb, i 771 ufs_inode->ui_size = cpu_to_fs64(sb, inode->i_size); 780 ufs_inode->ui_atime = cpu_to_fs64(sb, !! 772 ufs_inode->ui_atime = cpu_to_fs64(sb, inode->i_atime.tv_sec); 781 ufs_inode->ui_atimensec = cpu_to_fs32( !! 773 ufs_inode->ui_atimensec = cpu_to_fs32(sb, inode->i_atime.tv_nsec); 782 !! 774 ufs_inode->ui_ctime = cpu_to_fs64(sb, inode->i_ctime.tv_sec); 783 ufs_inode->ui_ctime = cpu_to_fs64(sb, !! 775 ufs_inode->ui_ctimensec = cpu_to_fs32(sb, inode->i_ctime.tv_nsec); 784 ufs_inode->ui_ctimensec = cpu_to_fs32( !! 776 ufs_inode->ui_mtime = cpu_to_fs64(sb, inode->i_mtime.tv_sec); 785 !! 777 ufs_inode->ui_mtimensec = cpu_to_fs32(sb, inode->i_mtime.tv_nsec); 786 ufs_inode->ui_mtime = cpu_to_fs64(sb, << 787 ufs_inode->ui_mtimensec = cpu_to_fs32( << 788 << 789 778 790 ufs_inode->ui_blocks = cpu_to_fs64(sb, 779 ufs_inode->ui_blocks = cpu_to_fs64(sb, inode->i_blocks); 791 ufs_inode->ui_flags = cpu_to_fs32(sb, 780 ufs_inode->ui_flags = cpu_to_fs32(sb, ufsi->i_flags); 792 ufs_inode->ui_gen = cpu_to_fs32(sb, in 781 ufs_inode->ui_gen = cpu_to_fs32(sb, inode->i_generation); 793 782 794 if (S_ISCHR(inode->i_mode) || S_ISBLK( 783 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { 795 /* ufs_inode->ui_u2.ui_addr.ui 784 /* ufs_inode->ui_u2.ui_addr.ui_db[0] = cpu_to_fs32(sb, inode->i_rdev); */ 796 ufs_inode->ui_u2.ui_addr.ui_db 785 ufs_inode->ui_u2.ui_addr.ui_db[0] = ufsi->i_u1.u2_i_data[0]; 797 } else if (inode->i_blocks) { 786 } else if (inode->i_blocks) { 798 memcpy(&ufs_inode->ui_u2.ui_ad 787 memcpy(&ufs_inode->ui_u2.ui_addr, ufsi->i_u1.u2_i_data, 799 sizeof(ufs_inode->ui_u2 788 sizeof(ufs_inode->ui_u2.ui_addr)); 800 } else { 789 } else { 801 memcpy(&ufs_inode->ui_u2.ui_sy 790 memcpy(&ufs_inode->ui_u2.ui_symlink, ufsi->i_u1.i_symlink, 802 sizeof(ufs_inode->ui_u2 791 sizeof(ufs_inode->ui_u2.ui_symlink)); 803 } 792 } 804 793 805 if (!inode->i_nlink) 794 if (!inode->i_nlink) 806 memset (ufs_inode, 0, sizeof(s 795 memset (ufs_inode, 0, sizeof(struct ufs2_inode)); 807 UFSD("EXIT\n"); 796 UFSD("EXIT\n"); 808 } 797 } 809 798 810 static int ufs_update_inode(struct inode * ino 799 static int ufs_update_inode(struct inode * inode, int do_sync) 811 { 800 { 812 struct super_block *sb = inode->i_sb; 801 struct super_block *sb = inode->i_sb; 813 struct ufs_sb_private_info *uspi = UFS 802 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; 814 struct buffer_head * bh; 803 struct buffer_head * bh; 815 804 816 UFSD("ENTER, ino %lu\n", inode->i_ino) 805 UFSD("ENTER, ino %lu\n", inode->i_ino); 817 806 818 if (inode->i_ino < UFS_ROOTINO || 807 if (inode->i_ino < UFS_ROOTINO || 819 inode->i_ino > (uspi->s_ncg * uspi 808 inode->i_ino > (uspi->s_ncg * uspi->s_ipg)) { 820 ufs_warning (sb, "ufs_read_ino 809 ufs_warning (sb, "ufs_read_inode", "bad inode number (%lu)\n", inode->i_ino); 821 return -1; 810 return -1; 822 } 811 } 823 812 824 bh = sb_bread(sb, ufs_inotofsba(inode- 813 bh = sb_bread(sb, ufs_inotofsba(inode->i_ino)); 825 if (!bh) { 814 if (!bh) { 826 ufs_warning (sb, "ufs_read_ino 815 ufs_warning (sb, "ufs_read_inode", "unable to read inode %lu\n", inode->i_ino); 827 return -1; 816 return -1; 828 } 817 } 829 if (uspi->fs_magic == UFS2_MAGIC) { 818 if (uspi->fs_magic == UFS2_MAGIC) { 830 struct ufs2_inode *ufs2_inode 819 struct ufs2_inode *ufs2_inode = (struct ufs2_inode *)bh->b_data; 831 820 832 ufs2_update_inode(inode, 821 ufs2_update_inode(inode, 833 ufs2_inode + 822 ufs2_inode + ufs_inotofsbo(inode->i_ino)); 834 } else { 823 } else { 835 struct ufs_inode *ufs_inode = 824 struct ufs_inode *ufs_inode = (struct ufs_inode *) bh->b_data; 836 825 837 ufs1_update_inode(inode, ufs_i 826 ufs1_update_inode(inode, ufs_inode + ufs_inotofsbo(inode->i_ino)); 838 } 827 } 839 828 840 mark_buffer_dirty(bh); 829 mark_buffer_dirty(bh); 841 if (do_sync) 830 if (do_sync) 842 sync_dirty_buffer(bh); 831 sync_dirty_buffer(bh); 843 brelse (bh); 832 brelse (bh); 844 833 845 UFSD("EXIT\n"); 834 UFSD("EXIT\n"); 846 return 0; 835 return 0; 847 } 836 } 848 837 849 int ufs_write_inode(struct inode *inode, struc 838 int ufs_write_inode(struct inode *inode, struct writeback_control *wbc) 850 { 839 { 851 return ufs_update_inode(inode, wbc->sy 840 return ufs_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL); 852 } 841 } 853 842 854 int ufs_sync_inode (struct inode *inode) 843 int ufs_sync_inode (struct inode *inode) 855 { 844 { 856 return ufs_update_inode (inode, 1); 845 return ufs_update_inode (inode, 1); 857 } 846 } 858 847 859 void ufs_evict_inode(struct inode * inode) 848 void ufs_evict_inode(struct inode * inode) 860 { 849 { 861 int want_delete = 0; 850 int want_delete = 0; 862 851 863 if (!inode->i_nlink && !is_bad_inode(i 852 if (!inode->i_nlink && !is_bad_inode(inode)) 864 want_delete = 1; 853 want_delete = 1; 865 854 866 truncate_inode_pages_final(&inode->i_d 855 truncate_inode_pages_final(&inode->i_data); 867 if (want_delete) { 856 if (want_delete) { 868 inode->i_size = 0; 857 inode->i_size = 0; 869 if (inode->i_blocks && 858 if (inode->i_blocks && 870 (S_ISREG(inode->i_mode) || 859 (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 871 S_ISLNK(inode->i_mode))) 860 S_ISLNK(inode->i_mode))) 872 ufs_truncate_blocks(in 861 ufs_truncate_blocks(inode); 873 ufs_update_inode(inode, inode_ 862 ufs_update_inode(inode, inode_needs_sync(inode)); 874 } 863 } 875 864 876 invalidate_inode_buffers(inode); 865 invalidate_inode_buffers(inode); 877 clear_inode(inode); 866 clear_inode(inode); 878 867 879 if (want_delete) 868 if (want_delete) 880 ufs_free_inode(inode); 869 ufs_free_inode(inode); 881 } 870 } 882 871 883 struct to_free { 872 struct to_free { 884 struct inode *inode; 873 struct inode *inode; 885 u64 to; 874 u64 to; 886 unsigned count; 875 unsigned count; 887 }; 876 }; 888 877 889 static inline void free_data(struct to_free *c 878 static inline void free_data(struct to_free *ctx, u64 from, unsigned count) 890 { 879 { 891 if (ctx->count && ctx->to != from) { 880 if (ctx->count && ctx->to != from) { 892 ufs_free_blocks(ctx->inode, ct 881 ufs_free_blocks(ctx->inode, ctx->to - ctx->count, ctx->count); 893 ctx->count = 0; 882 ctx->count = 0; 894 } 883 } 895 ctx->count += count; 884 ctx->count += count; 896 ctx->to = from + count; 885 ctx->to = from + count; 897 } 886 } 898 887 899 #define DIRECT_FRAGMENT ((inode->i_size + uspi 888 #define DIRECT_FRAGMENT ((inode->i_size + uspi->s_fsize - 1) >> uspi->s_fshift) 900 889 901 static void ufs_trunc_direct(struct inode *ino 890 static void ufs_trunc_direct(struct inode *inode) 902 { 891 { 903 struct ufs_inode_info *ufsi = UFS_I(in 892 struct ufs_inode_info *ufsi = UFS_I(inode); 904 struct super_block * sb; 893 struct super_block * sb; 905 struct ufs_sb_private_info * uspi; 894 struct ufs_sb_private_info * uspi; 906 void *p; 895 void *p; 907 u64 frag1, frag2, frag3, frag4, block1 896 u64 frag1, frag2, frag3, frag4, block1, block2; 908 struct to_free ctx = {.inode = inode}; 897 struct to_free ctx = {.inode = inode}; 909 unsigned i, tmp; 898 unsigned i, tmp; 910 899 911 UFSD("ENTER: ino %lu\n", inode->i_ino) 900 UFSD("ENTER: ino %lu\n", inode->i_ino); 912 901 913 sb = inode->i_sb; 902 sb = inode->i_sb; 914 uspi = UFS_SB(sb)->s_uspi; 903 uspi = UFS_SB(sb)->s_uspi; 915 904 916 frag1 = DIRECT_FRAGMENT; 905 frag1 = DIRECT_FRAGMENT; 917 frag4 = min_t(u64, UFS_NDIR_FRAGMENT, 906 frag4 = min_t(u64, UFS_NDIR_FRAGMENT, ufsi->i_lastfrag); 918 frag2 = ((frag1 & uspi->s_fpbmask) ? ( 907 frag2 = ((frag1 & uspi->s_fpbmask) ? ((frag1 | uspi->s_fpbmask) + 1) : frag1); 919 frag3 = frag4 & ~uspi->s_fpbmask; 908 frag3 = frag4 & ~uspi->s_fpbmask; 920 block1 = block2 = 0; 909 block1 = block2 = 0; 921 if (frag2 > frag3) { 910 if (frag2 > frag3) { 922 frag2 = frag4; 911 frag2 = frag4; 923 frag3 = frag4 = 0; 912 frag3 = frag4 = 0; 924 } else if (frag2 < frag3) { 913 } else if (frag2 < frag3) { 925 block1 = ufs_fragstoblks (frag 914 block1 = ufs_fragstoblks (frag2); 926 block2 = ufs_fragstoblks (frag 915 block2 = ufs_fragstoblks (frag3); 927 } 916 } 928 917 929 UFSD("ino %lu, frag1 %llu, frag2 %llu, 918 UFSD("ino %lu, frag1 %llu, frag2 %llu, block1 %llu, block2 %llu," 930 " frag3 %llu, frag4 %llu\n", inod 919 " frag3 %llu, frag4 %llu\n", inode->i_ino, 931 (unsigned long long)frag1, (unsig 920 (unsigned long long)frag1, (unsigned long long)frag2, 932 (unsigned long long)block1, (unsi 921 (unsigned long long)block1, (unsigned long long)block2, 933 (unsigned long long)frag3, (unsig 922 (unsigned long long)frag3, (unsigned long long)frag4); 934 923 935 if (frag1 >= frag2) 924 if (frag1 >= frag2) 936 goto next1; 925 goto next1; 937 926 938 /* 927 /* 939 * Free first free fragments 928 * Free first free fragments 940 */ 929 */ 941 p = ufs_get_direct_data_ptr(uspi, ufsi 930 p = ufs_get_direct_data_ptr(uspi, ufsi, ufs_fragstoblks(frag1)); 942 tmp = ufs_data_ptr_to_cpu(sb, p); 931 tmp = ufs_data_ptr_to_cpu(sb, p); 943 if (!tmp ) 932 if (!tmp ) 944 ufs_panic (sb, "ufs_trunc_dire 933 ufs_panic (sb, "ufs_trunc_direct", "internal error"); 945 frag2 -= frag1; 934 frag2 -= frag1; 946 frag1 = ufs_fragnum (frag1); 935 frag1 = ufs_fragnum (frag1); 947 936 948 ufs_free_fragments(inode, tmp + frag1, 937 ufs_free_fragments(inode, tmp + frag1, frag2); 949 938 950 next1: 939 next1: 951 /* 940 /* 952 * Free whole blocks 941 * Free whole blocks 953 */ 942 */ 954 for (i = block1 ; i < block2; i++) { 943 for (i = block1 ; i < block2; i++) { 955 p = ufs_get_direct_data_ptr(us 944 p = ufs_get_direct_data_ptr(uspi, ufsi, i); 956 tmp = ufs_data_ptr_to_cpu(sb, 945 tmp = ufs_data_ptr_to_cpu(sb, p); 957 if (!tmp) 946 if (!tmp) 958 continue; 947 continue; 959 write_seqlock(&ufsi->meta_lock 948 write_seqlock(&ufsi->meta_lock); 960 ufs_data_ptr_clear(uspi, p); 949 ufs_data_ptr_clear(uspi, p); 961 write_sequnlock(&ufsi->meta_lo 950 write_sequnlock(&ufsi->meta_lock); 962 951 963 free_data(&ctx, tmp, uspi->s_f 952 free_data(&ctx, tmp, uspi->s_fpb); 964 } 953 } 965 954 966 free_data(&ctx, 0, 0); 955 free_data(&ctx, 0, 0); 967 956 968 if (frag3 >= frag4) 957 if (frag3 >= frag4) 969 goto next3; 958 goto next3; 970 959 971 /* 960 /* 972 * Free last free fragments 961 * Free last free fragments 973 */ 962 */ 974 p = ufs_get_direct_data_ptr(uspi, ufsi 963 p = ufs_get_direct_data_ptr(uspi, ufsi, ufs_fragstoblks(frag3)); 975 tmp = ufs_data_ptr_to_cpu(sb, p); 964 tmp = ufs_data_ptr_to_cpu(sb, p); 976 if (!tmp ) 965 if (!tmp ) 977 ufs_panic(sb, "ufs_truncate_di 966 ufs_panic(sb, "ufs_truncate_direct", "internal error"); 978 frag4 = ufs_fragnum (frag4); 967 frag4 = ufs_fragnum (frag4); 979 write_seqlock(&ufsi->meta_lock); 968 write_seqlock(&ufsi->meta_lock); 980 ufs_data_ptr_clear(uspi, p); 969 ufs_data_ptr_clear(uspi, p); 981 write_sequnlock(&ufsi->meta_lock); 970 write_sequnlock(&ufsi->meta_lock); 982 971 983 ufs_free_fragments (inode, tmp, frag4) 972 ufs_free_fragments (inode, tmp, frag4); 984 next3: 973 next3: 985 974 986 UFSD("EXIT: ino %lu\n", inode->i_ino); 975 UFSD("EXIT: ino %lu\n", inode->i_ino); 987 } 976 } 988 977 989 static void free_full_branch(struct inode *ino 978 static void free_full_branch(struct inode *inode, u64 ind_block, int depth) 990 { 979 { 991 struct super_block *sb = inode->i_sb; 980 struct super_block *sb = inode->i_sb; 992 struct ufs_sb_private_info *uspi = UFS 981 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; 993 struct ufs_buffer_head *ubh = ubh_brea 982 struct ufs_buffer_head *ubh = ubh_bread(sb, ind_block, uspi->s_bsize); 994 unsigned i; 983 unsigned i; 995 984 996 if (!ubh) 985 if (!ubh) 997 return; 986 return; 998 987 999 if (--depth) { 988 if (--depth) { 1000 for (i = 0; i < uspi->s_apb; 989 for (i = 0; i < uspi->s_apb; i++) { 1001 void *p = ubh_get_dat 990 void *p = ubh_get_data_ptr(uspi, ubh, i); 1002 u64 block = ufs_data_ 991 u64 block = ufs_data_ptr_to_cpu(sb, p); 1003 if (block) 992 if (block) 1004 free_full_bra 993 free_full_branch(inode, block, depth); 1005 } 994 } 1006 } else { 995 } else { 1007 struct to_free ctx = {.inode 996 struct to_free ctx = {.inode = inode}; 1008 997 1009 for (i = 0; i < uspi->s_apb; 998 for (i = 0; i < uspi->s_apb; i++) { 1010 void *p = ubh_get_dat 999 void *p = ubh_get_data_ptr(uspi, ubh, i); 1011 u64 block = ufs_data_ 1000 u64 block = ufs_data_ptr_to_cpu(sb, p); 1012 if (block) 1001 if (block) 1013 free_data(&ct 1002 free_data(&ctx, block, uspi->s_fpb); 1014 } 1003 } 1015 free_data(&ctx, 0, 0); 1004 free_data(&ctx, 0, 0); 1016 } 1005 } 1017 1006 1018 ubh_bforget(ubh); 1007 ubh_bforget(ubh); 1019 ufs_free_blocks(inode, ind_block, usp 1008 ufs_free_blocks(inode, ind_block, uspi->s_fpb); 1020 } 1009 } 1021 1010 1022 static void free_branch_tail(struct inode *in 1011 static void free_branch_tail(struct inode *inode, unsigned from, struct ufs_buffer_head *ubh, int depth) 1023 { 1012 { 1024 struct super_block *sb = inode->i_sb; 1013 struct super_block *sb = inode->i_sb; 1025 struct ufs_sb_private_info *uspi = UF 1014 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; 1026 unsigned i; 1015 unsigned i; 1027 1016 1028 if (--depth) { 1017 if (--depth) { 1029 for (i = from; i < uspi->s_ap 1018 for (i = from; i < uspi->s_apb ; i++) { 1030 void *p = ubh_get_dat 1019 void *p = ubh_get_data_ptr(uspi, ubh, i); 1031 u64 block = ufs_data_ 1020 u64 block = ufs_data_ptr_to_cpu(sb, p); 1032 if (block) { 1021 if (block) { 1033 write_seqlock 1022 write_seqlock(&UFS_I(inode)->meta_lock); 1034 ufs_data_ptr_ 1023 ufs_data_ptr_clear(uspi, p); 1035 write_sequnlo 1024 write_sequnlock(&UFS_I(inode)->meta_lock); 1036 ubh_mark_buff 1025 ubh_mark_buffer_dirty(ubh); 1037 free_full_bra 1026 free_full_branch(inode, block, depth); 1038 } 1027 } 1039 } 1028 } 1040 } else { 1029 } else { 1041 struct to_free ctx = {.inode 1030 struct to_free ctx = {.inode = inode}; 1042 1031 1043 for (i = from; i < uspi->s_ap 1032 for (i = from; i < uspi->s_apb; i++) { 1044 void *p = ubh_get_dat 1033 void *p = ubh_get_data_ptr(uspi, ubh, i); 1045 u64 block = ufs_data_ 1034 u64 block = ufs_data_ptr_to_cpu(sb, p); 1046 if (block) { 1035 if (block) { 1047 write_seqlock 1036 write_seqlock(&UFS_I(inode)->meta_lock); 1048 ufs_data_ptr_ 1037 ufs_data_ptr_clear(uspi, p); 1049 write_sequnlo 1038 write_sequnlock(&UFS_I(inode)->meta_lock); 1050 ubh_mark_buff 1039 ubh_mark_buffer_dirty(ubh); 1051 free_data(&ct 1040 free_data(&ctx, block, uspi->s_fpb); 1052 } 1041 } 1053 } 1042 } 1054 free_data(&ctx, 0, 0); 1043 free_data(&ctx, 0, 0); 1055 } 1044 } 1056 if (IS_SYNC(inode) && ubh_buffer_dirt 1045 if (IS_SYNC(inode) && ubh_buffer_dirty(ubh)) 1057 ubh_sync_block(ubh); 1046 ubh_sync_block(ubh); 1058 ubh_brelse(ubh); 1047 ubh_brelse(ubh); 1059 } 1048 } 1060 1049 1061 static int ufs_alloc_lastblock(struct inode * 1050 static int ufs_alloc_lastblock(struct inode *inode, loff_t size) 1062 { 1051 { 1063 int err = 0; 1052 int err = 0; 1064 struct super_block *sb = inode->i_sb; 1053 struct super_block *sb = inode->i_sb; 1065 struct address_space *mapping = inode 1054 struct address_space *mapping = inode->i_mapping; 1066 struct ufs_sb_private_info *uspi = UF 1055 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; 1067 unsigned i, end; 1056 unsigned i, end; 1068 sector_t lastfrag; 1057 sector_t lastfrag; 1069 struct folio *folio; !! 1058 struct page *lastpage; 1070 struct buffer_head *bh; 1059 struct buffer_head *bh; 1071 u64 phys64; 1060 u64 phys64; 1072 1061 1073 lastfrag = (size + uspi->s_fsize - 1) 1062 lastfrag = (size + uspi->s_fsize - 1) >> uspi->s_fshift; 1074 1063 1075 if (!lastfrag) 1064 if (!lastfrag) 1076 goto out; 1065 goto out; 1077 1066 1078 lastfrag--; 1067 lastfrag--; 1079 1068 1080 folio = ufs_get_locked_folio(mapping, !! 1069 lastpage = ufs_get_locked_page(mapping, lastfrag >> 1081 (PAGE_ 1070 (PAGE_SHIFT - inode->i_blkbits)); 1082 if (IS_ERR(folio)) { !! 1071 if (IS_ERR(lastpage)) { 1083 err = -EIO; !! 1072 err = -EIO; 1084 goto out; !! 1073 goto out; 1085 } !! 1074 } >> 1075 >> 1076 end = lastfrag & ((1 << (PAGE_SHIFT - inode->i_blkbits)) - 1); >> 1077 bh = page_buffers(lastpage); >> 1078 for (i = 0; i < end; ++i) >> 1079 bh = bh->b_this_page; 1086 1080 1087 end = lastfrag & ((1 << (PAGE_SHIFT - << 1088 bh = folio_buffers(folio); << 1089 for (i = 0; i < end; ++i) << 1090 bh = bh->b_this_page; << 1091 1081 1092 err = ufs_getfrag_block(inode, lastfra 1082 err = ufs_getfrag_block(inode, lastfrag, bh, 1); 1093 1083 1094 if (unlikely(err)) 1084 if (unlikely(err)) 1095 goto out_unlock; 1085 goto out_unlock; 1096 1086 1097 if (buffer_new(bh)) { 1087 if (buffer_new(bh)) { 1098 clear_buffer_new(bh); 1088 clear_buffer_new(bh); 1099 clean_bdev_bh_alias(bh); 1089 clean_bdev_bh_alias(bh); 1100 /* 1090 /* 1101 * we do not zeroize fragment, 1091 * we do not zeroize fragment, because of 1102 * if it maped to hole, it alr 1092 * if it maped to hole, it already contains zeroes 1103 */ 1093 */ 1104 set_buffer_uptodate(bh); 1094 set_buffer_uptodate(bh); 1105 mark_buffer_dirty(bh); 1095 mark_buffer_dirty(bh); 1106 folio_mark_dirty(folio); !! 1096 set_page_dirty(lastpage); 1107 } 1097 } 1108 1098 1109 if (lastfrag >= UFS_IND_FRAGMENT) { 1099 if (lastfrag >= UFS_IND_FRAGMENT) { 1110 end = uspi->s_fpb - ufs_fragnu 1100 end = uspi->s_fpb - ufs_fragnum(lastfrag) - 1; 1111 phys64 = bh->b_blocknr + 1; 1101 phys64 = bh->b_blocknr + 1; 1112 for (i = 0; i < end; ++i) { 1102 for (i = 0; i < end; ++i) { 1113 bh = sb_getblk(sb, i + 1103 bh = sb_getblk(sb, i + phys64); 1114 lock_buffer(bh); 1104 lock_buffer(bh); 1115 memset(bh->b_data, 0, 1105 memset(bh->b_data, 0, sb->s_blocksize); 1116 set_buffer_uptodate(bh 1106 set_buffer_uptodate(bh); 1117 mark_buffer_dirty(bh); 1107 mark_buffer_dirty(bh); 1118 unlock_buffer(bh); 1108 unlock_buffer(bh); 1119 sync_dirty_buffer(bh); 1109 sync_dirty_buffer(bh); 1120 brelse(bh); 1110 brelse(bh); 1121 } 1111 } 1122 } 1112 } 1123 out_unlock: 1113 out_unlock: 1124 ufs_put_locked_folio(folio); !! 1114 ufs_put_locked_page(lastpage); 1125 out: 1115 out: 1126 return err; 1116 return err; 1127 } 1117 } 1128 1118 1129 static void ufs_truncate_blocks(struct inode 1119 static void ufs_truncate_blocks(struct inode *inode) 1130 { 1120 { 1131 struct ufs_inode_info *ufsi = UFS_I(i 1121 struct ufs_inode_info *ufsi = UFS_I(inode); 1132 struct super_block *sb = inode->i_sb; 1122 struct super_block *sb = inode->i_sb; 1133 struct ufs_sb_private_info *uspi = UF 1123 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; 1134 unsigned offsets[4]; 1124 unsigned offsets[4]; 1135 int depth; 1125 int depth; 1136 int depth2; 1126 int depth2; 1137 unsigned i; 1127 unsigned i; 1138 struct ufs_buffer_head *ubh[3]; 1128 struct ufs_buffer_head *ubh[3]; 1139 void *p; 1129 void *p; 1140 u64 block; 1130 u64 block; 1141 1131 1142 if (inode->i_size) { 1132 if (inode->i_size) { 1143 sector_t last = (inode->i_siz 1133 sector_t last = (inode->i_size - 1) >> uspi->s_bshift; 1144 depth = ufs_block_to_path(ino 1134 depth = ufs_block_to_path(inode, last, offsets); 1145 if (!depth) 1135 if (!depth) 1146 return; 1136 return; 1147 } else { 1137 } else { 1148 depth = 1; 1138 depth = 1; 1149 } 1139 } 1150 1140 1151 for (depth2 = depth - 1; depth2; dept 1141 for (depth2 = depth - 1; depth2; depth2--) 1152 if (offsets[depth2] != uspi-> 1142 if (offsets[depth2] != uspi->s_apb - 1) 1153 break; 1143 break; 1154 1144 1155 mutex_lock(&ufsi->truncate_mutex); 1145 mutex_lock(&ufsi->truncate_mutex); 1156 if (depth == 1) { 1146 if (depth == 1) { 1157 ufs_trunc_direct(inode); 1147 ufs_trunc_direct(inode); 1158 offsets[0] = UFS_IND_BLOCK; 1148 offsets[0] = UFS_IND_BLOCK; 1159 } else { 1149 } else { 1160 /* get the blocks that should 1150 /* get the blocks that should be partially emptied */ 1161 p = ufs_get_direct_data_ptr(u 1151 p = ufs_get_direct_data_ptr(uspi, ufsi, offsets[0]++); 1162 for (i = 0; i < depth2; i++) 1152 for (i = 0; i < depth2; i++) { 1163 block = ufs_data_ptr_ 1153 block = ufs_data_ptr_to_cpu(sb, p); 1164 if (!block) 1154 if (!block) 1165 break; 1155 break; 1166 ubh[i] = ubh_bread(sb 1156 ubh[i] = ubh_bread(sb, block, uspi->s_bsize); 1167 if (!ubh[i]) { 1157 if (!ubh[i]) { 1168 write_seqlock 1158 write_seqlock(&ufsi->meta_lock); 1169 ufs_data_ptr_ 1159 ufs_data_ptr_clear(uspi, p); 1170 write_sequnlo 1160 write_sequnlock(&ufsi->meta_lock); 1171 break; 1161 break; 1172 } 1162 } 1173 p = ubh_get_data_ptr( 1163 p = ubh_get_data_ptr(uspi, ubh[i], offsets[i + 1]++); 1174 } 1164 } 1175 while (i--) 1165 while (i--) 1176 free_branch_tail(inod 1166 free_branch_tail(inode, offsets[i + 1], ubh[i], depth - i - 1); 1177 } 1167 } 1178 for (i = offsets[0]; i <= UFS_TIND_BL 1168 for (i = offsets[0]; i <= UFS_TIND_BLOCK; i++) { 1179 p = ufs_get_direct_data_ptr(u 1169 p = ufs_get_direct_data_ptr(uspi, ufsi, i); 1180 block = ufs_data_ptr_to_cpu(s 1170 block = ufs_data_ptr_to_cpu(sb, p); 1181 if (block) { 1171 if (block) { 1182 write_seqlock(&ufsi-> 1172 write_seqlock(&ufsi->meta_lock); 1183 ufs_data_ptr_clear(us 1173 ufs_data_ptr_clear(uspi, p); 1184 write_sequnlock(&ufsi 1174 write_sequnlock(&ufsi->meta_lock); 1185 free_full_branch(inod 1175 free_full_branch(inode, block, i - UFS_IND_BLOCK + 1); 1186 } 1176 } 1187 } 1177 } 1188 read_seqlock_excl(&ufsi->meta_lock); 1178 read_seqlock_excl(&ufsi->meta_lock); 1189 ufsi->i_lastfrag = DIRECT_FRAGMENT; 1179 ufsi->i_lastfrag = DIRECT_FRAGMENT; 1190 read_sequnlock_excl(&ufsi->meta_lock) 1180 read_sequnlock_excl(&ufsi->meta_lock); 1191 mark_inode_dirty(inode); 1181 mark_inode_dirty(inode); 1192 mutex_unlock(&ufsi->truncate_mutex); 1182 mutex_unlock(&ufsi->truncate_mutex); 1193 } 1183 } 1194 1184 1195 static int ufs_truncate(struct inode *inode, 1185 static int ufs_truncate(struct inode *inode, loff_t size) 1196 { 1186 { 1197 int err = 0; 1187 int err = 0; 1198 1188 1199 UFSD("ENTER: ino %lu, i_size: %llu, o 1189 UFSD("ENTER: ino %lu, i_size: %llu, old_i_size: %llu\n", 1200 inode->i_ino, (unsigned long lon 1190 inode->i_ino, (unsigned long long)size, 1201 (unsigned long long)i_size_read( 1191 (unsigned long long)i_size_read(inode)); 1202 1192 1203 if (!(S_ISREG(inode->i_mode) || S_ISD 1193 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 1204 S_ISLNK(inode->i_mode))) 1194 S_ISLNK(inode->i_mode))) 1205 return -EINVAL; 1195 return -EINVAL; 1206 if (IS_APPEND(inode) || IS_IMMUTABLE( 1196 if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 1207 return -EPERM; 1197 return -EPERM; 1208 1198 1209 err = ufs_alloc_lastblock(inode, size 1199 err = ufs_alloc_lastblock(inode, size); 1210 1200 1211 if (err) 1201 if (err) 1212 goto out; 1202 goto out; 1213 1203 1214 block_truncate_page(inode->i_mapping, 1204 block_truncate_page(inode->i_mapping, size, ufs_getfrag_block); 1215 1205 1216 truncate_setsize(inode, size); 1206 truncate_setsize(inode, size); 1217 1207 1218 ufs_truncate_blocks(inode); 1208 ufs_truncate_blocks(inode); 1219 inode_set_mtime_to_ts(inode, inode_se !! 1209 inode->i_mtime = inode->i_ctime = current_time(inode); 1220 mark_inode_dirty(inode); 1210 mark_inode_dirty(inode); 1221 out: 1211 out: 1222 UFSD("EXIT: err %d\n", err); 1212 UFSD("EXIT: err %d\n", err); 1223 return err; 1213 return err; 1224 } 1214 } 1225 1215 1226 int ufs_setattr(struct mnt_idmap *idmap, stru !! 1216 int ufs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry, 1227 struct iattr *attr) 1217 struct iattr *attr) 1228 { 1218 { 1229 struct inode *inode = d_inode(dentry) 1219 struct inode *inode = d_inode(dentry); 1230 unsigned int ia_valid = attr->ia_vali 1220 unsigned int ia_valid = attr->ia_valid; 1231 int error; 1221 int error; 1232 1222 1233 error = setattr_prepare(&nop_mnt_idma !! 1223 error = setattr_prepare(&init_user_ns, dentry, attr); 1234 if (error) 1224 if (error) 1235 return error; 1225 return error; 1236 1226 1237 if (ia_valid & ATTR_SIZE && attr->ia_ 1227 if (ia_valid & ATTR_SIZE && attr->ia_size != inode->i_size) { 1238 error = ufs_truncate(inode, a 1228 error = ufs_truncate(inode, attr->ia_size); 1239 if (error) 1229 if (error) 1240 return error; 1230 return error; 1241 } 1231 } 1242 1232 1243 setattr_copy(&nop_mnt_idmap, inode, a !! 1233 setattr_copy(&init_user_ns, inode, attr); 1244 mark_inode_dirty(inode); 1234 mark_inode_dirty(inode); 1245 return 0; 1235 return 0; 1246 } 1236 } 1247 1237 1248 const struct inode_operations ufs_file_inode_ 1238 const struct inode_operations ufs_file_inode_operations = { 1249 .setattr = ufs_setattr, 1239 .setattr = ufs_setattr, 1250 }; 1240 }; 1251 1241
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.