1 // SPDX-License-Identifier: GPL-2.0
6 * Daniel Pirkl <daniel.pirkl@email.cz>
7 * Charles University, Faculty of Mathematics and Physics
11 * linux/fs/ext2/inode.c
13 * Copyright (C) 1992, 1993, 1994, 1995
14 * Remy Card (card@masi.ibp.fr)
15 * Laboratoire MASI - Institut Blaise Pascal
16 * Universite Pierre et Marie Curie (Paris VI)
20 * linux/fs/minix/inode.c
22 * Copyright (C) 1991, 1992 Linus Torvalds
24 * Goal-directed block allocation by Stephen Tweedie (sct@dcs.ed.ac.uk), 1993
25 * Big-endian to little-endian byte-swapping/bitmaps by
26 * David S. Miller (davem@caip.rutgers.edu), 1995
29 #include <linux/uaccess.h>
31 #include <linux/errno.h>
33 #include <linux/time.h>
34 #include <linux/stat.h>
35 #include <linux/string.h>
37 #include <linux/buffer_head.h>
38 #include <linux/writeback.h>
45 static int ufs_block_to_path(struct inode
*inode
, sector_t i_block
, unsigned offsets
[4])
47 struct ufs_sb_private_info
*uspi
= UFS_SB(inode
->i_sb
)->s_uspi
;
48 int ptrs
= uspi
->s_apb
;
49 int ptrs_bits
= uspi
->s_apbshift
;
50 const long direct_blocks
= UFS_NDADDR
,
51 indirect_blocks
= ptrs
,
52 double_blocks
= (1 << (ptrs_bits
* 2));
56 UFSD("ptrs=uspi->s_apb = %d,double_blocks=%ld \n",ptrs
,double_blocks
);
57 if (i_block
< direct_blocks
) {
58 offsets
[n
++] = i_block
;
59 } else if ((i_block
-= direct_blocks
) < indirect_blocks
) {
60 offsets
[n
++] = UFS_IND_BLOCK
;
61 offsets
[n
++] = i_block
;
62 } else if ((i_block
-= indirect_blocks
) < double_blocks
) {
63 offsets
[n
++] = UFS_DIND_BLOCK
;
64 offsets
[n
++] = i_block
>> ptrs_bits
;
65 offsets
[n
++] = i_block
& (ptrs
- 1);
66 } else if (((i_block
-= double_blocks
) >> (ptrs_bits
* 2)) < ptrs
) {
67 offsets
[n
++] = UFS_TIND_BLOCK
;
68 offsets
[n
++] = i_block
>> (ptrs_bits
* 2);
69 offsets
[n
++] = (i_block
>> ptrs_bits
) & (ptrs
- 1);
70 offsets
[n
++] = i_block
& (ptrs
- 1);
72 ufs_warning(inode
->i_sb
, "ufs_block_to_path", "block > big");
83 struct buffer_head
*bh
;
86 static inline int grow_chain32(struct ufs_inode_info
*ufsi
,
87 struct buffer_head
*bh
, __fs32
*v
,
88 Indirect
*from
, Indirect
*to
)
94 seq
= read_seqbegin(&ufsi
->meta_lock
);
95 to
->key32
= *(__fs32
*)(to
->p
= v
);
96 for (p
= from
; p
<= to
&& p
->key32
== *(__fs32
*)p
->p
; p
++)
98 } while (read_seqretry(&ufsi
->meta_lock
, seq
));
102 static inline int grow_chain64(struct ufs_inode_info
*ufsi
,
103 struct buffer_head
*bh
, __fs64
*v
,
104 Indirect
*from
, Indirect
*to
)
110 seq
= read_seqbegin(&ufsi
->meta_lock
);
111 to
->key64
= *(__fs64
*)(to
->p
= v
);
112 for (p
= from
; p
<= to
&& p
->key64
== *(__fs64
*)p
->p
; p
++)
114 } while (read_seqretry(&ufsi
->meta_lock
, seq
));
119 * Returns the location of the fragment from
120 * the beginning of the filesystem.
123 static u64
ufs_frag_map(struct inode
*inode
, unsigned offsets
[4], int depth
)
125 struct ufs_inode_info
*ufsi
= UFS_I(inode
);
126 struct super_block
*sb
= inode
->i_sb
;
127 struct ufs_sb_private_info
*uspi
= UFS_SB(sb
)->s_uspi
;
128 u64 mask
= (u64
) uspi
->s_apbmask
>>uspi
->s_fpbshift
;
129 int shift
= uspi
->s_apbshift
-uspi
->s_fpbshift
;
130 Indirect chain
[4], *q
= chain
;
132 unsigned flags
= UFS_SB(sb
)->s_flags
;
135 UFSD(": uspi->s_fpbshift = %d ,uspi->s_apbmask = %x, mask=%llx\n",
136 uspi
->s_fpbshift
, uspi
->s_apbmask
,
137 (unsigned long long)mask
);
145 if ((flags
& UFS_TYPE_MASK
) == UFS_TYPE_UFS2
)
148 if (!grow_chain32(ufsi
, NULL
, &ufsi
->i_u1
.i_data
[*p
++], chain
, q
))
154 struct buffer_head
*bh
;
157 bh
= sb_bread(sb
, uspi
->s_sbbase
+
158 fs32_to_cpu(sb
, q
->key32
) + (n
>>shift
));
161 ptr
= (__fs32
*)bh
->b_data
+ (n
& mask
);
162 if (!grow_chain32(ufsi
, bh
, ptr
, chain
, ++q
))
167 res
= fs32_to_cpu(sb
, q
->key32
);
171 if (!grow_chain64(ufsi
, NULL
, &ufsi
->i_u1
.u2_i_data
[*p
++], chain
, q
))
178 struct buffer_head
*bh
;
181 bh
= sb_bread(sb
, uspi
->s_sbbase
+
182 fs64_to_cpu(sb
, q
->key64
) + (n
>>shift
));
185 ptr
= (__fs64
*)bh
->b_data
+ (n
& mask
);
186 if (!grow_chain64(ufsi
, bh
, ptr
, chain
, ++q
))
191 res
= fs64_to_cpu(sb
, q
->key64
);
193 res
+= uspi
->s_sbbase
;
210 * Unpacking tails: we have a file with partial final block and
211 * we had been asked to extend it. If the fragment being written
212 * is within the same block, we need to extend the tail just to cover
213 * that fragment. Otherwise the tail is extended to full block.
215 * Note that we might need to create a _new_ tail, but that will
216 * be handled elsewhere; this is strictly for resizing old
220 ufs_extend_tail(struct inode
*inode
, u64 writes_to
,
221 int *err
, struct page
*locked_page
)
223 struct ufs_inode_info
*ufsi
= UFS_I(inode
);
224 struct super_block
*sb
= inode
->i_sb
;
225 struct ufs_sb_private_info
*uspi
= UFS_SB(sb
)->s_uspi
;
226 unsigned lastfrag
= ufsi
->i_lastfrag
; /* it's a short file, so unsigned is enough */
227 unsigned block
= ufs_fragstoblks(lastfrag
);
232 if (writes_to
< (lastfrag
| uspi
->s_fpbmask
))
233 new_size
= (writes_to
& uspi
->s_fpbmask
) + 1;
235 new_size
= uspi
->s_fpb
;
237 p
= ufs_get_direct_data_ptr(uspi
, ufsi
, block
);
238 tmp
= ufs_new_fragments(inode
, p
, lastfrag
, ufs_data_ptr_to_cpu(sb
, p
),
239 new_size
- (lastfrag
& uspi
->s_fpbmask
), err
,
245 * ufs_inode_getfrag() - allocate new fragment(s)
246 * @inode: pointer to inode
247 * @index: number of block pointer within the inode's array.
248 * @new_fragment: number of new allocated fragment(s)
249 * @err: we set it if something wrong
250 * @new: we set it if we allocate new block
251 * @locked_page: for ufs_new_fragments()
254 ufs_inode_getfrag(struct inode
*inode
, unsigned index
,
255 sector_t new_fragment
, int *err
,
256 int *new, struct page
*locked_page
)
258 struct ufs_inode_info
*ufsi
= UFS_I(inode
);
259 struct super_block
*sb
= inode
->i_sb
;
260 struct ufs_sb_private_info
*uspi
= UFS_SB(sb
)->s_uspi
;
261 u64 tmp
, goal
, lastfrag
;
262 unsigned nfrags
= uspi
->s_fpb
;
265 /* TODO : to be done for write support
266 if ( (flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
270 p
= ufs_get_direct_data_ptr(uspi
, ufsi
, index
);
271 tmp
= ufs_data_ptr_to_cpu(sb
, p
);
275 lastfrag
= ufsi
->i_lastfrag
;
277 /* will that be a new tail? */
278 if (new_fragment
< UFS_NDIR_FRAGMENT
&& new_fragment
>= lastfrag
)
279 nfrags
= (new_fragment
& uspi
->s_fpbmask
) + 1;
283 goal
= ufs_data_ptr_to_cpu(sb
,
284 ufs_get_direct_data_ptr(uspi
, ufsi
, index
- 1));
288 tmp
= ufs_new_fragments(inode
, p
, ufs_blknum(new_fragment
),
289 goal
, nfrags
, err
, locked_page
);
298 inode
->i_ctime
= current_time(inode
);
300 ufs_sync_inode (inode
);
301 mark_inode_dirty(inode
);
303 return tmp
+ uspi
->s_sbbase
;
305 /* This part : To be implemented ....
306 Required only for writing, not required for READ-ONLY.
309 u2_block = ufs_fragstoblks(fragment);
310 u2_blockoff = ufs_fragnum(fragment);
311 p = ufsi->i_u1.u2_i_data + block;
315 tmp = fs32_to_cpu(sb, *p);
316 lastfrag = ufsi->i_lastfrag;
322 * ufs_inode_getblock() - allocate new block
323 * @inode: pointer to inode
324 * @ind_block: block number of the indirect block
325 * @index: number of pointer within the indirect block
326 * @new_fragment: number of new allocated fragment
327 * (block will hold this fragment and also uspi->s_fpb-1)
328 * @err: see ufs_inode_getfrag()
329 * @new: see ufs_inode_getfrag()
330 * @locked_page: see ufs_inode_getfrag()
333 ufs_inode_getblock(struct inode
*inode
, u64 ind_block
,
334 unsigned index
, sector_t new_fragment
, int *err
,
335 int *new, struct page
*locked_page
)
337 struct super_block
*sb
= inode
->i_sb
;
338 struct ufs_sb_private_info
*uspi
= UFS_SB(sb
)->s_uspi
;
339 int shift
= uspi
->s_apbshift
- uspi
->s_fpbshift
;
341 struct buffer_head
*bh
;
347 bh
= sb_bread(sb
, ind_block
+ (index
>> shift
));
353 index
&= uspi
->s_apbmask
>> uspi
->s_fpbshift
;
354 if (uspi
->fs_magic
== UFS2_MAGIC
)
355 p
= (__fs64
*)bh
->b_data
+ index
;
357 p
= (__fs32
*)bh
->b_data
+ index
;
359 tmp
= ufs_data_ptr_to_cpu(sb
, p
);
363 if (index
&& (uspi
->fs_magic
== UFS2_MAGIC
?
364 (tmp
= fs64_to_cpu(sb
, ((__fs64
*)bh
->b_data
)[index
-1])) :
365 (tmp
= fs32_to_cpu(sb
, ((__fs32
*)bh
->b_data
)[index
-1]))))
366 goal
= tmp
+ uspi
->s_fpb
;
368 goal
= bh
->b_blocknr
+ uspi
->s_fpb
;
369 tmp
= ufs_new_fragments(inode
, p
, ufs_blknum(new_fragment
), goal
,
370 uspi
->s_fpb
, err
, locked_page
);
377 mark_buffer_dirty(bh
);
379 sync_dirty_buffer(bh
);
380 inode
->i_ctime
= current_time(inode
);
381 mark_inode_dirty(inode
);
386 tmp
+= uspi
->s_sbbase
;
391 * ufs_getfrag_block() - `get_block_t' function, interface between UFS and
392 * readpage, writepage and so on
395 static int ufs_getfrag_block(struct inode
*inode
, sector_t fragment
, struct buffer_head
*bh_result
, int create
)
397 struct super_block
*sb
= inode
->i_sb
;
398 struct ufs_sb_private_info
*uspi
= UFS_SB(sb
)->s_uspi
;
399 int err
= 0, new = 0;
401 int depth
= ufs_block_to_path(inode
, fragment
>> uspi
->s_fpbshift
, offsets
);
403 unsigned frag
= fragment
& uspi
->s_fpbmask
;
405 phys64
= ufs_frag_map(inode
, offsets
, depth
);
410 if (fragment
>= UFS_NDIR_FRAGMENT
)
412 read_seqlock_excl(&UFS_I(inode
)->meta_lock
);
413 if (fragment
< UFS_I(inode
)->i_lastfrag
) {
414 read_sequnlock_excl(&UFS_I(inode
)->meta_lock
);
417 read_sequnlock_excl(&UFS_I(inode
)->meta_lock
);
419 /* This code entered only while writing ....? */
421 mutex_lock(&UFS_I(inode
)->truncate_mutex
);
423 UFSD("ENTER, ino %lu, fragment %llu\n", inode
->i_ino
, (unsigned long long)fragment
);
424 if (unlikely(!depth
)) {
425 ufs_warning(sb
, "ufs_get_block", "block > big");
430 if (UFS_I(inode
)->i_lastfrag
< UFS_NDIR_FRAGMENT
) {
431 unsigned lastfrag
= UFS_I(inode
)->i_lastfrag
;
432 unsigned tailfrags
= lastfrag
& uspi
->s_fpbmask
;
433 if (tailfrags
&& fragment
>= lastfrag
) {
434 if (!ufs_extend_tail(inode
, fragment
,
435 &err
, bh_result
->b_page
))
441 phys64
= ufs_inode_getfrag(inode
, offsets
[0], fragment
,
442 &err
, &new, bh_result
->b_page
);
445 phys64
= ufs_inode_getfrag(inode
, offsets
[0], fragment
,
447 for (i
= 1; i
< depth
- 1; i
++)
448 phys64
= ufs_inode_getblock(inode
, phys64
, offsets
[i
],
449 fragment
, &err
, NULL
, NULL
);
450 phys64
= ufs_inode_getblock(inode
, phys64
, offsets
[depth
- 1],
451 fragment
, &err
, &new, bh_result
->b_page
);
456 map_bh(bh_result
, sb
, phys64
);
458 set_buffer_new(bh_result
);
460 mutex_unlock(&UFS_I(inode
)->truncate_mutex
);
465 map_bh(bh_result
, sb
, phys64
+ frag
);
469 static int ufs_writepage(struct page
*page
, struct writeback_control
*wbc
)
471 return block_write_full_page(page
,ufs_getfrag_block
,wbc
);
474 static int ufs_readpage(struct file
*file
, struct page
*page
)
476 return block_read_full_page(page
,ufs_getfrag_block
);
479 int ufs_prepare_chunk(struct page
*page
, loff_t pos
, unsigned len
)
481 return __block_write_begin(page
, pos
, len
, ufs_getfrag_block
);
484 static void ufs_truncate_blocks(struct inode
*);
486 static void ufs_write_failed(struct address_space
*mapping
, loff_t to
)
488 struct inode
*inode
= mapping
->host
;
490 if (to
> inode
->i_size
) {
491 truncate_pagecache(inode
, inode
->i_size
);
492 ufs_truncate_blocks(inode
);
496 static int ufs_write_begin(struct file
*file
, struct address_space
*mapping
,
497 loff_t pos
, unsigned len
, unsigned flags
,
498 struct page
**pagep
, void **fsdata
)
502 ret
= block_write_begin(mapping
, pos
, len
, flags
, pagep
,
505 ufs_write_failed(mapping
, pos
+ len
);
510 static int ufs_write_end(struct file
*file
, struct address_space
*mapping
,
511 loff_t pos
, unsigned len
, unsigned copied
,
512 struct page
*page
, void *fsdata
)
516 ret
= generic_write_end(file
, mapping
, pos
, len
, copied
, page
, fsdata
);
518 ufs_write_failed(mapping
, pos
+ len
);
522 static sector_t
ufs_bmap(struct address_space
*mapping
, sector_t block
)
524 return generic_block_bmap(mapping
,block
,ufs_getfrag_block
);
527 const struct address_space_operations ufs_aops
= {
528 .readpage
= ufs_readpage
,
529 .writepage
= ufs_writepage
,
530 .write_begin
= ufs_write_begin
,
531 .write_end
= ufs_write_end
,
535 static void ufs_set_inode_ops(struct inode
*inode
)
537 if (S_ISREG(inode
->i_mode
)) {
538 inode
->i_op
= &ufs_file_inode_operations
;
539 inode
->i_fop
= &ufs_file_operations
;
540 inode
->i_mapping
->a_ops
= &ufs_aops
;
541 } else if (S_ISDIR(inode
->i_mode
)) {
542 inode
->i_op
= &ufs_dir_inode_operations
;
543 inode
->i_fop
= &ufs_dir_operations
;
544 inode
->i_mapping
->a_ops
= &ufs_aops
;
545 } else if (S_ISLNK(inode
->i_mode
)) {
546 if (!inode
->i_blocks
) {
547 inode
->i_link
= (char *)UFS_I(inode
)->i_u1
.i_symlink
;
548 inode
->i_op
= &simple_symlink_inode_operations
;
550 inode
->i_mapping
->a_ops
= &ufs_aops
;
551 inode
->i_op
= &page_symlink_inode_operations
;
552 inode_nohighmem(inode
);
555 init_special_inode(inode
, inode
->i_mode
,
556 ufs_get_inode_dev(inode
->i_sb
, UFS_I(inode
)));
559 static int ufs1_read_inode(struct inode
*inode
, struct ufs_inode
*ufs_inode
)
561 struct ufs_inode_info
*ufsi
= UFS_I(inode
);
562 struct super_block
*sb
= inode
->i_sb
;
566 * Copy data to the in-core inode.
568 inode
->i_mode
= mode
= fs16_to_cpu(sb
, ufs_inode
->ui_mode
);
569 set_nlink(inode
, fs16_to_cpu(sb
, ufs_inode
->ui_nlink
));
570 if (inode
->i_nlink
== 0)
574 * Linux now has 32-bit uid and gid, so we can support EFT.
576 i_uid_write(inode
, ufs_get_inode_uid(sb
, ufs_inode
));
577 i_gid_write(inode
, ufs_get_inode_gid(sb
, ufs_inode
));
579 inode
->i_size
= fs64_to_cpu(sb
, ufs_inode
->ui_size
);
580 inode
->i_atime
.tv_sec
= (signed)fs32_to_cpu(sb
, ufs_inode
->ui_atime
.tv_sec
);
581 inode
->i_ctime
.tv_sec
= (signed)fs32_to_cpu(sb
, ufs_inode
->ui_ctime
.tv_sec
);
582 inode
->i_mtime
.tv_sec
= (signed)fs32_to_cpu(sb
, ufs_inode
->ui_mtime
.tv_sec
);
583 inode
->i_mtime
.tv_nsec
= 0;
584 inode
->i_atime
.tv_nsec
= 0;
585 inode
->i_ctime
.tv_nsec
= 0;
586 inode
->i_blocks
= fs32_to_cpu(sb
, ufs_inode
->ui_blocks
);
587 inode
->i_generation
= fs32_to_cpu(sb
, ufs_inode
->ui_gen
);
588 ufsi
->i_flags
= fs32_to_cpu(sb
, ufs_inode
->ui_flags
);
589 ufsi
->i_shadow
= fs32_to_cpu(sb
, ufs_inode
->ui_u3
.ui_sun
.ui_shadow
);
590 ufsi
->i_oeftflag
= fs32_to_cpu(sb
, ufs_inode
->ui_u3
.ui_sun
.ui_oeftflag
);
593 if (S_ISCHR(mode
) || S_ISBLK(mode
) || inode
->i_blocks
) {
594 memcpy(ufsi
->i_u1
.i_data
, &ufs_inode
->ui_u2
.ui_addr
,
595 sizeof(ufs_inode
->ui_u2
.ui_addr
));
597 memcpy(ufsi
->i_u1
.i_symlink
, ufs_inode
->ui_u2
.ui_symlink
,
598 sizeof(ufs_inode
->ui_u2
.ui_symlink
) - 1);
599 ufsi
->i_u1
.i_symlink
[sizeof(ufs_inode
->ui_u2
.ui_symlink
) - 1] = 0;
604 static int ufs2_read_inode(struct inode
*inode
, struct ufs2_inode
*ufs2_inode
)
606 struct ufs_inode_info
*ufsi
= UFS_I(inode
);
607 struct super_block
*sb
= inode
->i_sb
;
610 UFSD("Reading ufs2 inode, ino %lu\n", inode
->i_ino
);
612 * Copy data to the in-core inode.
614 inode
->i_mode
= mode
= fs16_to_cpu(sb
, ufs2_inode
->ui_mode
);
615 set_nlink(inode
, fs16_to_cpu(sb
, ufs2_inode
->ui_nlink
));
616 if (inode
->i_nlink
== 0)
620 * Linux now has 32-bit uid and gid, so we can support EFT.
622 i_uid_write(inode
, fs32_to_cpu(sb
, ufs2_inode
->ui_uid
));
623 i_gid_write(inode
, fs32_to_cpu(sb
, ufs2_inode
->ui_gid
));
625 inode
->i_size
= fs64_to_cpu(sb
, ufs2_inode
->ui_size
);
626 inode
->i_atime
.tv_sec
= fs64_to_cpu(sb
, ufs2_inode
->ui_atime
);
627 inode
->i_ctime
.tv_sec
= fs64_to_cpu(sb
, ufs2_inode
->ui_ctime
);
628 inode
->i_mtime
.tv_sec
= fs64_to_cpu(sb
, ufs2_inode
->ui_mtime
);
629 inode
->i_atime
.tv_nsec
= fs32_to_cpu(sb
, ufs2_inode
->ui_atimensec
);
630 inode
->i_ctime
.tv_nsec
= fs32_to_cpu(sb
, ufs2_inode
->ui_ctimensec
);
631 inode
->i_mtime
.tv_nsec
= fs32_to_cpu(sb
, ufs2_inode
->ui_mtimensec
);
632 inode
->i_blocks
= fs64_to_cpu(sb
, ufs2_inode
->ui_blocks
);
633 inode
->i_generation
= fs32_to_cpu(sb
, ufs2_inode
->ui_gen
);
634 ufsi
->i_flags
= fs32_to_cpu(sb
, ufs2_inode
->ui_flags
);
636 ufsi->i_shadow = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_shadow);
637 ufsi->i_oeftflag = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_oeftflag);
640 if (S_ISCHR(mode
) || S_ISBLK(mode
) || inode
->i_blocks
) {
641 memcpy(ufsi
->i_u1
.u2_i_data
, &ufs2_inode
->ui_u2
.ui_addr
,
642 sizeof(ufs2_inode
->ui_u2
.ui_addr
));
644 memcpy(ufsi
->i_u1
.i_symlink
, ufs2_inode
->ui_u2
.ui_symlink
,
645 sizeof(ufs2_inode
->ui_u2
.ui_symlink
) - 1);
646 ufsi
->i_u1
.i_symlink
[sizeof(ufs2_inode
->ui_u2
.ui_symlink
) - 1] = 0;
651 struct inode
*ufs_iget(struct super_block
*sb
, unsigned long ino
)
653 struct ufs_inode_info
*ufsi
;
654 struct ufs_sb_private_info
*uspi
= UFS_SB(sb
)->s_uspi
;
655 struct buffer_head
* bh
;
659 UFSD("ENTER, ino %lu\n", ino
);
661 if (ino
< UFS_ROOTINO
|| ino
> (uspi
->s_ncg
* uspi
->s_ipg
)) {
662 ufs_warning(sb
, "ufs_read_inode", "bad inode number (%lu)\n",
664 return ERR_PTR(-EIO
);
667 inode
= iget_locked(sb
, ino
);
669 return ERR_PTR(-ENOMEM
);
670 if (!(inode
->i_state
& I_NEW
))
675 bh
= sb_bread(sb
, uspi
->s_sbbase
+ ufs_inotofsba(inode
->i_ino
));
677 ufs_warning(sb
, "ufs_read_inode", "unable to read inode %lu\n",
681 if ((UFS_SB(sb
)->s_flags
& UFS_TYPE_MASK
) == UFS_TYPE_UFS2
) {
682 struct ufs2_inode
*ufs2_inode
= (struct ufs2_inode
*)bh
->b_data
;
684 err
= ufs2_read_inode(inode
,
685 ufs2_inode
+ ufs_inotofsbo(inode
->i_ino
));
687 struct ufs_inode
*ufs_inode
= (struct ufs_inode
*)bh
->b_data
;
689 err
= ufs1_read_inode(inode
,
690 ufs_inode
+ ufs_inotofsbo(inode
->i_ino
));
698 (inode
->i_size
+ uspi
->s_fsize
- 1) >> uspi
->s_fshift
;
699 ufsi
->i_dir_start_lookup
= 0;
702 ufs_set_inode_ops(inode
);
705 unlock_new_inode(inode
);
713 static void ufs1_update_inode(struct inode
*inode
, struct ufs_inode
*ufs_inode
)
715 struct super_block
*sb
= inode
->i_sb
;
716 struct ufs_inode_info
*ufsi
= UFS_I(inode
);
718 ufs_inode
->ui_mode
= cpu_to_fs16(sb
, inode
->i_mode
);
719 ufs_inode
->ui_nlink
= cpu_to_fs16(sb
, inode
->i_nlink
);
721 ufs_set_inode_uid(sb
, ufs_inode
, i_uid_read(inode
));
722 ufs_set_inode_gid(sb
, ufs_inode
, i_gid_read(inode
));
724 ufs_inode
->ui_size
= cpu_to_fs64(sb
, inode
->i_size
);
725 ufs_inode
->ui_atime
.tv_sec
= cpu_to_fs32(sb
, inode
->i_atime
.tv_sec
);
726 ufs_inode
->ui_atime
.tv_usec
= 0;
727 ufs_inode
->ui_ctime
.tv_sec
= cpu_to_fs32(sb
, inode
->i_ctime
.tv_sec
);
728 ufs_inode
->ui_ctime
.tv_usec
= 0;
729 ufs_inode
->ui_mtime
.tv_sec
= cpu_to_fs32(sb
, inode
->i_mtime
.tv_sec
);
730 ufs_inode
->ui_mtime
.tv_usec
= 0;
731 ufs_inode
->ui_blocks
= cpu_to_fs32(sb
, inode
->i_blocks
);
732 ufs_inode
->ui_flags
= cpu_to_fs32(sb
, ufsi
->i_flags
);
733 ufs_inode
->ui_gen
= cpu_to_fs32(sb
, inode
->i_generation
);
735 if ((UFS_SB(sb
)->s_flags
& UFS_UID_MASK
) == UFS_UID_EFT
) {
736 ufs_inode
->ui_u3
.ui_sun
.ui_shadow
= cpu_to_fs32(sb
, ufsi
->i_shadow
);
737 ufs_inode
->ui_u3
.ui_sun
.ui_oeftflag
= cpu_to_fs32(sb
, ufsi
->i_oeftflag
);
740 if (S_ISCHR(inode
->i_mode
) || S_ISBLK(inode
->i_mode
)) {
741 /* ufs_inode->ui_u2.ui_addr.ui_db[0] = cpu_to_fs32(sb, inode->i_rdev); */
742 ufs_inode
->ui_u2
.ui_addr
.ui_db
[0] = ufsi
->i_u1
.i_data
[0];
743 } else if (inode
->i_blocks
) {
744 memcpy(&ufs_inode
->ui_u2
.ui_addr
, ufsi
->i_u1
.i_data
,
745 sizeof(ufs_inode
->ui_u2
.ui_addr
));
748 memcpy(&ufs_inode
->ui_u2
.ui_symlink
, ufsi
->i_u1
.i_symlink
,
749 sizeof(ufs_inode
->ui_u2
.ui_symlink
));
753 memset (ufs_inode
, 0, sizeof(struct ufs_inode
));
756 static void ufs2_update_inode(struct inode
*inode
, struct ufs2_inode
*ufs_inode
)
758 struct super_block
*sb
= inode
->i_sb
;
759 struct ufs_inode_info
*ufsi
= UFS_I(inode
);
762 ufs_inode
->ui_mode
= cpu_to_fs16(sb
, inode
->i_mode
);
763 ufs_inode
->ui_nlink
= cpu_to_fs16(sb
, inode
->i_nlink
);
765 ufs_inode
->ui_uid
= cpu_to_fs32(sb
, i_uid_read(inode
));
766 ufs_inode
->ui_gid
= cpu_to_fs32(sb
, i_gid_read(inode
));
768 ufs_inode
->ui_size
= cpu_to_fs64(sb
, inode
->i_size
);
769 ufs_inode
->ui_atime
= cpu_to_fs64(sb
, inode
->i_atime
.tv_sec
);
770 ufs_inode
->ui_atimensec
= cpu_to_fs32(sb
, inode
->i_atime
.tv_nsec
);
771 ufs_inode
->ui_ctime
= cpu_to_fs64(sb
, inode
->i_ctime
.tv_sec
);
772 ufs_inode
->ui_ctimensec
= cpu_to_fs32(sb
, inode
->i_ctime
.tv_nsec
);
773 ufs_inode
->ui_mtime
= cpu_to_fs64(sb
, inode
->i_mtime
.tv_sec
);
774 ufs_inode
->ui_mtimensec
= cpu_to_fs32(sb
, inode
->i_mtime
.tv_nsec
);
776 ufs_inode
->ui_blocks
= cpu_to_fs64(sb
, inode
->i_blocks
);
777 ufs_inode
->ui_flags
= cpu_to_fs32(sb
, ufsi
->i_flags
);
778 ufs_inode
->ui_gen
= cpu_to_fs32(sb
, inode
->i_generation
);
780 if (S_ISCHR(inode
->i_mode
) || S_ISBLK(inode
->i_mode
)) {
781 /* ufs_inode->ui_u2.ui_addr.ui_db[0] = cpu_to_fs32(sb, inode->i_rdev); */
782 ufs_inode
->ui_u2
.ui_addr
.ui_db
[0] = ufsi
->i_u1
.u2_i_data
[0];
783 } else if (inode
->i_blocks
) {
784 memcpy(&ufs_inode
->ui_u2
.ui_addr
, ufsi
->i_u1
.u2_i_data
,
785 sizeof(ufs_inode
->ui_u2
.ui_addr
));
787 memcpy(&ufs_inode
->ui_u2
.ui_symlink
, ufsi
->i_u1
.i_symlink
,
788 sizeof(ufs_inode
->ui_u2
.ui_symlink
));
792 memset (ufs_inode
, 0, sizeof(struct ufs2_inode
));
796 static int ufs_update_inode(struct inode
* inode
, int do_sync
)
798 struct super_block
*sb
= inode
->i_sb
;
799 struct ufs_sb_private_info
*uspi
= UFS_SB(sb
)->s_uspi
;
800 struct buffer_head
* bh
;
802 UFSD("ENTER, ino %lu\n", inode
->i_ino
);
804 if (inode
->i_ino
< UFS_ROOTINO
||
805 inode
->i_ino
> (uspi
->s_ncg
* uspi
->s_ipg
)) {
806 ufs_warning (sb
, "ufs_read_inode", "bad inode number (%lu)\n", inode
->i_ino
);
810 bh
= sb_bread(sb
, ufs_inotofsba(inode
->i_ino
));
812 ufs_warning (sb
, "ufs_read_inode", "unable to read inode %lu\n", inode
->i_ino
);
815 if (uspi
->fs_magic
== UFS2_MAGIC
) {
816 struct ufs2_inode
*ufs2_inode
= (struct ufs2_inode
*)bh
->b_data
;
818 ufs2_update_inode(inode
,
819 ufs2_inode
+ ufs_inotofsbo(inode
->i_ino
));
821 struct ufs_inode
*ufs_inode
= (struct ufs_inode
*) bh
->b_data
;
823 ufs1_update_inode(inode
, ufs_inode
+ ufs_inotofsbo(inode
->i_ino
));
826 mark_buffer_dirty(bh
);
828 sync_dirty_buffer(bh
);
835 int ufs_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
837 return ufs_update_inode(inode
, wbc
->sync_mode
== WB_SYNC_ALL
);
840 int ufs_sync_inode (struct inode
*inode
)
842 return ufs_update_inode (inode
, 1);
845 void ufs_evict_inode(struct inode
* inode
)
849 if (!inode
->i_nlink
&& !is_bad_inode(inode
))
852 truncate_inode_pages_final(&inode
->i_data
);
855 if (inode
->i_blocks
&&
856 (S_ISREG(inode
->i_mode
) || S_ISDIR(inode
->i_mode
) ||
857 S_ISLNK(inode
->i_mode
)))
858 ufs_truncate_blocks(inode
);
859 ufs_update_inode(inode
, inode_needs_sync(inode
));
862 invalidate_inode_buffers(inode
);
866 ufs_free_inode(inode
);
875 static inline void free_data(struct to_free
*ctx
, u64 from
, unsigned count
)
877 if (ctx
->count
&& ctx
->to
!= from
) {
878 ufs_free_blocks(ctx
->inode
, ctx
->to
- ctx
->count
, ctx
->count
);
882 ctx
->to
= from
+ count
;
885 #define DIRECT_FRAGMENT ((inode->i_size + uspi->s_fsize - 1) >> uspi->s_fshift)
887 static void ufs_trunc_direct(struct inode
*inode
)
889 struct ufs_inode_info
*ufsi
= UFS_I(inode
);
890 struct super_block
* sb
;
891 struct ufs_sb_private_info
* uspi
;
893 u64 frag1
, frag2
, frag3
, frag4
, block1
, block2
;
894 struct to_free ctx
= {.inode
= inode
};
897 UFSD("ENTER: ino %lu\n", inode
->i_ino
);
900 uspi
= UFS_SB(sb
)->s_uspi
;
902 frag1
= DIRECT_FRAGMENT
;
903 frag4
= min_t(u64
, UFS_NDIR_FRAGMENT
, ufsi
->i_lastfrag
);
904 frag2
= ((frag1
& uspi
->s_fpbmask
) ? ((frag1
| uspi
->s_fpbmask
) + 1) : frag1
);
905 frag3
= frag4
& ~uspi
->s_fpbmask
;
910 } else if (frag2
< frag3
) {
911 block1
= ufs_fragstoblks (frag2
);
912 block2
= ufs_fragstoblks (frag3
);
915 UFSD("ino %lu, frag1 %llu, frag2 %llu, block1 %llu, block2 %llu,"
916 " frag3 %llu, frag4 %llu\n", inode
->i_ino
,
917 (unsigned long long)frag1
, (unsigned long long)frag2
,
918 (unsigned long long)block1
, (unsigned long long)block2
,
919 (unsigned long long)frag3
, (unsigned long long)frag4
);
925 * Free first free fragments
927 p
= ufs_get_direct_data_ptr(uspi
, ufsi
, ufs_fragstoblks(frag1
));
928 tmp
= ufs_data_ptr_to_cpu(sb
, p
);
930 ufs_panic (sb
, "ufs_trunc_direct", "internal error");
932 frag1
= ufs_fragnum (frag1
);
934 ufs_free_fragments(inode
, tmp
+ frag1
, frag2
);
940 for (i
= block1
; i
< block2
; i
++) {
941 p
= ufs_get_direct_data_ptr(uspi
, ufsi
, i
);
942 tmp
= ufs_data_ptr_to_cpu(sb
, p
);
945 write_seqlock(&ufsi
->meta_lock
);
946 ufs_data_ptr_clear(uspi
, p
);
947 write_sequnlock(&ufsi
->meta_lock
);
949 free_data(&ctx
, tmp
, uspi
->s_fpb
);
952 free_data(&ctx
, 0, 0);
958 * Free last free fragments
960 p
= ufs_get_direct_data_ptr(uspi
, ufsi
, ufs_fragstoblks(frag3
));
961 tmp
= ufs_data_ptr_to_cpu(sb
, p
);
963 ufs_panic(sb
, "ufs_truncate_direct", "internal error");
964 frag4
= ufs_fragnum (frag4
);
965 write_seqlock(&ufsi
->meta_lock
);
966 ufs_data_ptr_clear(uspi
, p
);
967 write_sequnlock(&ufsi
->meta_lock
);
969 ufs_free_fragments (inode
, tmp
, frag4
);
972 UFSD("EXIT: ino %lu\n", inode
->i_ino
);
975 static void free_full_branch(struct inode
*inode
, u64 ind_block
, int depth
)
977 struct super_block
*sb
= inode
->i_sb
;
978 struct ufs_sb_private_info
*uspi
= UFS_SB(sb
)->s_uspi
;
979 struct ufs_buffer_head
*ubh
= ubh_bread(sb
, ind_block
, uspi
->s_bsize
);
986 for (i
= 0; i
< uspi
->s_apb
; i
++) {
987 void *p
= ubh_get_data_ptr(uspi
, ubh
, i
);
988 u64 block
= ufs_data_ptr_to_cpu(sb
, p
);
990 free_full_branch(inode
, block
, depth
);
993 struct to_free ctx
= {.inode
= inode
};
995 for (i
= 0; i
< uspi
->s_apb
; i
++) {
996 void *p
= ubh_get_data_ptr(uspi
, ubh
, i
);
997 u64 block
= ufs_data_ptr_to_cpu(sb
, p
);
999 free_data(&ctx
, block
, uspi
->s_fpb
);
1001 free_data(&ctx
, 0, 0);
1005 ufs_free_blocks(inode
, ind_block
, uspi
->s_fpb
);
1008 static void free_branch_tail(struct inode
*inode
, unsigned from
, struct ufs_buffer_head
*ubh
, int depth
)
1010 struct super_block
*sb
= inode
->i_sb
;
1011 struct ufs_sb_private_info
*uspi
= UFS_SB(sb
)->s_uspi
;
1015 for (i
= from
; i
< uspi
->s_apb
; i
++) {
1016 void *p
= ubh_get_data_ptr(uspi
, ubh
, i
);
1017 u64 block
= ufs_data_ptr_to_cpu(sb
, p
);
1019 write_seqlock(&UFS_I(inode
)->meta_lock
);
1020 ufs_data_ptr_clear(uspi
, p
);
1021 write_sequnlock(&UFS_I(inode
)->meta_lock
);
1022 ubh_mark_buffer_dirty(ubh
);
1023 free_full_branch(inode
, block
, depth
);
1027 struct to_free ctx
= {.inode
= inode
};
1029 for (i
= from
; i
< uspi
->s_apb
; i
++) {
1030 void *p
= ubh_get_data_ptr(uspi
, ubh
, i
);
1031 u64 block
= ufs_data_ptr_to_cpu(sb
, p
);
1033 write_seqlock(&UFS_I(inode
)->meta_lock
);
1034 ufs_data_ptr_clear(uspi
, p
);
1035 write_sequnlock(&UFS_I(inode
)->meta_lock
);
1036 ubh_mark_buffer_dirty(ubh
);
1037 free_data(&ctx
, block
, uspi
->s_fpb
);
1040 free_data(&ctx
, 0, 0);
1042 if (IS_SYNC(inode
) && ubh_buffer_dirty(ubh
))
1043 ubh_sync_block(ubh
);
1047 static int ufs_alloc_lastblock(struct inode
*inode
, loff_t size
)
1050 struct super_block
*sb
= inode
->i_sb
;
1051 struct address_space
*mapping
= inode
->i_mapping
;
1052 struct ufs_sb_private_info
*uspi
= UFS_SB(sb
)->s_uspi
;
1055 struct page
*lastpage
;
1056 struct buffer_head
*bh
;
1059 lastfrag
= (size
+ uspi
->s_fsize
- 1) >> uspi
->s_fshift
;
1066 lastpage
= ufs_get_locked_page(mapping
, lastfrag
>>
1067 (PAGE_SHIFT
- inode
->i_blkbits
));
1068 if (IS_ERR(lastpage
)) {
1073 end
= lastfrag
& ((1 << (PAGE_SHIFT
- inode
->i_blkbits
)) - 1);
1074 bh
= page_buffers(lastpage
);
1075 for (i
= 0; i
< end
; ++i
)
1076 bh
= bh
->b_this_page
;
1079 err
= ufs_getfrag_block(inode
, lastfrag
, bh
, 1);
1084 if (buffer_new(bh
)) {
1085 clear_buffer_new(bh
);
1086 clean_bdev_bh_alias(bh
);
1088 * we do not zeroize fragment, because of
1089 * if it maped to hole, it already contains zeroes
1091 set_buffer_uptodate(bh
);
1092 mark_buffer_dirty(bh
);
1093 set_page_dirty(lastpage
);
1096 if (lastfrag
>= UFS_IND_FRAGMENT
) {
1097 end
= uspi
->s_fpb
- ufs_fragnum(lastfrag
) - 1;
1098 phys64
= bh
->b_blocknr
+ 1;
1099 for (i
= 0; i
< end
; ++i
) {
1100 bh
= sb_getblk(sb
, i
+ phys64
);
1102 memset(bh
->b_data
, 0, sb
->s_blocksize
);
1103 set_buffer_uptodate(bh
);
1104 mark_buffer_dirty(bh
);
1106 sync_dirty_buffer(bh
);
1111 ufs_put_locked_page(lastpage
);
1116 static void ufs_truncate_blocks(struct inode
*inode
)
1118 struct ufs_inode_info
*ufsi
= UFS_I(inode
);
1119 struct super_block
*sb
= inode
->i_sb
;
1120 struct ufs_sb_private_info
*uspi
= UFS_SB(sb
)->s_uspi
;
1121 unsigned offsets
[4];
1125 struct ufs_buffer_head
*ubh
[3];
1129 if (inode
->i_size
) {
1130 sector_t last
= (inode
->i_size
- 1) >> uspi
->s_bshift
;
1131 depth
= ufs_block_to_path(inode
, last
, offsets
);
1138 for (depth2
= depth
- 1; depth2
; depth2
--)
1139 if (offsets
[depth2
] != uspi
->s_apb
- 1)
1142 mutex_lock(&ufsi
->truncate_mutex
);
1144 ufs_trunc_direct(inode
);
1145 offsets
[0] = UFS_IND_BLOCK
;
1147 /* get the blocks that should be partially emptied */
1148 p
= ufs_get_direct_data_ptr(uspi
, ufsi
, offsets
[0]++);
1149 for (i
= 0; i
< depth2
; i
++) {
1150 block
= ufs_data_ptr_to_cpu(sb
, p
);
1153 ubh
[i
] = ubh_bread(sb
, block
, uspi
->s_bsize
);
1155 write_seqlock(&ufsi
->meta_lock
);
1156 ufs_data_ptr_clear(uspi
, p
);
1157 write_sequnlock(&ufsi
->meta_lock
);
1160 p
= ubh_get_data_ptr(uspi
, ubh
[i
], offsets
[i
+ 1]++);
1163 free_branch_tail(inode
, offsets
[i
+ 1], ubh
[i
], depth
- i
- 1);
1165 for (i
= offsets
[0]; i
<= UFS_TIND_BLOCK
; i
++) {
1166 p
= ufs_get_direct_data_ptr(uspi
, ufsi
, i
);
1167 block
= ufs_data_ptr_to_cpu(sb
, p
);
1169 write_seqlock(&ufsi
->meta_lock
);
1170 ufs_data_ptr_clear(uspi
, p
);
1171 write_sequnlock(&ufsi
->meta_lock
);
1172 free_full_branch(inode
, block
, i
- UFS_IND_BLOCK
+ 1);
1175 read_seqlock_excl(&ufsi
->meta_lock
);
1176 ufsi
->i_lastfrag
= DIRECT_FRAGMENT
;
1177 read_sequnlock_excl(&ufsi
->meta_lock
);
1178 mark_inode_dirty(inode
);
1179 mutex_unlock(&ufsi
->truncate_mutex
);
1182 static int ufs_truncate(struct inode
*inode
, loff_t size
)
1186 UFSD("ENTER: ino %lu, i_size: %llu, old_i_size: %llu\n",
1187 inode
->i_ino
, (unsigned long long)size
,
1188 (unsigned long long)i_size_read(inode
));
1190 if (!(S_ISREG(inode
->i_mode
) || S_ISDIR(inode
->i_mode
) ||
1191 S_ISLNK(inode
->i_mode
)))
1193 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
1196 err
= ufs_alloc_lastblock(inode
, size
);
1201 block_truncate_page(inode
->i_mapping
, size
, ufs_getfrag_block
);
1203 truncate_setsize(inode
, size
);
1205 ufs_truncate_blocks(inode
);
1206 inode
->i_mtime
= inode
->i_ctime
= current_time(inode
);
1207 mark_inode_dirty(inode
);
1209 UFSD("EXIT: err %d\n", err
);
1213 int ufs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
1215 struct inode
*inode
= d_inode(dentry
);
1216 unsigned int ia_valid
= attr
->ia_valid
;
1219 error
= setattr_prepare(dentry
, attr
);
1223 if (ia_valid
& ATTR_SIZE
&& attr
->ia_size
!= inode
->i_size
) {
1224 error
= ufs_truncate(inode
, attr
->ia_size
);
1229 setattr_copy(inode
, attr
);
1230 mark_inode_dirty(inode
);
1234 const struct inode_operations ufs_file_inode_operations
= {
1235 .setattr
= ufs_setattr
,