5 * Daniel Pirkl <daniel.pirkl@email.cz>
6 * Charles University, Faculty of Mathematics and Physics
10 * linux/fs/ext2/inode.c
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)
19 * linux/fs/minix/inode.c
21 * Copyright (C) 1991, 1992 Linus Torvalds
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
28 #include <asm/uaccess.h>
30 #include <linux/errno.h>
32 #include <linux/time.h>
33 #include <linux/stat.h>
34 #include <linux/string.h>
36 #include <linux/buffer_head.h>
37 #include <linux/writeback.h>
44 static int ufs_block_to_path(struct inode *inode, sector_t i_block, unsigned offsets[4])
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));
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);
71 ufs_warning(inode->i_sb, "ufs_block_to_path", "block > big");
82 struct buffer_head *bh;
85 static inline int grow_chain32(struct ufs_inode_info *ufsi,
86 struct buffer_head *bh, __fs32 *v,
87 Indirect *from, Indirect *to)
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++)
97 } while (read_seqretry(&ufsi->meta_lock, seq));
101 static inline int grow_chain64(struct ufs_inode_info *ufsi,
102 struct buffer_head *bh, __fs64 *v,
103 Indirect *from, Indirect *to)
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++)
113 } while (read_seqretry(&ufsi->meta_lock, seq));
118 * Returns the location of the fragment from
119 * the beginning of the filesystem.
122 static u64 ufs_frag_map(struct inode *inode, sector_t frag)
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 unsigned 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;
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);
146 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
149 if (!grow_chain32(ufsi, NULL, &ufsi->i_u1.i_data[*p++], chain, q))
155 struct buffer_head *bh;
158 bh = sb_bread(sb, uspi->s_sbbase +
159 fs32_to_cpu(sb, q->key32) + (n>>shift));
162 ptr = (__fs32 *)bh->b_data + (n & mask);
163 if (!grow_chain32(ufsi, bh, ptr, chain, ++q))
168 res = fs32_to_cpu(sb, q->key32);
172 if (!grow_chain64(ufsi, NULL, &ufsi->i_u1.u2_i_data[*p++], chain, q))
179 struct buffer_head *bh;
182 bh = sb_bread(sb, uspi->s_sbbase +
183 fs64_to_cpu(sb, q->key64) + (n>>shift));
186 ptr = (__fs64 *)bh->b_data + (n & mask);
187 if (!grow_chain64(ufsi, bh, ptr, chain, ++q))
192 res = fs64_to_cpu(sb, q->key64);
194 res += uspi->s_sbbase + (frag & uspi->s_fpbmask);
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()
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)
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;
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);
240 /* TODO : to be done for write support
241 if ( (flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
245 block = ufs_fragstoblks (fragment);
246 blockoff = ufs_fragnum (fragment);
247 p = ufs_get_direct_data_ptr(uspi, ufsi, block);
252 tmp = ufs_data_ptr_to_cpu(sb, p);
254 lastfrag = ufsi->i_lastfrag;
255 if (tmp && fragment < lastfrag) {
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);
266 *phys = uspi->s_sbbase + tmp + blockoff;
271 lastblock = ufs_fragstoblks (lastfrag);
272 lastblockoff = ufs_fragnum (lastfrag);
274 * We will extend file into new block beyond last allocated block
276 if (lastblock < block) {
278 * We must reallocate last allocated block
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,
287 if (lastfrag != ufsi->i_lastfrag)
292 lastfrag = ufsi->i_lastfrag;
295 tmp = ufs_data_ptr_to_cpu(sb,
296 ufs_get_direct_data_ptr(uspi, ufsi,
299 goal = tmp + uspi->s_fpb;
300 tmp = ufs_new_fragments (inode, p, fragment - blockoff,
301 goal, required + blockoff,
303 phys != NULL ? locked_page : NULL);
304 } else if (lastblock == block) {
306 * We will extend last allocated block
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) */ {
315 * We will allocate new block before last allocated block
318 tmp = ufs_data_ptr_to_cpu(sb,
319 ufs_get_direct_data_ptr(uspi, ufsi, block - 1));
321 goal = tmp + uspi->s_fpb;
323 tmp = ufs_new_fragments(inode, p, fragment - blockoff,
324 goal, uspi->s_fpb, err,
325 phys != NULL ? locked_page : NULL);
328 if ((!blockoff && ufs_data_ptr_to_cpu(sb, p)) ||
329 (blockoff && lastfrag != ufsi->i_lastfrag))
336 result = sb_getblk(sb, uspi->s_sbbase + tmp + blockoff);
338 *phys = uspi->s_sbbase + tmp + blockoff;
344 inode->i_ctime = CURRENT_TIME_SEC;
346 ufs_sync_inode (inode);
347 mark_inode_dirty(inode);
348 UFSD("EXIT, result %llu\n", (unsigned long long)tmp + blockoff);
351 /* This part : To be implemented ....
352 Required only for writing, not required for READ-ONLY.
355 u2_block = ufs_fragstoblks(fragment);
356 u2_blockoff = ufs_fragnum(fragment);
357 p = ufsi->i_u1.u2_i_data + block;
361 tmp = fs32_to_cpu(sb, *p);
362 lastfrag = ufsi->i_lastfrag;
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()
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)
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;
389 u64 tmp, goal, block;
392 block = ufs_fragstoblks (fragment);
393 blockoff = ufs_fragnum (fragment);
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);
402 if (!buffer_uptodate(bh)) {
403 ll_rw_block (READ, 1, &bh);
405 if (!buffer_uptodate(bh))
408 if (uspi->fs_magic == UFS2_MAGIC)
409 p = (__fs64 *)bh->b_data + block;
411 p = (__fs32 *)bh->b_data + block;
413 tmp = ufs_data_ptr_to_cpu(sb, p);
416 result = sb_getblk(sb, uspi->s_sbbase + tmp + blockoff);
417 if (tmp == ufs_data_ptr_to_cpu(sb, p))
422 *phys = uspi->s_sbbase + tmp + blockoff;
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;
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);
436 if (ufs_data_ptr_to_cpu(sb, p))
443 result = sb_getblk(sb, uspi->s_sbbase + tmp + blockoff);
445 *phys = uspi->s_sbbase + tmp + blockoff;
449 mark_buffer_dirty(bh);
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);
462 * ufs_getfrag_block() - `get_block_t' function, interface between UFS and
463 * readpage, writepage and so on
466 static int ufs_getfrag_block(struct inode *inode, sector_t fragment, struct buffer_head *bh_result, int create)
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;
473 unsigned long ptr,phys;
477 phys64 = ufs_frag_map(inode, fragment);
478 UFSD("phys64 = %llu\n", (unsigned long long)phys64);
480 map_bh(bh_result, sb, phys64);
484 /* This code entered only while writing ....? */
491 mutex_lock(&UFS_I(inode)->truncate_mutex);
493 UFSD("ENTER, ino %lu, fragment %llu\n", inode->i_ino, (unsigned long long)fragment);
495 ((UFS_NDADDR + uspi->s_apb + uspi->s_2apb + uspi->s_3apb)
496 << uspi->s_fpbshift))
503 * ok, these macros clean the logic up a bit and make
504 * it much more readable:
506 #define GET_INODE_DATABLOCK(x) \
507 ufs_inode_getfrag(inode, x, fragment, 1, &err, &phys, &new,\
509 #define GET_INODE_PTR(x) \
510 ufs_inode_getfrag(inode, x, fragment, uspi->s_fpb, &err, NULL, NULL,\
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)
519 if (ptr < UFS_NDIR_FRAGMENT) {
520 bh = GET_INODE_DATABLOCK(ptr);
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));
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));
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);
537 bh = GET_INDIRECT_PTR((ptr >> uspi->s_apbshift) & uspi->s_apbmask);
539 bh = GET_INDIRECT_DATABLOCK(ptr & uspi->s_apbmask);
541 #undef GET_INODE_DATABLOCK
543 #undef GET_INDIRECT_DATABLOCK
544 #undef GET_INDIRECT_PTR
550 set_buffer_new(bh_result);
551 map_bh(bh_result, sb, phys);
553 mutex_unlock(&UFS_I(inode)->truncate_mutex);
558 ufs_warning(sb, "ufs_get_block", "block > big");
562 static int ufs_writepage(struct page *page, struct writeback_control *wbc)
564 return block_write_full_page(page,ufs_getfrag_block,wbc);
567 static int ufs_readpage(struct file *file, struct page *page)
569 return block_read_full_page(page,ufs_getfrag_block);
572 int ufs_prepare_chunk(struct page *page, loff_t pos, unsigned len)
574 return __block_write_begin(page, pos, len, ufs_getfrag_block);
577 static void ufs_truncate_blocks(struct inode *);
579 static void ufs_write_failed(struct address_space *mapping, loff_t to)
581 struct inode *inode = mapping->host;
583 if (to > inode->i_size) {
584 truncate_pagecache(inode, inode->i_size);
585 ufs_truncate_blocks(inode);
589 static int ufs_write_begin(struct file *file, struct address_space *mapping,
590 loff_t pos, unsigned len, unsigned flags,
591 struct page **pagep, void **fsdata)
595 ret = block_write_begin(mapping, pos, len, flags, pagep,
598 ufs_write_failed(mapping, pos + len);
603 static int ufs_write_end(struct file *file, struct address_space *mapping,
604 loff_t pos, unsigned len, unsigned copied,
605 struct page *page, void *fsdata)
609 ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
611 ufs_write_failed(mapping, pos + len);
615 static sector_t ufs_bmap(struct address_space *mapping, sector_t block)
617 return generic_block_bmap(mapping,block,ufs_getfrag_block);
620 const struct address_space_operations ufs_aops = {
621 .readpage = ufs_readpage,
622 .writepage = ufs_writepage,
623 .write_begin = ufs_write_begin,
624 .write_end = ufs_write_end,
628 static void ufs_set_inode_ops(struct inode *inode)
630 if (S_ISREG(inode->i_mode)) {
631 inode->i_op = &ufs_file_inode_operations;
632 inode->i_fop = &ufs_file_operations;
633 inode->i_mapping->a_ops = &ufs_aops;
634 } else if (S_ISDIR(inode->i_mode)) {
635 inode->i_op = &ufs_dir_inode_operations;
636 inode->i_fop = &ufs_dir_operations;
637 inode->i_mapping->a_ops = &ufs_aops;
638 } else if (S_ISLNK(inode->i_mode)) {
639 if (!inode->i_blocks) {
640 inode->i_op = &ufs_fast_symlink_inode_operations;
641 inode->i_link = (char *)UFS_I(inode)->i_u1.i_symlink;
643 inode->i_op = &ufs_symlink_inode_operations;
644 inode->i_mapping->a_ops = &ufs_aops;
647 init_special_inode(inode, inode->i_mode,
648 ufs_get_inode_dev(inode->i_sb, UFS_I(inode)));
651 static int ufs1_read_inode(struct inode *inode, struct ufs_inode *ufs_inode)
653 struct ufs_inode_info *ufsi = UFS_I(inode);
654 struct super_block *sb = inode->i_sb;
658 * Copy data to the in-core inode.
660 inode->i_mode = mode = fs16_to_cpu(sb, ufs_inode->ui_mode);
661 set_nlink(inode, fs16_to_cpu(sb, ufs_inode->ui_nlink));
662 if (inode->i_nlink == 0) {
663 ufs_error (sb, "ufs_read_inode", "inode %lu has zero nlink\n", inode->i_ino);
668 * Linux now has 32-bit uid and gid, so we can support EFT.
670 i_uid_write(inode, ufs_get_inode_uid(sb, ufs_inode));
671 i_gid_write(inode, ufs_get_inode_gid(sb, ufs_inode));
673 inode->i_size = fs64_to_cpu(sb, ufs_inode->ui_size);
674 inode->i_atime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_atime.tv_sec);
675 inode->i_ctime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_ctime.tv_sec);
676 inode->i_mtime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_mtime.tv_sec);
677 inode->i_mtime.tv_nsec = 0;
678 inode->i_atime.tv_nsec = 0;
679 inode->i_ctime.tv_nsec = 0;
680 inode->i_blocks = fs32_to_cpu(sb, ufs_inode->ui_blocks);
681 inode->i_generation = fs32_to_cpu(sb, ufs_inode->ui_gen);
682 ufsi->i_flags = fs32_to_cpu(sb, ufs_inode->ui_flags);
683 ufsi->i_shadow = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_shadow);
684 ufsi->i_oeftflag = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_oeftflag);
687 if (S_ISCHR(mode) || S_ISBLK(mode) || inode->i_blocks) {
688 memcpy(ufsi->i_u1.i_data, &ufs_inode->ui_u2.ui_addr,
689 sizeof(ufs_inode->ui_u2.ui_addr));
691 memcpy(ufsi->i_u1.i_symlink, ufs_inode->ui_u2.ui_symlink,
692 sizeof(ufs_inode->ui_u2.ui_symlink) - 1);
693 ufsi->i_u1.i_symlink[sizeof(ufs_inode->ui_u2.ui_symlink) - 1] = 0;
698 static int ufs2_read_inode(struct inode *inode, struct ufs2_inode *ufs2_inode)
700 struct ufs_inode_info *ufsi = UFS_I(inode);
701 struct super_block *sb = inode->i_sb;
704 UFSD("Reading ufs2 inode, ino %lu\n", inode->i_ino);
706 * Copy data to the in-core inode.
708 inode->i_mode = mode = fs16_to_cpu(sb, ufs2_inode->ui_mode);
709 set_nlink(inode, fs16_to_cpu(sb, ufs2_inode->ui_nlink));
710 if (inode->i_nlink == 0) {
711 ufs_error (sb, "ufs_read_inode", "inode %lu has zero nlink\n", inode->i_ino);
716 * Linux now has 32-bit uid and gid, so we can support EFT.
718 i_uid_write(inode, fs32_to_cpu(sb, ufs2_inode->ui_uid));
719 i_gid_write(inode, fs32_to_cpu(sb, ufs2_inode->ui_gid));
721 inode->i_size = fs64_to_cpu(sb, ufs2_inode->ui_size);
722 inode->i_atime.tv_sec = fs64_to_cpu(sb, ufs2_inode->ui_atime);
723 inode->i_ctime.tv_sec = fs64_to_cpu(sb, ufs2_inode->ui_ctime);
724 inode->i_mtime.tv_sec = fs64_to_cpu(sb, ufs2_inode->ui_mtime);
725 inode->i_atime.tv_nsec = fs32_to_cpu(sb, ufs2_inode->ui_atimensec);
726 inode->i_ctime.tv_nsec = fs32_to_cpu(sb, ufs2_inode->ui_ctimensec);
727 inode->i_mtime.tv_nsec = fs32_to_cpu(sb, ufs2_inode->ui_mtimensec);
728 inode->i_blocks = fs64_to_cpu(sb, ufs2_inode->ui_blocks);
729 inode->i_generation = fs32_to_cpu(sb, ufs2_inode->ui_gen);
730 ufsi->i_flags = fs32_to_cpu(sb, ufs2_inode->ui_flags);
732 ufsi->i_shadow = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_shadow);
733 ufsi->i_oeftflag = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_oeftflag);
736 if (S_ISCHR(mode) || S_ISBLK(mode) || inode->i_blocks) {
737 memcpy(ufsi->i_u1.u2_i_data, &ufs2_inode->ui_u2.ui_addr,
738 sizeof(ufs2_inode->ui_u2.ui_addr));
740 memcpy(ufsi->i_u1.i_symlink, ufs2_inode->ui_u2.ui_symlink,
741 sizeof(ufs2_inode->ui_u2.ui_symlink) - 1);
742 ufsi->i_u1.i_symlink[sizeof(ufs2_inode->ui_u2.ui_symlink) - 1] = 0;
747 struct inode *ufs_iget(struct super_block *sb, unsigned long ino)
749 struct ufs_inode_info *ufsi;
750 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
751 struct buffer_head * bh;
755 UFSD("ENTER, ino %lu\n", ino);
757 if (ino < UFS_ROOTINO || ino > (uspi->s_ncg * uspi->s_ipg)) {
758 ufs_warning(sb, "ufs_read_inode", "bad inode number (%lu)\n",
760 return ERR_PTR(-EIO);
763 inode = iget_locked(sb, ino);
765 return ERR_PTR(-ENOMEM);
766 if (!(inode->i_state & I_NEW))
771 bh = sb_bread(sb, uspi->s_sbbase + ufs_inotofsba(inode->i_ino));
773 ufs_warning(sb, "ufs_read_inode", "unable to read inode %lu\n",
777 if ((UFS_SB(sb)->s_flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) {
778 struct ufs2_inode *ufs2_inode = (struct ufs2_inode *)bh->b_data;
780 err = ufs2_read_inode(inode,
781 ufs2_inode + ufs_inotofsbo(inode->i_ino));
783 struct ufs_inode *ufs_inode = (struct ufs_inode *)bh->b_data;
785 err = ufs1_read_inode(inode,
786 ufs_inode + ufs_inotofsbo(inode->i_ino));
793 (inode->i_size + uspi->s_fsize - 1) >> uspi->s_fshift;
794 ufsi->i_dir_start_lookup = 0;
797 ufs_set_inode_ops(inode);
802 unlock_new_inode(inode);
807 return ERR_PTR(-EIO);
810 static void ufs1_update_inode(struct inode *inode, struct ufs_inode *ufs_inode)
812 struct super_block *sb = inode->i_sb;
813 struct ufs_inode_info *ufsi = UFS_I(inode);
815 ufs_inode->ui_mode = cpu_to_fs16(sb, inode->i_mode);
816 ufs_inode->ui_nlink = cpu_to_fs16(sb, inode->i_nlink);
818 ufs_set_inode_uid(sb, ufs_inode, i_uid_read(inode));
819 ufs_set_inode_gid(sb, ufs_inode, i_gid_read(inode));
821 ufs_inode->ui_size = cpu_to_fs64(sb, inode->i_size);
822 ufs_inode->ui_atime.tv_sec = cpu_to_fs32(sb, inode->i_atime.tv_sec);
823 ufs_inode->ui_atime.tv_usec = 0;
824 ufs_inode->ui_ctime.tv_sec = cpu_to_fs32(sb, inode->i_ctime.tv_sec);
825 ufs_inode->ui_ctime.tv_usec = 0;
826 ufs_inode->ui_mtime.tv_sec = cpu_to_fs32(sb, inode->i_mtime.tv_sec);
827 ufs_inode->ui_mtime.tv_usec = 0;
828 ufs_inode->ui_blocks = cpu_to_fs32(sb, inode->i_blocks);
829 ufs_inode->ui_flags = cpu_to_fs32(sb, ufsi->i_flags);
830 ufs_inode->ui_gen = cpu_to_fs32(sb, inode->i_generation);
832 if ((UFS_SB(sb)->s_flags & UFS_UID_MASK) == UFS_UID_EFT) {
833 ufs_inode->ui_u3.ui_sun.ui_shadow = cpu_to_fs32(sb, ufsi->i_shadow);
834 ufs_inode->ui_u3.ui_sun.ui_oeftflag = cpu_to_fs32(sb, ufsi->i_oeftflag);
837 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
838 /* ufs_inode->ui_u2.ui_addr.ui_db[0] = cpu_to_fs32(sb, inode->i_rdev); */
839 ufs_inode->ui_u2.ui_addr.ui_db[0] = ufsi->i_u1.i_data[0];
840 } else if (inode->i_blocks) {
841 memcpy(&ufs_inode->ui_u2.ui_addr, ufsi->i_u1.i_data,
842 sizeof(ufs_inode->ui_u2.ui_addr));
845 memcpy(&ufs_inode->ui_u2.ui_symlink, ufsi->i_u1.i_symlink,
846 sizeof(ufs_inode->ui_u2.ui_symlink));
850 memset (ufs_inode, 0, sizeof(struct ufs_inode));
853 static void ufs2_update_inode(struct inode *inode, struct ufs2_inode *ufs_inode)
855 struct super_block *sb = inode->i_sb;
856 struct ufs_inode_info *ufsi = UFS_I(inode);
859 ufs_inode->ui_mode = cpu_to_fs16(sb, inode->i_mode);
860 ufs_inode->ui_nlink = cpu_to_fs16(sb, inode->i_nlink);
862 ufs_inode->ui_uid = cpu_to_fs32(sb, i_uid_read(inode));
863 ufs_inode->ui_gid = cpu_to_fs32(sb, i_gid_read(inode));
865 ufs_inode->ui_size = cpu_to_fs64(sb, inode->i_size);
866 ufs_inode->ui_atime = cpu_to_fs64(sb, inode->i_atime.tv_sec);
867 ufs_inode->ui_atimensec = cpu_to_fs32(sb, inode->i_atime.tv_nsec);
868 ufs_inode->ui_ctime = cpu_to_fs64(sb, inode->i_ctime.tv_sec);
869 ufs_inode->ui_ctimensec = cpu_to_fs32(sb, inode->i_ctime.tv_nsec);
870 ufs_inode->ui_mtime = cpu_to_fs64(sb, inode->i_mtime.tv_sec);
871 ufs_inode->ui_mtimensec = cpu_to_fs32(sb, inode->i_mtime.tv_nsec);
873 ufs_inode->ui_blocks = cpu_to_fs64(sb, inode->i_blocks);
874 ufs_inode->ui_flags = cpu_to_fs32(sb, ufsi->i_flags);
875 ufs_inode->ui_gen = cpu_to_fs32(sb, inode->i_generation);
877 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
878 /* ufs_inode->ui_u2.ui_addr.ui_db[0] = cpu_to_fs32(sb, inode->i_rdev); */
879 ufs_inode->ui_u2.ui_addr.ui_db[0] = ufsi->i_u1.u2_i_data[0];
880 } else if (inode->i_blocks) {
881 memcpy(&ufs_inode->ui_u2.ui_addr, ufsi->i_u1.u2_i_data,
882 sizeof(ufs_inode->ui_u2.ui_addr));
884 memcpy(&ufs_inode->ui_u2.ui_symlink, ufsi->i_u1.i_symlink,
885 sizeof(ufs_inode->ui_u2.ui_symlink));
889 memset (ufs_inode, 0, sizeof(struct ufs2_inode));
893 static int ufs_update_inode(struct inode * inode, int do_sync)
895 struct super_block *sb = inode->i_sb;
896 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
897 struct buffer_head * bh;
899 UFSD("ENTER, ino %lu\n", inode->i_ino);
901 if (inode->i_ino < UFS_ROOTINO ||
902 inode->i_ino > (uspi->s_ncg * uspi->s_ipg)) {
903 ufs_warning (sb, "ufs_read_inode", "bad inode number (%lu)\n", inode->i_ino);
907 bh = sb_bread(sb, ufs_inotofsba(inode->i_ino));
909 ufs_warning (sb, "ufs_read_inode", "unable to read inode %lu\n", inode->i_ino);
912 if (uspi->fs_magic == UFS2_MAGIC) {
913 struct ufs2_inode *ufs2_inode = (struct ufs2_inode *)bh->b_data;
915 ufs2_update_inode(inode,
916 ufs2_inode + ufs_inotofsbo(inode->i_ino));
918 struct ufs_inode *ufs_inode = (struct ufs_inode *) bh->b_data;
920 ufs1_update_inode(inode, ufs_inode + ufs_inotofsbo(inode->i_ino));
923 mark_buffer_dirty(bh);
925 sync_dirty_buffer(bh);
932 int ufs_write_inode(struct inode *inode, struct writeback_control *wbc)
934 return ufs_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
937 int ufs_sync_inode (struct inode *inode)
939 return ufs_update_inode (inode, 1);
942 void ufs_evict_inode(struct inode * inode)
946 if (!inode->i_nlink && !is_bad_inode(inode))
949 truncate_inode_pages_final(&inode->i_data);
953 ufs_truncate_blocks(inode);
956 invalidate_inode_buffers(inode);
960 ufs_free_inode(inode);
963 #define DIRECT_BLOCK ((inode->i_size + uspi->s_bsize - 1) >> uspi->s_bshift)
964 #define DIRECT_FRAGMENT ((inode->i_size + uspi->s_fsize - 1) >> uspi->s_fshift)
966 static void ufs_trunc_direct(struct inode *inode)
968 struct ufs_inode_info *ufsi = UFS_I(inode);
969 struct super_block * sb;
970 struct ufs_sb_private_info * uspi;
972 u64 frag1, frag2, frag3, frag4, block1, block2;
973 unsigned frag_to_free, free_count;
976 UFSD("ENTER: ino %lu\n", inode->i_ino);
979 uspi = UFS_SB(sb)->s_uspi;
984 frag1 = DIRECT_FRAGMENT;
985 frag4 = min_t(u64, UFS_NDIR_FRAGMENT, ufsi->i_lastfrag);
986 frag2 = ((frag1 & uspi->s_fpbmask) ? ((frag1 | uspi->s_fpbmask) + 1) : frag1);
987 frag3 = frag4 & ~uspi->s_fpbmask;
992 } else if (frag2 < frag3) {
993 block1 = ufs_fragstoblks (frag2);
994 block2 = ufs_fragstoblks (frag3);
997 UFSD("ino %lu, frag1 %llu, frag2 %llu, block1 %llu, block2 %llu,"
998 " frag3 %llu, frag4 %llu\n", inode->i_ino,
999 (unsigned long long)frag1, (unsigned long long)frag2,
1000 (unsigned long long)block1, (unsigned long long)block2,
1001 (unsigned long long)frag3, (unsigned long long)frag4);
1007 * Free first free fragments
1009 p = ufs_get_direct_data_ptr(uspi, ufsi, ufs_fragstoblks(frag1));
1010 tmp = ufs_data_ptr_to_cpu(sb, p);
1012 ufs_panic (sb, "ufs_trunc_direct", "internal error");
1014 frag1 = ufs_fragnum (frag1);
1016 ufs_free_fragments(inode, tmp + frag1, frag2);
1017 mark_inode_dirty(inode);
1018 frag_to_free = tmp + frag1;
1024 for (i = block1 ; i < block2; i++) {
1025 p = ufs_get_direct_data_ptr(uspi, ufsi, i);
1026 tmp = ufs_data_ptr_to_cpu(sb, p);
1029 write_seqlock(&ufsi->meta_lock);
1030 ufs_data_ptr_clear(uspi, p);
1031 write_sequnlock(&ufsi->meta_lock);
1033 if (free_count == 0) {
1035 free_count = uspi->s_fpb;
1036 } else if (free_count > 0 && frag_to_free == tmp - free_count)
1037 free_count += uspi->s_fpb;
1039 ufs_free_blocks (inode, frag_to_free, free_count);
1041 free_count = uspi->s_fpb;
1043 mark_inode_dirty(inode);
1047 ufs_free_blocks (inode, frag_to_free, free_count);
1053 * Free last free fragments
1055 p = ufs_get_direct_data_ptr(uspi, ufsi, ufs_fragstoblks(frag3));
1056 tmp = ufs_data_ptr_to_cpu(sb, p);
1058 ufs_panic(sb, "ufs_truncate_direct", "internal error");
1059 frag4 = ufs_fragnum (frag4);
1060 write_seqlock(&ufsi->meta_lock);
1061 ufs_data_ptr_clear(uspi, p);
1062 write_sequnlock(&ufsi->meta_lock);
1064 ufs_free_fragments (inode, tmp, frag4);
1065 mark_inode_dirty(inode);
1068 UFSD("EXIT: ino %lu\n", inode->i_ino);
1072 static void ufs_trunc_indirect(struct inode *inode, unsigned *offsets, int depth2, void *p)
1074 struct super_block *sb = inode->i_sb;
1075 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
1076 struct ufs_buffer_head * ind_ubh;
1078 u64 tmp, frag_to_free = 0;
1079 unsigned free_count = 0;
1080 unsigned from = offsets ? *offsets : 0;
1081 bool to_free = !offsets || !depth2;
1084 tmp = ufs_data_ptr_to_cpu(sb, p);
1087 ind_ubh = ubh_bread(sb, tmp, uspi->s_bsize);
1089 write_seqlock(&UFS_I(inode)->meta_lock);
1090 ufs_data_ptr_clear(uspi, p);
1091 write_sequnlock(&UFS_I(inode)->meta_lock);
1095 for (i = from; i < uspi->s_apb; i++) {
1096 ind = ubh_get_data_ptr(uspi, ind_ubh, i);
1097 tmp = ufs_data_ptr_to_cpu(sb, ind);
1101 write_seqlock(&UFS_I(inode)->meta_lock);
1102 ufs_data_ptr_clear(uspi, ind);
1103 write_sequnlock(&UFS_I(inode)->meta_lock);
1104 ubh_mark_buffer_dirty(ind_ubh);
1105 if (free_count == 0) {
1107 free_count = uspi->s_fpb;
1108 } else if (free_count > 0 && frag_to_free == tmp - free_count)
1109 free_count += uspi->s_fpb;
1111 ufs_free_blocks (inode, frag_to_free, free_count);
1113 free_count = uspi->s_fpb;
1116 mark_inode_dirty(inode);
1119 if (free_count > 0) {
1120 ufs_free_blocks (inode, frag_to_free, free_count);
1123 tmp = ufs_data_ptr_to_cpu(sb, p);
1124 write_seqlock(&UFS_I(inode)->meta_lock);
1125 ufs_data_ptr_clear(uspi, p);
1126 write_sequnlock(&UFS_I(inode)->meta_lock);
1128 ubh_bforget(ind_ubh);
1129 ufs_free_blocks (inode, tmp, uspi->s_fpb);
1130 mark_inode_dirty(inode);
1133 if (IS_SYNC(inode) && ubh_buffer_dirty(ind_ubh))
1134 ubh_sync_block(ind_ubh);
1135 ubh_brelse (ind_ubh);
1138 static void ufs_trunc_dindirect(struct inode *inode, unsigned *offsets, int depth2, void *p)
1140 struct super_block *sb = inode->i_sb;
1141 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
1142 struct ufs_buffer_head *dind_bh;
1145 bool free_it = !offsets || !depth2;
1146 unsigned dindirect_block = offsets ? *offsets++ : 0;
1149 tmp = ufs_data_ptr_to_cpu(sb, p);
1152 dind_bh = ubh_bread(sb, tmp, uspi->s_bsize);
1154 write_seqlock(&UFS_I(inode)->meta_lock);
1155 ufs_data_ptr_clear(uspi, p);
1156 write_sequnlock(&UFS_I(inode)->meta_lock);
1160 for (i = dindirect_block ; i < uspi->s_apb ; i++, offsets = NULL) {
1161 dind = ubh_get_data_ptr(uspi, dind_bh, i);
1162 tmp = ufs_data_ptr_to_cpu(sb, dind);
1165 ufs_trunc_indirect(inode, offsets, depth2 - 1, dind);
1166 ubh_mark_buffer_dirty(dind_bh);
1170 tmp = ufs_data_ptr_to_cpu(sb, p);
1171 write_seqlock(&UFS_I(inode)->meta_lock);
1172 ufs_data_ptr_clear(uspi, p);
1173 write_sequnlock(&UFS_I(inode)->meta_lock);
1175 ubh_bforget(dind_bh);
1176 ufs_free_blocks(inode, tmp, uspi->s_fpb);
1177 mark_inode_dirty(inode);
1180 if (IS_SYNC(inode) && ubh_buffer_dirty(dind_bh))
1181 ubh_sync_block(dind_bh);
1182 ubh_brelse (dind_bh);
1185 static void ufs_trunc_tindirect(struct inode *inode, unsigned *offsets, int depth2)
1187 struct super_block *sb = inode->i_sb;
1188 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
1189 struct ufs_inode_info *ufsi = UFS_I(inode);
1190 struct ufs_buffer_head * tind_bh;
1193 bool free_it = !offsets || !depth2;
1194 unsigned tindirect_block = offsets ? *offsets++ : 0;
1197 p = ufs_get_direct_data_ptr(uspi, ufsi, UFS_TIND_BLOCK);
1198 if (!(tmp = ufs_data_ptr_to_cpu(sb, p)))
1200 tind_bh = ubh_bread (sb, tmp, uspi->s_bsize);
1202 write_seqlock(&ufsi->meta_lock);
1203 ufs_data_ptr_clear(uspi, p);
1204 write_sequnlock(&ufsi->meta_lock);
1208 for (i = tindirect_block ; i < uspi->s_apb ; i++, offsets = NULL) {
1209 tind = ubh_get_data_ptr(uspi, tind_bh, i);
1210 ufs_trunc_dindirect(inode, offsets, depth2 - 1, tind);
1211 ubh_mark_buffer_dirty(tind_bh);
1214 tmp = ufs_data_ptr_to_cpu(sb, p);
1215 write_seqlock(&ufsi->meta_lock);
1216 ufs_data_ptr_clear(uspi, p);
1217 write_sequnlock(&ufsi->meta_lock);
1219 ubh_bforget(tind_bh);
1220 ufs_free_blocks(inode, tmp, uspi->s_fpb);
1221 mark_inode_dirty(inode);
1224 if (IS_SYNC(inode) && ubh_buffer_dirty(tind_bh))
1225 ubh_sync_block(tind_bh);
1226 ubh_brelse (tind_bh);
1229 static int ufs_alloc_lastblock(struct inode *inode, loff_t size)
1232 struct super_block *sb = inode->i_sb;
1233 struct address_space *mapping = inode->i_mapping;
1234 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
1237 struct page *lastpage;
1238 struct buffer_head *bh;
1241 lastfrag = (size + uspi->s_fsize - 1) >> uspi->s_fshift;
1248 lastpage = ufs_get_locked_page(mapping, lastfrag >>
1249 (PAGE_CACHE_SHIFT - inode->i_blkbits));
1250 if (IS_ERR(lastpage)) {
1255 end = lastfrag & ((1 << (PAGE_CACHE_SHIFT - inode->i_blkbits)) - 1);
1256 bh = page_buffers(lastpage);
1257 for (i = 0; i < end; ++i)
1258 bh = bh->b_this_page;
1261 err = ufs_getfrag_block(inode, lastfrag, bh, 1);
1266 if (buffer_new(bh)) {
1267 clear_buffer_new(bh);
1268 unmap_underlying_metadata(bh->b_bdev,
1271 * we do not zeroize fragment, because of
1272 * if it maped to hole, it already contains zeroes
1274 set_buffer_uptodate(bh);
1275 mark_buffer_dirty(bh);
1276 set_page_dirty(lastpage);
1279 if (lastfrag >= UFS_IND_FRAGMENT) {
1280 end = uspi->s_fpb - ufs_fragnum(lastfrag) - 1;
1281 phys64 = bh->b_blocknr + 1;
1282 for (i = 0; i < end; ++i) {
1283 bh = sb_getblk(sb, i + phys64);
1285 memset(bh->b_data, 0, sb->s_blocksize);
1286 set_buffer_uptodate(bh);
1287 mark_buffer_dirty(bh);
1289 sync_dirty_buffer(bh);
1294 ufs_put_locked_page(lastpage);
1299 static void __ufs_truncate_blocks(struct inode *inode)
1301 struct ufs_inode_info *ufsi = UFS_I(inode);
1302 struct super_block *sb = inode->i_sb;
1303 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
1304 unsigned offsets[4];
1305 int depth = ufs_block_to_path(inode, DIRECT_BLOCK, offsets);
1311 /* find the last non-zero in offsets[] */
1312 for (depth2 = depth - 1; depth2; depth2--)
1313 if (offsets[depth2])
1316 mutex_lock(&ufsi->truncate_mutex);
1319 ufs_trunc_direct(inode);
1320 ufs_trunc_indirect(inode, NULL, 0,
1321 ufs_get_direct_data_ptr(uspi, ufsi, UFS_IND_BLOCK));
1322 ufs_trunc_dindirect(inode, NULL, 0,
1323 ufs_get_direct_data_ptr(uspi, ufsi, UFS_DIND_BLOCK));
1324 ufs_trunc_tindirect(inode, NULL, 0);
1327 ufs_trunc_indirect(inode, offsets + 1, depth2,
1328 ufs_get_direct_data_ptr(uspi, ufsi, UFS_IND_BLOCK));
1329 ufs_trunc_dindirect(inode, NULL, 0,
1330 ufs_get_direct_data_ptr(uspi, ufsi, UFS_DIND_BLOCK));
1331 ufs_trunc_tindirect(inode, NULL, 0);
1334 ufs_trunc_dindirect(inode, offsets + 1, depth2,
1335 ufs_get_direct_data_ptr(uspi, ufsi, UFS_DIND_BLOCK));
1336 ufs_trunc_tindirect(inode, NULL, 0);
1339 ufs_trunc_tindirect(inode, offsets + 1, depth2);
1341 ufsi->i_lastfrag = DIRECT_FRAGMENT;
1342 mutex_unlock(&ufsi->truncate_mutex);
1345 static int ufs_truncate(struct inode *inode, loff_t size)
1349 UFSD("ENTER: ino %lu, i_size: %llu, old_i_size: %llu\n",
1350 inode->i_ino, (unsigned long long)size,
1351 (unsigned long long)i_size_read(inode));
1353 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1354 S_ISLNK(inode->i_mode)))
1356 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1359 err = ufs_alloc_lastblock(inode, size);
1364 block_truncate_page(inode->i_mapping, size, ufs_getfrag_block);
1366 truncate_setsize(inode, size);
1368 __ufs_truncate_blocks(inode);
1369 inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
1370 mark_inode_dirty(inode);
1372 UFSD("EXIT: err %d\n", err);
1376 void ufs_truncate_blocks(struct inode *inode)
1378 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1379 S_ISLNK(inode->i_mode)))
1381 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1383 __ufs_truncate_blocks(inode);
1386 int ufs_setattr(struct dentry *dentry, struct iattr *attr)
1388 struct inode *inode = d_inode(dentry);
1389 unsigned int ia_valid = attr->ia_valid;
1392 error = inode_change_ok(inode, attr);
1396 if (ia_valid & ATTR_SIZE && attr->ia_size != inode->i_size) {
1397 error = ufs_truncate(inode, attr->ia_size);
1402 setattr_copy(inode, attr);
1403 mark_inode_dirty(inode);
1407 const struct inode_operations ufs_file_inode_operations = {
1408 .setattr = ufs_setattr,