xfs: fix type usage
[linux/fpc-iii.git] / fs / ufs / inode.c
blobafb601c0dda0b1c18e9b4e2947749be451d55bf0
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * linux/fs/ufs/inode.c
5 * Copyright (C) 1998
6 * Daniel Pirkl <daniel.pirkl@email.cz>
7 * Charles University, Faculty of Mathematics and Physics
9 * from
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)
18 * from
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>
32 #include <linux/fs.h>
33 #include <linux/time.h>
34 #include <linux/stat.h>
35 #include <linux/string.h>
36 #include <linux/mm.h>
37 #include <linux/buffer_head.h>
38 #include <linux/writeback.h>
40 #include "ufs_fs.h"
41 #include "ufs.h"
42 #include "swab.h"
43 #include "util.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));
53 int n = 0;
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);
71 } else {
72 ufs_warning(inode->i_sb, "ufs_block_to_path", "block > big");
74 return n;
77 typedef struct {
78 void *p;
79 union {
80 __fs32 key32;
81 __fs64 key64;
83 struct buffer_head *bh;
84 } Indirect;
86 static inline int grow_chain32(struct ufs_inode_info *ufsi,
87 struct buffer_head *bh, __fs32 *v,
88 Indirect *from, Indirect *to)
90 Indirect *p;
91 unsigned seq;
92 to->bh = bh;
93 do {
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));
99 return (p > to);
102 static inline int grow_chain64(struct ufs_inode_info *ufsi,
103 struct buffer_head *bh, __fs64 *v,
104 Indirect *from, Indirect *to)
106 Indirect *p;
107 unsigned seq;
108 to->bh = bh;
109 do {
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));
115 return (p > to);
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;
131 unsigned *p;
132 unsigned flags = UFS_SB(sb)->s_flags;
133 u64 res = 0;
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);
139 if (depth == 0)
140 goto no_block;
142 again:
143 p = offsets;
145 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
146 goto ufs2;
148 if (!grow_chain32(ufsi, NULL, &ufsi->i_u1.i_data[*p++], chain, q))
149 goto changed;
150 if (!q->key32)
151 goto no_block;
152 while (--depth) {
153 __fs32 *ptr;
154 struct buffer_head *bh;
155 unsigned n = *p++;
157 bh = sb_bread(sb, uspi->s_sbbase +
158 fs32_to_cpu(sb, q->key32) + (n>>shift));
159 if (!bh)
160 goto no_block;
161 ptr = (__fs32 *)bh->b_data + (n & mask);
162 if (!grow_chain32(ufsi, bh, ptr, chain, ++q))
163 goto changed;
164 if (!q->key32)
165 goto no_block;
167 res = fs32_to_cpu(sb, q->key32);
168 goto found;
170 ufs2:
171 if (!grow_chain64(ufsi, NULL, &ufsi->i_u1.u2_i_data[*p++], chain, q))
172 goto changed;
173 if (!q->key64)
174 goto no_block;
176 while (--depth) {
177 __fs64 *ptr;
178 struct buffer_head *bh;
179 unsigned n = *p++;
181 bh = sb_bread(sb, uspi->s_sbbase +
182 fs64_to_cpu(sb, q->key64) + (n>>shift));
183 if (!bh)
184 goto no_block;
185 ptr = (__fs64 *)bh->b_data + (n & mask);
186 if (!grow_chain64(ufsi, bh, ptr, chain, ++q))
187 goto changed;
188 if (!q->key64)
189 goto no_block;
191 res = fs64_to_cpu(sb, q->key64);
192 found:
193 res += uspi->s_sbbase;
194 no_block:
195 while (q > chain) {
196 brelse(q->bh);
197 q--;
199 return res;
201 changed:
202 while (q > chain) {
203 brelse(q->bh);
204 q--;
206 goto again;
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
217 * ones.
219 static bool
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);
228 unsigned new_size;
229 void *p;
230 u64 tmp;
232 if (writes_to < (lastfrag | uspi->s_fpbmask))
233 new_size = (writes_to & uspi->s_fpbmask) + 1;
234 else
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,
240 locked_page);
241 return tmp != 0;
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()
253 static u64
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;
263 void *p;
265 /* TODO : to be done for write support
266 if ( (flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
267 goto ufs2;
270 p = ufs_get_direct_data_ptr(uspi, ufsi, index);
271 tmp = ufs_data_ptr_to_cpu(sb, p);
272 if (tmp)
273 goto out;
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;
281 goal = 0;
282 if (index) {
283 goal = ufs_data_ptr_to_cpu(sb,
284 ufs_get_direct_data_ptr(uspi, ufsi, index - 1));
285 if (goal)
286 goal += uspi->s_fpb;
288 tmp = ufs_new_fragments(inode, p, ufs_blknum(new_fragment),
289 goal, nfrags, err, locked_page);
291 if (!tmp) {
292 *err = -ENOSPC;
293 return 0;
296 if (new)
297 *new = 1;
298 inode->i_ctime = current_time(inode);
299 if (IS_SYNC(inode))
300 ufs_sync_inode (inode);
301 mark_inode_dirty(inode);
302 out:
303 return tmp + uspi->s_sbbase;
305 /* This part : To be implemented ....
306 Required only for writing, not required for READ-ONLY.
307 ufs2:
309 u2_block = ufs_fragstoblks(fragment);
310 u2_blockoff = ufs_fragnum(fragment);
311 p = ufsi->i_u1.u2_i_data + block;
312 goal = 0;
314 repeat2:
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()
332 static u64
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;
340 u64 tmp = 0, goal;
341 struct buffer_head *bh;
342 void *p;
344 if (!ind_block)
345 return 0;
347 bh = sb_bread(sb, ind_block + (index >> shift));
348 if (unlikely(!bh)) {
349 *err = -EIO;
350 return 0;
353 index &= uspi->s_apbmask >> uspi->s_fpbshift;
354 if (uspi->fs_magic == UFS2_MAGIC)
355 p = (__fs64 *)bh->b_data + index;
356 else
357 p = (__fs32 *)bh->b_data + index;
359 tmp = ufs_data_ptr_to_cpu(sb, p);
360 if (tmp)
361 goto out;
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;
367 else
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);
371 if (!tmp)
372 goto out;
374 if (new)
375 *new = 1;
377 mark_buffer_dirty(bh);
378 if (IS_SYNC(inode))
379 sync_dirty_buffer(bh);
380 inode->i_ctime = current_time(inode);
381 mark_inode_dirty(inode);
382 out:
383 brelse (bh);
384 UFSD("EXIT\n");
385 if (tmp)
386 tmp += uspi->s_sbbase;
387 return tmp;
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;
400 unsigned offsets[4];
401 int depth = ufs_block_to_path(inode, fragment >> uspi->s_fpbshift, offsets);
402 u64 phys64 = 0;
403 unsigned frag = fragment & uspi->s_fpbmask;
405 phys64 = ufs_frag_map(inode, offsets, depth);
406 if (!create)
407 goto done;
409 if (phys64) {
410 if (fragment >= UFS_NDIR_FRAGMENT)
411 goto done;
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);
415 goto done;
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");
426 err = -EIO;
427 goto out;
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))
436 goto out;
440 if (depth == 1) {
441 phys64 = ufs_inode_getfrag(inode, offsets[0], fragment,
442 &err, &new, bh_result->b_page);
443 } else {
444 int i;
445 phys64 = ufs_inode_getfrag(inode, offsets[0], fragment,
446 &err, NULL, NULL);
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);
453 out:
454 if (phys64) {
455 phys64 += frag;
456 map_bh(bh_result, sb, phys64);
457 if (new)
458 set_buffer_new(bh_result);
460 mutex_unlock(&UFS_I(inode)->truncate_mutex);
461 return err;
463 done:
464 if (phys64)
465 map_bh(bh_result, sb, phys64 + frag);
466 return 0;
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)
500 int ret;
502 ret = block_write_begin(mapping, pos, len, flags, pagep,
503 ufs_getfrag_block);
504 if (unlikely(ret))
505 ufs_write_failed(mapping, pos + len);
507 return ret;
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)
514 int ret;
516 ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
517 if (ret < len)
518 ufs_write_failed(mapping, pos + len);
519 return ret;
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,
532 .bmap = ufs_bmap
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;
549 } else {
550 inode->i_mapping->a_ops = &ufs_aops;
551 inode->i_op = &page_symlink_inode_operations;
552 inode_nohighmem(inode);
554 } else
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;
563 umode_t mode;
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)
571 return -ESTALE;
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));
596 } else {
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;
601 return 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;
608 umode_t mode;
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)
617 return -ESTALE;
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));
643 } else {
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;
648 return 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;
656 struct inode *inode;
657 int err = -EIO;
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",
663 ino);
664 return ERR_PTR(-EIO);
667 inode = iget_locked(sb, ino);
668 if (!inode)
669 return ERR_PTR(-ENOMEM);
670 if (!(inode->i_state & I_NEW))
671 return inode;
673 ufsi = UFS_I(inode);
675 bh = sb_bread(sb, uspi->s_sbbase + ufs_inotofsba(inode->i_ino));
676 if (!bh) {
677 ufs_warning(sb, "ufs_read_inode", "unable to read inode %lu\n",
678 inode->i_ino);
679 goto bad_inode;
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));
686 } else {
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));
692 brelse(bh);
693 if (err)
694 goto bad_inode;
696 inode->i_version++;
697 ufsi->i_lastfrag =
698 (inode->i_size + uspi->s_fsize - 1) >> uspi->s_fshift;
699 ufsi->i_dir_start_lookup = 0;
700 ufsi->i_osync = 0;
702 ufs_set_inode_ops(inode);
704 UFSD("EXIT\n");
705 unlock_new_inode(inode);
706 return inode;
708 bad_inode:
709 iget_failed(inode);
710 return ERR_PTR(err);
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));
747 else {
748 memcpy(&ufs_inode->ui_u2.ui_symlink, ufsi->i_u1.i_symlink,
749 sizeof(ufs_inode->ui_u2.ui_symlink));
752 if (!inode->i_nlink)
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);
761 UFSD("ENTER\n");
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));
786 } else {
787 memcpy(&ufs_inode->ui_u2.ui_symlink, ufsi->i_u1.i_symlink,
788 sizeof(ufs_inode->ui_u2.ui_symlink));
791 if (!inode->i_nlink)
792 memset (ufs_inode, 0, sizeof(struct ufs2_inode));
793 UFSD("EXIT\n");
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);
807 return -1;
810 bh = sb_bread(sb, ufs_inotofsba(inode->i_ino));
811 if (!bh) {
812 ufs_warning (sb, "ufs_read_inode", "unable to read inode %lu\n", inode->i_ino);
813 return -1;
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));
820 } else {
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);
827 if (do_sync)
828 sync_dirty_buffer(bh);
829 brelse (bh);
831 UFSD("EXIT\n");
832 return 0;
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)
847 int want_delete = 0;
849 if (!inode->i_nlink && !is_bad_inode(inode))
850 want_delete = 1;
852 truncate_inode_pages_final(&inode->i_data);
853 if (want_delete) {
854 inode->i_size = 0;
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);
863 clear_inode(inode);
865 if (want_delete)
866 ufs_free_inode(inode);
869 struct to_free {
870 struct inode *inode;
871 u64 to;
872 unsigned count;
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);
879 ctx->count = 0;
881 ctx->count += 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;
892 void *p;
893 u64 frag1, frag2, frag3, frag4, block1, block2;
894 struct to_free ctx = {.inode = inode};
895 unsigned i, tmp;
897 UFSD("ENTER: ino %lu\n", inode->i_ino);
899 sb = inode->i_sb;
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;
906 block1 = block2 = 0;
907 if (frag2 > frag3) {
908 frag2 = frag4;
909 frag3 = frag4 = 0;
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);
921 if (frag1 >= frag2)
922 goto next1;
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);
929 if (!tmp )
930 ufs_panic (sb, "ufs_trunc_direct", "internal error");
931 frag2 -= frag1;
932 frag1 = ufs_fragnum (frag1);
934 ufs_free_fragments(inode, tmp + frag1, frag2);
936 next1:
938 * Free whole blocks
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);
943 if (!tmp)
944 continue;
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);
954 if (frag3 >= frag4)
955 goto next3;
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);
962 if (!tmp )
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);
970 next3:
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);
980 unsigned i;
982 if (!ubh)
983 return;
985 if (--depth) {
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);
989 if (block)
990 free_full_branch(inode, block, depth);
992 } else {
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);
998 if (block)
999 free_data(&ctx, block, uspi->s_fpb);
1001 free_data(&ctx, 0, 0);
1004 ubh_bforget(ubh);
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;
1012 unsigned i;
1014 if (--depth) {
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);
1018 if (block) {
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);
1026 } else {
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);
1032 if (block) {
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);
1044 ubh_brelse(ubh);
1047 static int ufs_alloc_lastblock(struct inode *inode, loff_t size)
1049 int err = 0;
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;
1053 unsigned i, end;
1054 sector_t lastfrag;
1055 struct page *lastpage;
1056 struct buffer_head *bh;
1057 u64 phys64;
1059 lastfrag = (size + uspi->s_fsize - 1) >> uspi->s_fshift;
1061 if (!lastfrag)
1062 goto out;
1064 lastfrag--;
1066 lastpage = ufs_get_locked_page(mapping, lastfrag >>
1067 (PAGE_SHIFT - inode->i_blkbits));
1068 if (IS_ERR(lastpage)) {
1069 err = -EIO;
1070 goto out;
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);
1081 if (unlikely(err))
1082 goto out_unlock;
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);
1101 lock_buffer(bh);
1102 memset(bh->b_data, 0, sb->s_blocksize);
1103 set_buffer_uptodate(bh);
1104 mark_buffer_dirty(bh);
1105 unlock_buffer(bh);
1106 sync_dirty_buffer(bh);
1107 brelse(bh);
1110 out_unlock:
1111 ufs_put_locked_page(lastpage);
1112 out:
1113 return err;
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];
1122 int depth;
1123 int depth2;
1124 unsigned i;
1125 struct ufs_buffer_head *ubh[3];
1126 void *p;
1127 u64 block;
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);
1132 if (!depth)
1133 return;
1134 } else {
1135 depth = 1;
1138 for (depth2 = depth - 1; depth2; depth2--)
1139 if (offsets[depth2] != uspi->s_apb - 1)
1140 break;
1142 mutex_lock(&ufsi->truncate_mutex);
1143 if (depth == 1) {
1144 ufs_trunc_direct(inode);
1145 offsets[0] = UFS_IND_BLOCK;
1146 } else {
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);
1151 if (!block)
1152 break;
1153 ubh[i] = ubh_bread(sb, block, uspi->s_bsize);
1154 if (!ubh[i]) {
1155 write_seqlock(&ufsi->meta_lock);
1156 ufs_data_ptr_clear(uspi, p);
1157 write_sequnlock(&ufsi->meta_lock);
1158 break;
1160 p = ubh_get_data_ptr(uspi, ubh[i], offsets[i + 1]++);
1162 while (i--)
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);
1168 if (block) {
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)
1184 int err = 0;
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)))
1192 return -EINVAL;
1193 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1194 return -EPERM;
1196 err = ufs_alloc_lastblock(inode, size);
1198 if (err)
1199 goto out;
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);
1208 out:
1209 UFSD("EXIT: err %d\n", err);
1210 return 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;
1217 int error;
1219 error = setattr_prepare(dentry, attr);
1220 if (error)
1221 return error;
1223 if (ia_valid & ATTR_SIZE && attr->ia_size != inode->i_size) {
1224 error = ufs_truncate(inode, attr->ia_size);
1225 if (error)
1226 return error;
1229 setattr_copy(inode, attr);
1230 mark_inode_dirty(inode);
1231 return 0;
1234 const struct inode_operations ufs_file_inode_operations = {
1235 .setattr = ufs_setattr,