1 /* 2 * linux/fs/ufs/inode.c 3 * 4 * Copyright (C) 1998 5 * Daniel Pirkl <daniel.pirkl@email.cz> 6 * Charles University, Faculty of Mathematics and Physics 7 * 8 * from 9 * 10 * linux/fs/ext2/inode.c 11 * 12 * Copyright (C) 1992, 1993, 1994, 1995 13 * Remy Card (card@masi.ibp.fr) 14 * Laboratoire MASI - Institut Blaise Pascal 15 * Universite Pierre et Marie Curie (Paris VI) 16 * 17 * from 18 * 19 * linux/fs/minix/inode.c 20 * 21 * Copyright (C) 1991, 1992 Linus Torvalds 22 * 23 * Goal-directed block allocation by Stephen Tweedie (sct@dcs.ed.ac.uk), 1993 24 * Big-endian to little-endian byte-swapping/bitmaps by 25 * David S. Miller (davem@caip.rutgers.edu), 1995 26 */ 27 28 #include <asm/uaccess.h> 29 30 #include <linux/errno.h> 31 #include <linux/fs.h> 32 #include <linux/time.h> 33 #include <linux/stat.h> 34 #include <linux/string.h> 35 #include <linux/mm.h> 36 #include <linux/buffer_head.h> 37 #include <linux/writeback.h> 38 39 #include "ufs_fs.h" 40 #include "ufs.h" 41 #include "swab.h" 42 #include "util.h" 43 44 static int ufs_block_to_path(struct inode *inode, sector_t i_block, sector_t offsets[4]) 45 { 46 struct ufs_sb_private_info *uspi = UFS_SB(inode->i_sb)->s_uspi; 47 int ptrs = uspi->s_apb; 48 int ptrs_bits = uspi->s_apbshift; 49 const long direct_blocks = UFS_NDADDR, 50 indirect_blocks = ptrs, 51 double_blocks = (1 << (ptrs_bits * 2)); 52 int n = 0; 53 54 55 UFSD("ptrs=uspi->s_apb = %d,double_blocks=%ld \n",ptrs,double_blocks); 56 if (i_block < direct_blocks) { 57 offsets[n++] = i_block; 58 } else if ((i_block -= direct_blocks) < indirect_blocks) { 59 offsets[n++] = UFS_IND_BLOCK; 60 offsets[n++] = i_block; 61 } else if ((i_block -= indirect_blocks) < double_blocks) { 62 offsets[n++] = UFS_DIND_BLOCK; 63 offsets[n++] = i_block >> ptrs_bits; 64 offsets[n++] = i_block & (ptrs - 1); 65 } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) { 66 offsets[n++] = UFS_TIND_BLOCK; 67 offsets[n++] = i_block >> (ptrs_bits * 2); 68 offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1); 69 offsets[n++] = i_block & (ptrs - 1); 70 } else { 71 ufs_warning(inode->i_sb, "ufs_block_to_path", "block > big"); 72 } 73 return n; 74 } 75 76 typedef struct { 77 void *p; 78 union { 79 __fs32 key32; 80 __fs64 key64; 81 }; 82 struct buffer_head *bh; 83 } Indirect; 84 85 static inline int grow_chain32(struct ufs_inode_info *ufsi, 86 struct buffer_head *bh, __fs32 *v, 87 Indirect *from, Indirect *to) 88 { 89 Indirect *p; 90 unsigned seq; 91 to->bh = bh; 92 do { 93 seq = read_seqbegin(&ufsi->meta_lock); 94 to->key32 = *(__fs32 *)(to->p = v); 95 for (p = from; p <= to && p->key32 == *(__fs32 *)p->p; p++) 96 ; 97 } while (read_seqretry(&ufsi->meta_lock, seq)); 98 return (p > to); 99 } 100 101 static inline int grow_chain64(struct ufs_inode_info *ufsi, 102 struct buffer_head *bh, __fs64 *v, 103 Indirect *from, Indirect *to) 104 { 105 Indirect *p; 106 unsigned seq; 107 to->bh = bh; 108 do { 109 seq = read_seqbegin(&ufsi->meta_lock); 110 to->key64 = *(__fs64 *)(to->p = v); 111 for (p = from; p <= to && p->key64 == *(__fs64 *)p->p; p++) 112 ; 113 } while (read_seqretry(&ufsi->meta_lock, seq)); 114 return (p > to); 115 } 116 117 /* 118 * Returns the location of the fragment from 119 * the beginning of the filesystem. 120 */ 121 122 static u64 ufs_frag_map(struct inode *inode, sector_t frag) 123 { 124 struct ufs_inode_info *ufsi = UFS_I(inode); 125 struct super_block *sb = inode->i_sb; 126 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; 127 u64 mask = (u64) uspi->s_apbmask>>uspi->s_fpbshift; 128 int shift = uspi->s_apbshift-uspi->s_fpbshift; 129 sector_t offsets[4], *p; 130 Indirect chain[4], *q = chain; 131 int depth = ufs_block_to_path(inode, frag >> uspi->s_fpbshift, offsets); 132 unsigned flags = UFS_SB(sb)->s_flags; 133 u64 res = 0; 134 135 UFSD(": frag = %llu depth = %d\n", (unsigned long long)frag, depth); 136 UFSD(": uspi->s_fpbshift = %d ,uspi->s_apbmask = %x, mask=%llx\n", 137 uspi->s_fpbshift, uspi->s_apbmask, 138 (unsigned long long)mask); 139 140 if (depth == 0) 141 goto no_block; 142 143 again: 144 p = offsets; 145 146 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) 147 goto ufs2; 148 149 if (!grow_chain32(ufsi, NULL, &ufsi->i_u1.i_data[*p++], chain, q)) 150 goto changed; 151 if (!q->key32) 152 goto no_block; 153 while (--depth) { 154 __fs32 *ptr; 155 struct buffer_head *bh; 156 sector_t n = *p++; 157 158 bh = sb_bread(sb, uspi->s_sbbase + 159 fs32_to_cpu(sb, q->key32) + (n>>shift)); 160 if (!bh) 161 goto no_block; 162 ptr = (__fs32 *)bh->b_data + (n & mask); 163 if (!grow_chain32(ufsi, bh, ptr, chain, ++q)) 164 goto changed; 165 if (!q->key32) 166 goto no_block; 167 } 168 res = fs32_to_cpu(sb, q->key32); 169 goto found; 170 171 ufs2: 172 if (!grow_chain64(ufsi, NULL, &ufsi->i_u1.u2_i_data[*p++], chain, q)) 173 goto changed; 174 if (!q->key64) 175 goto no_block; 176 177 while (--depth) { 178 __fs64 *ptr; 179 struct buffer_head *bh; 180 sector_t n = *p++; 181 182 bh = sb_bread(sb, uspi->s_sbbase + 183 fs64_to_cpu(sb, q->key64) + (n>>shift)); 184 if (!bh) 185 goto no_block; 186 ptr = (__fs64 *)bh->b_data + (n & mask); 187 if (!grow_chain64(ufsi, bh, ptr, chain, ++q)) 188 goto changed; 189 if (!q->key64) 190 goto no_block; 191 } 192 res = fs64_to_cpu(sb, q->key64); 193 found: 194 res += uspi->s_sbbase + (frag & uspi->s_fpbmask); 195 no_block: 196 while (q > chain) { 197 brelse(q->bh); 198 q--; 199 } 200 return res; 201 202 changed: 203 while (q > chain) { 204 brelse(q->bh); 205 q--; 206 } 207 goto again; 208 } 209 210 /** 211 * ufs_inode_getfrag() - allocate new fragment(s) 212 * @inode: pointer to inode 213 * @fragment: number of `fragment' which hold pointer 214 * to new allocated fragment(s) 215 * @new_fragment: number of new allocated fragment(s) 216 * @required: how many fragment(s) we require 217 * @err: we set it if something wrong 218 * @phys: pointer to where we save physical number of new allocated fragments, 219 * NULL if we allocate not data(indirect blocks for example). 220 * @new: we set it if we allocate new block 221 * @locked_page: for ufs_new_fragments() 222 */ 223 static struct buffer_head * 224 ufs_inode_getfrag(struct inode *inode, u64 fragment, 225 sector_t new_fragment, unsigned int required, int *err, 226 long *phys, int *new, struct page *locked_page) 227 { 228 struct ufs_inode_info *ufsi = UFS_I(inode); 229 struct super_block *sb = inode->i_sb; 230 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; 231 struct buffer_head * result; 232 unsigned blockoff, lastblockoff; 233 u64 tmp, goal, lastfrag, block, lastblock; 234 void *p, *p2; 235 236 UFSD("ENTER, ino %lu, fragment %llu, new_fragment %llu, required %u, " 237 "metadata %d\n", inode->i_ino, (unsigned long long)fragment, 238 (unsigned long long)new_fragment, required, !phys); 239 240 /* TODO : to be done for write support 241 if ( (flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) 242 goto ufs2; 243 */ 244 245 block = ufs_fragstoblks (fragment); 246 blockoff = ufs_fragnum (fragment); 247 p = ufs_get_direct_data_ptr(uspi, ufsi, block); 248 249 goal = 0; 250 251 repeat: 252 tmp = ufs_data_ptr_to_cpu(sb, p); 253 254 lastfrag = ufsi->i_lastfrag; 255 if (tmp && fragment < lastfrag) { 256 if (!phys) { 257 result = sb_getblk(sb, uspi->s_sbbase + tmp + blockoff); 258 if (tmp == ufs_data_ptr_to_cpu(sb, p)) { 259 UFSD("EXIT, result %llu\n", 260 (unsigned long long)tmp + blockoff); 261 return result; 262 } 263 brelse (result); 264 goto repeat; 265 } else { 266 *phys = uspi->s_sbbase + tmp + blockoff; 267 return NULL; 268 } 269 } 270 271 lastblock = ufs_fragstoblks (lastfrag); 272 lastblockoff = ufs_fragnum (lastfrag); 273 /* 274 * We will extend file into new block beyond last allocated block 275 */ 276 if (lastblock < block) { 277 /* 278 * We must reallocate last allocated block 279 */ 280 if (lastblockoff) { 281 p2 = ufs_get_direct_data_ptr(uspi, ufsi, lastblock); 282 tmp = ufs_new_fragments(inode, p2, lastfrag, 283 ufs_data_ptr_to_cpu(sb, p2), 284 uspi->s_fpb - lastblockoff, 285 err, locked_page); 286 if (!tmp) { 287 if (lastfrag != ufsi->i_lastfrag) 288 goto repeat; 289 else 290 return NULL; 291 } 292 lastfrag = ufsi->i_lastfrag; 293 294 } 295 tmp = ufs_data_ptr_to_cpu(sb, 296 ufs_get_direct_data_ptr(uspi, ufsi, 297 lastblock)); 298 if (tmp) 299 goal = tmp + uspi->s_fpb; 300 tmp = ufs_new_fragments (inode, p, fragment - blockoff, 301 goal, required + blockoff, 302 err, 303 phys != NULL ? locked_page : NULL); 304 } else if (lastblock == block) { 305 /* 306 * We will extend last allocated block 307 */ 308 tmp = ufs_new_fragments(inode, p, fragment - 309 (blockoff - lastblockoff), 310 ufs_data_ptr_to_cpu(sb, p), 311 required + (blockoff - lastblockoff), 312 err, phys != NULL ? locked_page : NULL); 313 } else /* (lastblock > block) */ { 314 /* 315 * We will allocate new block before last allocated block 316 */ 317 if (block) { 318 tmp = ufs_data_ptr_to_cpu(sb, 319 ufs_get_direct_data_ptr(uspi, ufsi, block - 1)); 320 if (tmp) 321 goal = tmp + uspi->s_fpb; 322 } 323 tmp = ufs_new_fragments(inode, p, fragment - blockoff, 324 goal, uspi->s_fpb, err, 325 phys != NULL ? locked_page : NULL); 326 } 327 if (!tmp) { 328 if ((!blockoff && ufs_data_ptr_to_cpu(sb, p)) || 329 (blockoff && lastfrag != ufsi->i_lastfrag)) 330 goto repeat; 331 *err = -ENOSPC; 332 return NULL; 333 } 334 335 if (!phys) { 336 result = sb_getblk(sb, uspi->s_sbbase + tmp + blockoff); 337 } else { 338 *phys = uspi->s_sbbase + tmp + blockoff; 339 result = NULL; 340 *err = 0; 341 *new = 1; 342 } 343 344 inode->i_ctime = CURRENT_TIME_SEC; 345 if (IS_SYNC(inode)) 346 ufs_sync_inode (inode); 347 mark_inode_dirty(inode); 348 UFSD("EXIT, result %llu\n", (unsigned long long)tmp + blockoff); 349 return result; 350 351 /* This part : To be implemented .... 352 Required only for writing, not required for READ-ONLY. 353 ufs2: 354 355 u2_block = ufs_fragstoblks(fragment); 356 u2_blockoff = ufs_fragnum(fragment); 357 p = ufsi->i_u1.u2_i_data + block; 358 goal = 0; 359 360 repeat2: 361 tmp = fs32_to_cpu(sb, *p); 362 lastfrag = ufsi->i_lastfrag; 363 364 */ 365 } 366 367 /** 368 * ufs_inode_getblock() - allocate new block 369 * @inode: pointer to inode 370 * @bh: pointer to block which hold "pointer" to new allocated block 371 * @fragment: number of `fragment' which hold pointer 372 * to new allocated block 373 * @new_fragment: number of new allocated fragment 374 * (block will hold this fragment and also uspi->s_fpb-1) 375 * @err: see ufs_inode_getfrag() 376 * @phys: see ufs_inode_getfrag() 377 * @new: see ufs_inode_getfrag() 378 * @locked_page: see ufs_inode_getfrag() 379 */ 380 static struct buffer_head * 381 ufs_inode_getblock(struct inode *inode, struct buffer_head *bh, 382 u64 fragment, sector_t new_fragment, int *err, 383 long *phys, int *new, struct page *locked_page) 384 { 385 struct super_block *sb = inode->i_sb; 386 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; 387 struct buffer_head * result; 388 unsigned blockoff; 389 u64 tmp, goal, block; 390 void *p; 391 392 block = ufs_fragstoblks (fragment); 393 blockoff = ufs_fragnum (fragment); 394 395 UFSD("ENTER, ino %lu, fragment %llu, new_fragment %llu, metadata %d\n", 396 inode->i_ino, (unsigned long long)fragment, 397 (unsigned long long)new_fragment, !phys); 398 399 result = NULL; 400 if (!bh) 401 goto out; 402 if (!buffer_uptodate(bh)) { 403 ll_rw_block (READ, 1, &bh); 404 wait_on_buffer (bh); 405 if (!buffer_uptodate(bh)) 406 goto out; 407 } 408 if (uspi->fs_magic == UFS2_MAGIC) 409 p = (__fs64 *)bh->b_data + block; 410 else 411 p = (__fs32 *)bh->b_data + block; 412 repeat: 413 tmp = ufs_data_ptr_to_cpu(sb, p); 414 if (tmp) { 415 if (!phys) { 416 result = sb_getblk(sb, uspi->s_sbbase + tmp + blockoff); 417 if (tmp == ufs_data_ptr_to_cpu(sb, p)) 418 goto out; 419 brelse (result); 420 goto repeat; 421 } else { 422 *phys = uspi->s_sbbase + tmp + blockoff; 423 goto out; 424 } 425 } 426 427 if (block && (uspi->fs_magic == UFS2_MAGIC ? 428 (tmp = fs64_to_cpu(sb, ((__fs64 *)bh->b_data)[block-1])) : 429 (tmp = fs32_to_cpu(sb, ((__fs32 *)bh->b_data)[block-1])))) 430 goal = tmp + uspi->s_fpb; 431 else 432 goal = bh->b_blocknr + uspi->s_fpb; 433 tmp = ufs_new_fragments(inode, p, ufs_blknum(new_fragment), goal, 434 uspi->s_fpb, err, locked_page); 435 if (!tmp) { 436 if (ufs_data_ptr_to_cpu(sb, p)) 437 goto repeat; 438 goto out; 439 } 440 441 442 if (!phys) { 443 result = sb_getblk(sb, uspi->s_sbbase + tmp + blockoff); 444 } else { 445 *phys = uspi->s_sbbase + tmp + blockoff; 446 *new = 1; 447 } 448 449 mark_buffer_dirty(bh); 450 if (IS_SYNC(inode)) 451 sync_dirty_buffer(bh); 452 inode->i_ctime = CURRENT_TIME_SEC; 453 mark_inode_dirty(inode); 454 UFSD("result %llu\n", (unsigned long long)tmp + blockoff); 455 out: 456 brelse (bh); 457 UFSD("EXIT\n"); 458 return result; 459 } 460 461 /** 462 * ufs_getfrag_block() - `get_block_t' function, interface between UFS and 463 * readpage, writepage and so on 464 */ 465 466 int ufs_getfrag_block(struct inode *inode, sector_t fragment, struct buffer_head *bh_result, int create) 467 { 468 struct super_block * sb = inode->i_sb; 469 struct ufs_sb_info * sbi = UFS_SB(sb); 470 struct ufs_sb_private_info * uspi = sbi->s_uspi; 471 struct buffer_head * bh; 472 int ret, err, new; 473 unsigned long ptr,phys; 474 u64 phys64 = 0; 475 476 if (!create) { 477 phys64 = ufs_frag_map(inode, fragment); 478 UFSD("phys64 = %llu\n", (unsigned long long)phys64); 479 if (phys64) 480 map_bh(bh_result, sb, phys64); 481 return 0; 482 } 483 484 /* This code entered only while writing ....? */ 485 486 err = -EIO; 487 new = 0; 488 ret = 0; 489 bh = NULL; 490 491 mutex_lock(&UFS_I(inode)->truncate_mutex); 492 493 UFSD("ENTER, ino %lu, fragment %llu\n", inode->i_ino, (unsigned long long)fragment); 494 if (fragment > 495 ((UFS_NDADDR + uspi->s_apb + uspi->s_2apb + uspi->s_3apb) 496 << uspi->s_fpbshift)) 497 goto abort_too_big; 498 499 err = 0; 500 ptr = fragment; 501 502 /* 503 * ok, these macros clean the logic up a bit and make 504 * it much more readable: 505 */ 506 #define GET_INODE_DATABLOCK(x) \ 507 ufs_inode_getfrag(inode, x, fragment, 1, &err, &phys, &new,\ 508 bh_result->b_page) 509 #define GET_INODE_PTR(x) \ 510 ufs_inode_getfrag(inode, x, fragment, uspi->s_fpb, &err, NULL, NULL,\ 511 bh_result->b_page) 512 #define GET_INDIRECT_DATABLOCK(x) \ 513 ufs_inode_getblock(inode, bh, x, fragment, \ 514 &err, &phys, &new, bh_result->b_page) 515 #define GET_INDIRECT_PTR(x) \ 516 ufs_inode_getblock(inode, bh, x, fragment, \ 517 &err, NULL, NULL, NULL) 518 519 if (ptr < UFS_NDIR_FRAGMENT) { 520 bh = GET_INODE_DATABLOCK(ptr); 521 goto out; 522 } 523 ptr -= UFS_NDIR_FRAGMENT; 524 if (ptr < (1 << (uspi->s_apbshift + uspi->s_fpbshift))) { 525 bh = GET_INODE_PTR(UFS_IND_FRAGMENT + (ptr >> uspi->s_apbshift)); 526 goto get_indirect; 527 } 528 ptr -= 1 << (uspi->s_apbshift + uspi->s_fpbshift); 529 if (ptr < (1 << (uspi->s_2apbshift + uspi->s_fpbshift))) { 530 bh = GET_INODE_PTR(UFS_DIND_FRAGMENT + (ptr >> uspi->s_2apbshift)); 531 goto get_double; 532 } 533 ptr -= 1 << (uspi->s_2apbshift + uspi->s_fpbshift); 534 bh = GET_INODE_PTR(UFS_TIND_FRAGMENT + (ptr >> uspi->s_3apbshift)); 535 bh = GET_INDIRECT_PTR((ptr >> uspi->s_2apbshift) & uspi->s_apbmask); 536 get_double: 537 bh = GET_INDIRECT_PTR((ptr >> uspi->s_apbshift) & uspi->s_apbmask); 538 get_indirect: 539 bh = GET_INDIRECT_DATABLOCK(ptr & uspi->s_apbmask); 540 541 #undef GET_INODE_DATABLOCK 542 #undef GET_INODE_PTR 543 #undef GET_INDIRECT_DATABLOCK 544 #undef GET_INDIRECT_PTR 545 546 out: 547 if (err) 548 goto abort; 549 if (new) 550 set_buffer_new(bh_result); 551 map_bh(bh_result, sb, phys); 552 abort: 553 mutex_unlock(&UFS_I(inode)->truncate_mutex); 554 555 return err; 556 557 abort_too_big: 558 ufs_warning(sb, "ufs_get_block", "block > big"); 559 goto abort; 560 } 561 562 static int ufs_writepage(struct page *page, struct writeback_control *wbc) 563 { 564 return block_write_full_page(page,ufs_getfrag_block,wbc); 565 } 566 567 static int ufs_readpage(struct file *file, struct page *page) 568 { 569 return block_read_full_page(page,ufs_getfrag_block); 570 } 571 572 int ufs_prepare_chunk(struct page *page, loff_t pos, unsigned len) 573 { 574 return __block_write_begin(page, pos, len, ufs_getfrag_block); 575 } 576 577 static void ufs_write_failed(struct address_space *mapping, loff_t to) 578 { 579 struct inode *inode = mapping->host; 580 581 if (to > inode->i_size) { 582 truncate_pagecache(inode, inode->i_size); 583 ufs_truncate_blocks(inode); 584 } 585 } 586 587 static int ufs_write_begin(struct file *file, struct address_space *mapping, 588 loff_t pos, unsigned len, unsigned flags, 589 struct page **pagep, void **fsdata) 590 { 591 int ret; 592 593 ret = block_write_begin(mapping, pos, len, flags, pagep, 594 ufs_getfrag_block); 595 if (unlikely(ret)) 596 ufs_write_failed(mapping, pos + len); 597 598 return ret; 599 } 600 601 static int ufs_write_end(struct file *file, struct address_space *mapping, 602 loff_t pos, unsigned len, unsigned copied, 603 struct page *page, void *fsdata) 604 { 605 int ret; 606 607 ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata); 608 if (ret < len) 609 ufs_write_failed(mapping, pos + len); 610 return ret; 611 } 612 613 static sector_t ufs_bmap(struct address_space *mapping, sector_t block) 614 { 615 return generic_block_bmap(mapping,block,ufs_getfrag_block); 616 } 617 618 const struct address_space_operations ufs_aops = { 619 .readpage = ufs_readpage, 620 .writepage = ufs_writepage, 621 .write_begin = ufs_write_begin, 622 .write_end = ufs_write_end, 623 .bmap = ufs_bmap 624 }; 625 626 static void ufs_set_inode_ops(struct inode *inode) 627 { 628 if (S_ISREG(inode->i_mode)) { 629 inode->i_op = &ufs_file_inode_operations; 630 inode->i_fop = &ufs_file_operations; 631 inode->i_mapping->a_ops = &ufs_aops; 632 } else if (S_ISDIR(inode->i_mode)) { 633 inode->i_op = &ufs_dir_inode_operations; 634 inode->i_fop = &ufs_dir_operations; 635 inode->i_mapping->a_ops = &ufs_aops; 636 } else if (S_ISLNK(inode->i_mode)) { 637 if (!inode->i_blocks) { 638 inode->i_op = &ufs_fast_symlink_inode_operations; 639 inode->i_link = (char *)UFS_I(inode)->i_u1.i_symlink; 640 } else { 641 inode->i_op = &ufs_symlink_inode_operations; 642 inode->i_mapping->a_ops = &ufs_aops; 643 } 644 } else 645 init_special_inode(inode, inode->i_mode, 646 ufs_get_inode_dev(inode->i_sb, UFS_I(inode))); 647 } 648 649 static int ufs1_read_inode(struct inode *inode, struct ufs_inode *ufs_inode) 650 { 651 struct ufs_inode_info *ufsi = UFS_I(inode); 652 struct super_block *sb = inode->i_sb; 653 umode_t mode; 654 655 /* 656 * Copy data to the in-core inode. 657 */ 658 inode->i_mode = mode = fs16_to_cpu(sb, ufs_inode->ui_mode); 659 set_nlink(inode, fs16_to_cpu(sb, ufs_inode->ui_nlink)); 660 if (inode->i_nlink == 0) { 661 ufs_error (sb, "ufs_read_inode", "inode %lu has zero nlink\n", inode->i_ino); 662 return -1; 663 } 664 665 /* 666 * Linux now has 32-bit uid and gid, so we can support EFT. 667 */ 668 i_uid_write(inode, ufs_get_inode_uid(sb, ufs_inode)); 669 i_gid_write(inode, ufs_get_inode_gid(sb, ufs_inode)); 670 671 inode->i_size = fs64_to_cpu(sb, ufs_inode->ui_size); 672 inode->i_atime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_atime.tv_sec); 673 inode->i_ctime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_ctime.tv_sec); 674 inode->i_mtime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_mtime.tv_sec); 675 inode->i_mtime.tv_nsec = 0; 676 inode->i_atime.tv_nsec = 0; 677 inode->i_ctime.tv_nsec = 0; 678 inode->i_blocks = fs32_to_cpu(sb, ufs_inode->ui_blocks); 679 inode->i_generation = fs32_to_cpu(sb, ufs_inode->ui_gen); 680 ufsi->i_flags = fs32_to_cpu(sb, ufs_inode->ui_flags); 681 ufsi->i_shadow = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_shadow); 682 ufsi->i_oeftflag = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_oeftflag); 683 684 685 if (S_ISCHR(mode) || S_ISBLK(mode) || inode->i_blocks) { 686 memcpy(ufsi->i_u1.i_data, &ufs_inode->ui_u2.ui_addr, 687 sizeof(ufs_inode->ui_u2.ui_addr)); 688 } else { 689 memcpy(ufsi->i_u1.i_symlink, ufs_inode->ui_u2.ui_symlink, 690 sizeof(ufs_inode->ui_u2.ui_symlink) - 1); 691 ufsi->i_u1.i_symlink[sizeof(ufs_inode->ui_u2.ui_symlink) - 1] = 0; 692 } 693 return 0; 694 } 695 696 static int ufs2_read_inode(struct inode *inode, struct ufs2_inode *ufs2_inode) 697 { 698 struct ufs_inode_info *ufsi = UFS_I(inode); 699 struct super_block *sb = inode->i_sb; 700 umode_t mode; 701 702 UFSD("Reading ufs2 inode, ino %lu\n", inode->i_ino); 703 /* 704 * Copy data to the in-core inode. 705 */ 706 inode->i_mode = mode = fs16_to_cpu(sb, ufs2_inode->ui_mode); 707 set_nlink(inode, fs16_to_cpu(sb, ufs2_inode->ui_nlink)); 708 if (inode->i_nlink == 0) { 709 ufs_error (sb, "ufs_read_inode", "inode %lu has zero nlink\n", inode->i_ino); 710 return -1; 711 } 712 713 /* 714 * Linux now has 32-bit uid and gid, so we can support EFT. 715 */ 716 i_uid_write(inode, fs32_to_cpu(sb, ufs2_inode->ui_uid)); 717 i_gid_write(inode, fs32_to_cpu(sb, ufs2_inode->ui_gid)); 718 719 inode->i_size = fs64_to_cpu(sb, ufs2_inode->ui_size); 720 inode->i_atime.tv_sec = fs64_to_cpu(sb, ufs2_inode->ui_atime); 721 inode->i_ctime.tv_sec = fs64_to_cpu(sb, ufs2_inode->ui_ctime); 722 inode->i_mtime.tv_sec = fs64_to_cpu(sb, ufs2_inode->ui_mtime); 723 inode->i_atime.tv_nsec = fs32_to_cpu(sb, ufs2_inode->ui_atimensec); 724 inode->i_ctime.tv_nsec = fs32_to_cpu(sb, ufs2_inode->ui_ctimensec); 725 inode->i_mtime.tv_nsec = fs32_to_cpu(sb, ufs2_inode->ui_mtimensec); 726 inode->i_blocks = fs64_to_cpu(sb, ufs2_inode->ui_blocks); 727 inode->i_generation = fs32_to_cpu(sb, ufs2_inode->ui_gen); 728 ufsi->i_flags = fs32_to_cpu(sb, ufs2_inode->ui_flags); 729 /* 730 ufsi->i_shadow = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_shadow); 731 ufsi->i_oeftflag = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_oeftflag); 732 */ 733 734 if (S_ISCHR(mode) || S_ISBLK(mode) || inode->i_blocks) { 735 memcpy(ufsi->i_u1.u2_i_data, &ufs2_inode->ui_u2.ui_addr, 736 sizeof(ufs2_inode->ui_u2.ui_addr)); 737 } else { 738 memcpy(ufsi->i_u1.i_symlink, ufs2_inode->ui_u2.ui_symlink, 739 sizeof(ufs2_inode->ui_u2.ui_symlink) - 1); 740 ufsi->i_u1.i_symlink[sizeof(ufs2_inode->ui_u2.ui_symlink) - 1] = 0; 741 } 742 return 0; 743 } 744 745 struct inode *ufs_iget(struct super_block *sb, unsigned long ino) 746 { 747 struct ufs_inode_info *ufsi; 748 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; 749 struct buffer_head * bh; 750 struct inode *inode; 751 int err; 752 753 UFSD("ENTER, ino %lu\n", ino); 754 755 if (ino < UFS_ROOTINO || ino > (uspi->s_ncg * uspi->s_ipg)) { 756 ufs_warning(sb, "ufs_read_inode", "bad inode number (%lu)\n", 757 ino); 758 return ERR_PTR(-EIO); 759 } 760 761 inode = iget_locked(sb, ino); 762 if (!inode) 763 return ERR_PTR(-ENOMEM); 764 if (!(inode->i_state & I_NEW)) 765 return inode; 766 767 ufsi = UFS_I(inode); 768 769 bh = sb_bread(sb, uspi->s_sbbase + ufs_inotofsba(inode->i_ino)); 770 if (!bh) { 771 ufs_warning(sb, "ufs_read_inode", "unable to read inode %lu\n", 772 inode->i_ino); 773 goto bad_inode; 774 } 775 if ((UFS_SB(sb)->s_flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) { 776 struct ufs2_inode *ufs2_inode = (struct ufs2_inode *)bh->b_data; 777 778 err = ufs2_read_inode(inode, 779 ufs2_inode + ufs_inotofsbo(inode->i_ino)); 780 } else { 781 struct ufs_inode *ufs_inode = (struct ufs_inode *)bh->b_data; 782 783 err = ufs1_read_inode(inode, 784 ufs_inode + ufs_inotofsbo(inode->i_ino)); 785 } 786 787 if (err) 788 goto bad_inode; 789 inode->i_version++; 790 ufsi->i_lastfrag = 791 (inode->i_size + uspi->s_fsize - 1) >> uspi->s_fshift; 792 ufsi->i_dir_start_lookup = 0; 793 ufsi->i_osync = 0; 794 795 ufs_set_inode_ops(inode); 796 797 brelse(bh); 798 799 UFSD("EXIT\n"); 800 unlock_new_inode(inode); 801 return inode; 802 803 bad_inode: 804 iget_failed(inode); 805 return ERR_PTR(-EIO); 806 } 807 808 static void ufs1_update_inode(struct inode *inode, struct ufs_inode *ufs_inode) 809 { 810 struct super_block *sb = inode->i_sb; 811 struct ufs_inode_info *ufsi = UFS_I(inode); 812 813 ufs_inode->ui_mode = cpu_to_fs16(sb, inode->i_mode); 814 ufs_inode->ui_nlink = cpu_to_fs16(sb, inode->i_nlink); 815 816 ufs_set_inode_uid(sb, ufs_inode, i_uid_read(inode)); 817 ufs_set_inode_gid(sb, ufs_inode, i_gid_read(inode)); 818 819 ufs_inode->ui_size = cpu_to_fs64(sb, inode->i_size); 820 ufs_inode->ui_atime.tv_sec = cpu_to_fs32(sb, inode->i_atime.tv_sec); 821 ufs_inode->ui_atime.tv_usec = 0; 822 ufs_inode->ui_ctime.tv_sec = cpu_to_fs32(sb, inode->i_ctime.tv_sec); 823 ufs_inode->ui_ctime.tv_usec = 0; 824 ufs_inode->ui_mtime.tv_sec = cpu_to_fs32(sb, inode->i_mtime.tv_sec); 825 ufs_inode->ui_mtime.tv_usec = 0; 826 ufs_inode->ui_blocks = cpu_to_fs32(sb, inode->i_blocks); 827 ufs_inode->ui_flags = cpu_to_fs32(sb, ufsi->i_flags); 828 ufs_inode->ui_gen = cpu_to_fs32(sb, inode->i_generation); 829 830 if ((UFS_SB(sb)->s_flags & UFS_UID_MASK) == UFS_UID_EFT) { 831 ufs_inode->ui_u3.ui_sun.ui_shadow = cpu_to_fs32(sb, ufsi->i_shadow); 832 ufs_inode->ui_u3.ui_sun.ui_oeftflag = cpu_to_fs32(sb, ufsi->i_oeftflag); 833 } 834 835 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { 836 /* ufs_inode->ui_u2.ui_addr.ui_db[0] = cpu_to_fs32(sb, inode->i_rdev); */ 837 ufs_inode->ui_u2.ui_addr.ui_db[0] = ufsi->i_u1.i_data[0]; 838 } else if (inode->i_blocks) { 839 memcpy(&ufs_inode->ui_u2.ui_addr, ufsi->i_u1.i_data, 840 sizeof(ufs_inode->ui_u2.ui_addr)); 841 } 842 else { 843 memcpy(&ufs_inode->ui_u2.ui_symlink, ufsi->i_u1.i_symlink, 844 sizeof(ufs_inode->ui_u2.ui_symlink)); 845 } 846 847 if (!inode->i_nlink) 848 memset (ufs_inode, 0, sizeof(struct ufs_inode)); 849 } 850 851 static void ufs2_update_inode(struct inode *inode, struct ufs2_inode *ufs_inode) 852 { 853 struct super_block *sb = inode->i_sb; 854 struct ufs_inode_info *ufsi = UFS_I(inode); 855 856 UFSD("ENTER\n"); 857 ufs_inode->ui_mode = cpu_to_fs16(sb, inode->i_mode); 858 ufs_inode->ui_nlink = cpu_to_fs16(sb, inode->i_nlink); 859 860 ufs_inode->ui_uid = cpu_to_fs32(sb, i_uid_read(inode)); 861 ufs_inode->ui_gid = cpu_to_fs32(sb, i_gid_read(inode)); 862 863 ufs_inode->ui_size = cpu_to_fs64(sb, inode->i_size); 864 ufs_inode->ui_atime = cpu_to_fs64(sb, inode->i_atime.tv_sec); 865 ufs_inode->ui_atimensec = cpu_to_fs32(sb, inode->i_atime.tv_nsec); 866 ufs_inode->ui_ctime = cpu_to_fs64(sb, inode->i_ctime.tv_sec); 867 ufs_inode->ui_ctimensec = cpu_to_fs32(sb, inode->i_ctime.tv_nsec); 868 ufs_inode->ui_mtime = cpu_to_fs64(sb, inode->i_mtime.tv_sec); 869 ufs_inode->ui_mtimensec = cpu_to_fs32(sb, inode->i_mtime.tv_nsec); 870 871 ufs_inode->ui_blocks = cpu_to_fs64(sb, inode->i_blocks); 872 ufs_inode->ui_flags = cpu_to_fs32(sb, ufsi->i_flags); 873 ufs_inode->ui_gen = cpu_to_fs32(sb, inode->i_generation); 874 875 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { 876 /* ufs_inode->ui_u2.ui_addr.ui_db[0] = cpu_to_fs32(sb, inode->i_rdev); */ 877 ufs_inode->ui_u2.ui_addr.ui_db[0] = ufsi->i_u1.u2_i_data[0]; 878 } else if (inode->i_blocks) { 879 memcpy(&ufs_inode->ui_u2.ui_addr, ufsi->i_u1.u2_i_data, 880 sizeof(ufs_inode->ui_u2.ui_addr)); 881 } else { 882 memcpy(&ufs_inode->ui_u2.ui_symlink, ufsi->i_u1.i_symlink, 883 sizeof(ufs_inode->ui_u2.ui_symlink)); 884 } 885 886 if (!inode->i_nlink) 887 memset (ufs_inode, 0, sizeof(struct ufs2_inode)); 888 UFSD("EXIT\n"); 889 } 890 891 static int ufs_update_inode(struct inode * inode, int do_sync) 892 { 893 struct super_block *sb = inode->i_sb; 894 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; 895 struct buffer_head * bh; 896 897 UFSD("ENTER, ino %lu\n", inode->i_ino); 898 899 if (inode->i_ino < UFS_ROOTINO || 900 inode->i_ino > (uspi->s_ncg * uspi->s_ipg)) { 901 ufs_warning (sb, "ufs_read_inode", "bad inode number (%lu)\n", inode->i_ino); 902 return -1; 903 } 904 905 bh = sb_bread(sb, ufs_inotofsba(inode->i_ino)); 906 if (!bh) { 907 ufs_warning (sb, "ufs_read_inode", "unable to read inode %lu\n", inode->i_ino); 908 return -1; 909 } 910 if (uspi->fs_magic == UFS2_MAGIC) { 911 struct ufs2_inode *ufs2_inode = (struct ufs2_inode *)bh->b_data; 912 913 ufs2_update_inode(inode, 914 ufs2_inode + ufs_inotofsbo(inode->i_ino)); 915 } else { 916 struct ufs_inode *ufs_inode = (struct ufs_inode *) bh->b_data; 917 918 ufs1_update_inode(inode, ufs_inode + ufs_inotofsbo(inode->i_ino)); 919 } 920 921 mark_buffer_dirty(bh); 922 if (do_sync) 923 sync_dirty_buffer(bh); 924 brelse (bh); 925 926 UFSD("EXIT\n"); 927 return 0; 928 } 929 930 int ufs_write_inode(struct inode *inode, struct writeback_control *wbc) 931 { 932 return ufs_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL); 933 } 934 935 int ufs_sync_inode (struct inode *inode) 936 { 937 return ufs_update_inode (inode, 1); 938 } 939 940 void ufs_evict_inode(struct inode * inode) 941 { 942 int want_delete = 0; 943 944 if (!inode->i_nlink && !is_bad_inode(inode)) 945 want_delete = 1; 946 947 truncate_inode_pages_final(&inode->i_data); 948 if (want_delete) { 949 inode->i_size = 0; 950 if (inode->i_blocks) 951 ufs_truncate_blocks(inode); 952 } 953 954 invalidate_inode_buffers(inode); 955 clear_inode(inode); 956 957 if (want_delete) 958 ufs_free_inode(inode); 959 } 960