xref: /openbmc/linux/fs/ufs/inode.c (revision d3964221)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  linux/fs/ufs/inode.c
4  *
5  * Copyright (C) 1998
6  * Daniel Pirkl <daniel.pirkl@email.cz>
7  * Charles University, Faculty of Mathematics and Physics
8  *
9  *  from
10  *
11  *  linux/fs/ext2/inode.c
12  *
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)
17  *
18  *  from
19  *
20  *  linux/fs/minix/inode.c
21  *
22  *  Copyright (C) 1991, 1992  Linus Torvalds
23  *
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
27  */
28 
29 #include <linux/uaccess.h>
30 
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>
39 
40 #include "ufs_fs.h"
41 #include "ufs.h"
42 #include "swab.h"
43 #include "util.h"
44 
45 static int ufs_block_to_path(struct inode *inode, sector_t i_block, unsigned offsets[4])
46 {
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;
54 
55 
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");
73 	}
74 	return n;
75 }
76 
77 typedef struct {
78 	void	*p;
79 	union {
80 		__fs32	key32;
81 		__fs64	key64;
82 	};
83 	struct buffer_head *bh;
84 } Indirect;
85 
86 static inline int grow_chain32(struct ufs_inode_info *ufsi,
87 			       struct buffer_head *bh, __fs32 *v,
88 			       Indirect *from, Indirect *to)
89 {
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++)
97 			;
98 	} while (read_seqretry(&ufsi->meta_lock, seq));
99 	return (p > to);
100 }
101 
102 static inline int grow_chain64(struct ufs_inode_info *ufsi,
103 			       struct buffer_head *bh, __fs64 *v,
104 			       Indirect *from, Indirect *to)
105 {
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++)
113 			;
114 	} while (read_seqretry(&ufsi->meta_lock, seq));
115 	return (p > to);
116 }
117 
118 /*
119  * Returns the location of the fragment from
120  * the beginning of the filesystem.
121  */
122 
123 static u64 ufs_frag_map(struct inode *inode, unsigned offsets[4], int depth)
124 {
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;
134 
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);
138 
139 	if (depth == 0)
140 		goto no_block;
141 
142 again:
143 	p = offsets;
144 
145 	if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
146 		goto ufs2;
147 
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++;
156 
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;
166 	}
167 	res = fs32_to_cpu(sb, q->key32);
168 	goto found;
169 
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;
175 
176 	while (--depth) {
177 		__fs64 *ptr;
178 		struct buffer_head *bh;
179 		unsigned n = *p++;
180 
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;
190 	}
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--;
198 	}
199 	return res;
200 
201 changed:
202 	while (q > chain) {
203 		brelse(q->bh);
204 		q--;
205 	}
206 	goto again;
207 }
208 
209 /*
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.
214  *
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.
218  */
219 static bool
220 ufs_extend_tail(struct inode *inode, u64 writes_to,
221 		  int *err, struct page *locked_page)
222 {
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;
231 
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;
236 
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;
242 }
243 
244 /**
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()
252  */
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)
257 {
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;
264 
265         /* TODO : to be done for write support
266         if ( (flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
267              goto ufs2;
268          */
269 
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;
274 
275 	lastfrag = ufsi->i_lastfrag;
276 
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;
280 
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;
287 	}
288 	tmp = ufs_new_fragments(inode, p, ufs_blknum(new_fragment),
289 				goal, nfrags, err, locked_page);
290 
291 	if (!tmp) {
292 		*err = -ENOSPC;
293 		return 0;
294 	}
295 
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;
304 
305      /* This part : To be implemented ....
306         Required only for writing, not required for READ-ONLY.
307 ufs2:
308 
309 	u2_block = ufs_fragstoblks(fragment);
310 	u2_blockoff = ufs_fragnum(fragment);
311 	p = ufsi->i_u1.u2_i_data + block;
312 	goal = 0;
313 
314 repeat2:
315 	tmp = fs32_to_cpu(sb, *p);
316 	lastfrag = ufsi->i_lastfrag;
317 
318      */
319 }
320 
321 /**
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()
331  */
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)
336 {
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;
343 
344 	if (!ind_block)
345 		return 0;
346 
347 	bh = sb_bread(sb, ind_block + (index >> shift));
348 	if (unlikely(!bh)) {
349 		*err = -EIO;
350 		return 0;
351 	}
352 
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;
358 
359 	tmp = ufs_data_ptr_to_cpu(sb, p);
360 	if (tmp)
361 		goto out;
362 
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;
373 
374 	if (new)
375 		*new = 1;
376 
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;
388 }
389 
390 /**
391  * ufs_getfrag_block() - `get_block_t' function, interface between UFS and
392  * readpage, writepage and so on
393  */
394 
395 static int ufs_getfrag_block(struct inode *inode, sector_t fragment, struct buffer_head *bh_result, int create)
396 {
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;
404 
405 	phys64 = ufs_frag_map(inode, offsets, depth);
406 	if (!create)
407 		goto done;
408 
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;
416 		}
417 		read_sequnlock_excl(&UFS_I(inode)->meta_lock);
418 	}
419         /* This code entered only while writing ....? */
420 
421 	mutex_lock(&UFS_I(inode)->truncate_mutex);
422 
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;
428 	}
429 
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;
437 		}
438 	}
439 
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);
452 	}
453 out:
454 	if (phys64) {
455 		phys64 += frag;
456 		map_bh(bh_result, sb, phys64);
457 		if (new)
458 			set_buffer_new(bh_result);
459 	}
460 	mutex_unlock(&UFS_I(inode)->truncate_mutex);
461 	return err;
462 
463 done:
464 	if (phys64)
465 		map_bh(bh_result, sb, phys64 + frag);
466 	return 0;
467 }
468 
469 static int ufs_writepage(struct page *page, struct writeback_control *wbc)
470 {
471 	return block_write_full_page(page,ufs_getfrag_block,wbc);
472 }
473 
474 static int ufs_readpage(struct file *file, struct page *page)
475 {
476 	return block_read_full_page(page,ufs_getfrag_block);
477 }
478 
479 int ufs_prepare_chunk(struct page *page, loff_t pos, unsigned len)
480 {
481 	return __block_write_begin(page, pos, len, ufs_getfrag_block);
482 }
483 
484 static void ufs_truncate_blocks(struct inode *);
485 
486 static void ufs_write_failed(struct address_space *mapping, loff_t to)
487 {
488 	struct inode *inode = mapping->host;
489 
490 	if (to > inode->i_size) {
491 		truncate_pagecache(inode, inode->i_size);
492 		ufs_truncate_blocks(inode);
493 	}
494 }
495 
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)
499 {
500 	int ret;
501 
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);
506 
507 	return ret;
508 }
509 
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)
513 {
514 	int ret;
515 
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;
520 }
521 
522 static sector_t ufs_bmap(struct address_space *mapping, sector_t block)
523 {
524 	return generic_block_bmap(mapping,block,ufs_getfrag_block);
525 }
526 
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
533 };
534 
535 static void ufs_set_inode_ops(struct inode *inode)
536 {
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);
553 		}
554 	} else
555 		init_special_inode(inode, inode->i_mode,
556 				   ufs_get_inode_dev(inode->i_sb, UFS_I(inode)));
557 }
558 
559 static int ufs1_read_inode(struct inode *inode, struct ufs_inode *ufs_inode)
560 {
561 	struct ufs_inode_info *ufsi = UFS_I(inode);
562 	struct super_block *sb = inode->i_sb;
563 	umode_t mode;
564 
565 	/*
566 	 * Copy data to the in-core inode.
567 	 */
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;
572 
573 	/*
574 	 * Linux now has 32-bit uid and gid, so we can support EFT.
575 	 */
576 	i_uid_write(inode, ufs_get_inode_uid(sb, ufs_inode));
577 	i_gid_write(inode, ufs_get_inode_gid(sb, ufs_inode));
578 
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);
591 
592 
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;
600 	}
601 	return 0;
602 }
603 
604 static int ufs2_read_inode(struct inode *inode, struct ufs2_inode *ufs2_inode)
605 {
606 	struct ufs_inode_info *ufsi = UFS_I(inode);
607 	struct super_block *sb = inode->i_sb;
608 	umode_t mode;
609 
610 	UFSD("Reading ufs2 inode, ino %lu\n", inode->i_ino);
611 	/*
612 	 * Copy data to the in-core inode.
613 	 */
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;
618 
619         /*
620          * Linux now has 32-bit uid and gid, so we can support EFT.
621          */
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));
624 
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);
635 	/*
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);
638 	*/
639 
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;
647 	}
648 	return 0;
649 }
650 
651 struct inode *ufs_iget(struct super_block *sb, unsigned long ino)
652 {
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;
658 
659 	UFSD("ENTER, ino %lu\n", ino);
660 
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);
665 	}
666 
667 	inode = iget_locked(sb, ino);
668 	if (!inode)
669 		return ERR_PTR(-ENOMEM);
670 	if (!(inode->i_state & I_NEW))
671 		return inode;
672 
673 	ufsi = UFS_I(inode);
674 
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;
680 	}
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;
683 
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;
688 
689 		err = ufs1_read_inode(inode,
690 				      ufs_inode + ufs_inotofsbo(inode->i_ino));
691 	}
692 	brelse(bh);
693 	if (err)
694 		goto bad_inode;
695 
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;
701 
702 	ufs_set_inode_ops(inode);
703 
704 	UFSD("EXIT\n");
705 	unlock_new_inode(inode);
706 	return inode;
707 
708 bad_inode:
709 	iget_failed(inode);
710 	return ERR_PTR(err);
711 }
712 
713 static void ufs1_update_inode(struct inode *inode, struct ufs_inode *ufs_inode)
714 {
715 	struct super_block *sb = inode->i_sb;
716  	struct ufs_inode_info *ufsi = UFS_I(inode);
717 
718 	ufs_inode->ui_mode = cpu_to_fs16(sb, inode->i_mode);
719 	ufs_inode->ui_nlink = cpu_to_fs16(sb, inode->i_nlink);
720 
721 	ufs_set_inode_uid(sb, ufs_inode, i_uid_read(inode));
722 	ufs_set_inode_gid(sb, ufs_inode, i_gid_read(inode));
723 
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);
734 
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);
738 	}
739 
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));
746 	}
747 	else {
748 		memcpy(&ufs_inode->ui_u2.ui_symlink, ufsi->i_u1.i_symlink,
749 		       sizeof(ufs_inode->ui_u2.ui_symlink));
750 	}
751 
752 	if (!inode->i_nlink)
753 		memset (ufs_inode, 0, sizeof(struct ufs_inode));
754 }
755 
756 static void ufs2_update_inode(struct inode *inode, struct ufs2_inode *ufs_inode)
757 {
758 	struct super_block *sb = inode->i_sb;
759  	struct ufs_inode_info *ufsi = UFS_I(inode);
760 
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);
764 
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));
767 
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);
775 
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);
779 
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));
789  	}
790 
791 	if (!inode->i_nlink)
792 		memset (ufs_inode, 0, sizeof(struct ufs2_inode));
793 	UFSD("EXIT\n");
794 }
795 
796 static int ufs_update_inode(struct inode * inode, int do_sync)
797 {
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;
801 
802 	UFSD("ENTER, ino %lu\n", inode->i_ino);
803 
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;
808 	}
809 
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;
814 	}
815 	if (uspi->fs_magic == UFS2_MAGIC) {
816 		struct ufs2_inode *ufs2_inode = (struct ufs2_inode *)bh->b_data;
817 
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;
822 
823 		ufs1_update_inode(inode, ufs_inode + ufs_inotofsbo(inode->i_ino));
824 	}
825 
826 	mark_buffer_dirty(bh);
827 	if (do_sync)
828 		sync_dirty_buffer(bh);
829 	brelse (bh);
830 
831 	UFSD("EXIT\n");
832 	return 0;
833 }
834 
835 int ufs_write_inode(struct inode *inode, struct writeback_control *wbc)
836 {
837 	return ufs_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
838 }
839 
840 int ufs_sync_inode (struct inode *inode)
841 {
842 	return ufs_update_inode (inode, 1);
843 }
844 
845 void ufs_evict_inode(struct inode * inode)
846 {
847 	int want_delete = 0;
848 
849 	if (!inode->i_nlink && !is_bad_inode(inode))
850 		want_delete = 1;
851 
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));
860 	}
861 
862 	invalidate_inode_buffers(inode);
863 	clear_inode(inode);
864 
865 	if (want_delete)
866 		ufs_free_inode(inode);
867 }
868 
869 struct to_free {
870 	struct inode *inode;
871 	u64 to;
872 	unsigned count;
873 };
874 
875 static inline void free_data(struct to_free *ctx, u64 from, unsigned count)
876 {
877 	if (ctx->count && ctx->to != from) {
878 		ufs_free_blocks(ctx->inode, ctx->to - ctx->count, ctx->count);
879 		ctx->count = 0;
880 	}
881 	ctx->count += count;
882 	ctx->to = from + count;
883 }
884 
885 #define DIRECT_FRAGMENT ((inode->i_size + uspi->s_fsize - 1) >> uspi->s_fshift)
886 
887 static void ufs_trunc_direct(struct inode *inode)
888 {
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;
896 
897 	UFSD("ENTER: ino %lu\n", inode->i_ino);
898 
899 	sb = inode->i_sb;
900 	uspi = UFS_SB(sb)->s_uspi;
901 
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);
913 	}
914 
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);
920 
921 	if (frag1 >= frag2)
922 		goto next1;
923 
924 	/*
925 	 * Free first free fragments
926 	 */
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);
933 
934 	ufs_free_fragments(inode, tmp + frag1, frag2);
935 
936 next1:
937 	/*
938 	 * Free whole blocks
939 	 */
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);
948 
949 		free_data(&ctx, tmp, uspi->s_fpb);
950 	}
951 
952 	free_data(&ctx, 0, 0);
953 
954 	if (frag3 >= frag4)
955 		goto next3;
956 
957 	/*
958 	 * Free last free fragments
959 	 */
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);
968 
969 	ufs_free_fragments (inode, tmp, frag4);
970  next3:
971 
972 	UFSD("EXIT: ino %lu\n", inode->i_ino);
973 }
974 
975 static void free_full_branch(struct inode *inode, u64 ind_block, int depth)
976 {
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;
981 
982 	if (!ubh)
983 		return;
984 
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);
991 		}
992 	} else {
993 		struct to_free ctx = {.inode = inode};
994 
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);
1000 		}
1001 		free_data(&ctx, 0, 0);
1002 	}
1003 
1004 	ubh_bforget(ubh);
1005 	ufs_free_blocks(inode, ind_block, uspi->s_fpb);
1006 }
1007 
1008 static void free_branch_tail(struct inode *inode, unsigned from, struct ufs_buffer_head *ubh, int depth)
1009 {
1010 	struct super_block *sb = inode->i_sb;
1011 	struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
1012 	unsigned i;
1013 
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);
1024 			}
1025 		}
1026 	} else {
1027 		struct to_free ctx = {.inode = inode};
1028 
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);
1038 			}
1039 		}
1040 		free_data(&ctx, 0, 0);
1041 	}
1042 	if (IS_SYNC(inode) && ubh_buffer_dirty(ubh))
1043 		ubh_sync_block(ubh);
1044 	ubh_brelse(ubh);
1045 }
1046 
1047 static int ufs_alloc_lastblock(struct inode *inode, loff_t size)
1048 {
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;
1058 
1059 	lastfrag = (size + uspi->s_fsize - 1) >> uspi->s_fshift;
1060 
1061 	if (!lastfrag)
1062 		goto out;
1063 
1064 	lastfrag--;
1065 
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;
1071        }
1072 
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;
1077 
1078 
1079        err = ufs_getfrag_block(inode, lastfrag, bh, 1);
1080 
1081        if (unlikely(err))
1082 	       goto out_unlock;
1083 
1084        if (buffer_new(bh)) {
1085 	       clear_buffer_new(bh);
1086 	       clean_bdev_bh_alias(bh);
1087 	       /*
1088 		* we do not zeroize fragment, because of
1089 		* if it maped to hole, it already contains zeroes
1090 		*/
1091 	       set_buffer_uptodate(bh);
1092 	       mark_buffer_dirty(bh);
1093 	       set_page_dirty(lastpage);
1094        }
1095 
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);
1108 	       }
1109        }
1110 out_unlock:
1111        ufs_put_locked_page(lastpage);
1112 out:
1113        return err;
1114 }
1115 
1116 static void ufs_truncate_blocks(struct inode *inode)
1117 {
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;
1128 
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;
1136 	}
1137 
1138 	for (depth2 = depth - 1; depth2; depth2--)
1139 		if (offsets[depth2] != uspi->s_apb - 1)
1140 			break;
1141 
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;
1159 			}
1160 			p = ubh_get_data_ptr(uspi, ubh[i], offsets[i + 1]++);
1161 		}
1162 		while (i--)
1163 			free_branch_tail(inode, offsets[i + 1], ubh[i], depth - i - 1);
1164 	}
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);
1173 		}
1174 	}
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);
1180 }
1181 
1182 static int ufs_truncate(struct inode *inode, loff_t size)
1183 {
1184 	int err = 0;
1185 
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));
1189 
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;
1195 
1196 	err = ufs_alloc_lastblock(inode, size);
1197 
1198 	if (err)
1199 		goto out;
1200 
1201 	block_truncate_page(inode->i_mapping, size, ufs_getfrag_block);
1202 
1203 	truncate_setsize(inode, size);
1204 
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;
1211 }
1212 
1213 int ufs_setattr(struct dentry *dentry, struct iattr *attr)
1214 {
1215 	struct inode *inode = d_inode(dentry);
1216 	unsigned int ia_valid = attr->ia_valid;
1217 	int error;
1218 
1219 	error = setattr_prepare(dentry, attr);
1220 	if (error)
1221 		return error;
1222 
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;
1227 	}
1228 
1229 	setattr_copy(inode, attr);
1230 	mark_inode_dirty(inode);
1231 	return 0;
1232 }
1233 
1234 const struct inode_operations ufs_file_inode_operations = {
1235 	.setattr = ufs_setattr,
1236 };
1237