Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/ufs/inode.c |
| 3 | * |
| 4 | * Copyright (C) 1998 |
| 5 | * Daniel Pirkl <daniel.pirkl@email.cz> |
| 6 | * Charles University, Faculty of Mathematics and Physics |
| 7 | * |
| 8 | * from |
| 9 | * |
| 10 | * linux/fs/ext2/inode.c |
| 11 | * |
| 12 | * Copyright (C) 1992, 1993, 1994, 1995 |
| 13 | * Remy Card (card@masi.ibp.fr) |
| 14 | * Laboratoire MASI - Institut Blaise Pascal |
| 15 | * Universite Pierre et Marie Curie (Paris VI) |
| 16 | * |
| 17 | * from |
| 18 | * |
| 19 | * linux/fs/minix/inode.c |
| 20 | * |
| 21 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 22 | * |
| 23 | * Goal-directed block allocation by Stephen Tweedie (sct@dcs.ed.ac.uk), 1993 |
| 24 | * Big-endian to little-endian byte-swapping/bitmaps by |
| 25 | * David S. Miller (davem@caip.rutgers.edu), 1995 |
| 26 | */ |
| 27 | |
| 28 | #include <asm/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
| 30 | #include <linux/errno.h> |
| 31 | #include <linux/fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/time.h> |
| 33 | #include <linux/stat.h> |
| 34 | #include <linux/string.h> |
| 35 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/buffer_head.h> |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 37 | #include <linux/writeback.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Mike Frysinger | e542059 | 2008-02-08 04:21:31 -0800 | [diff] [blame] | 39 | #include "ufs_fs.h" |
Christoph Hellwig | bcd6d4e | 2007-10-16 23:26:51 -0700 | [diff] [blame] | 40 | #include "ufs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include "swab.h" |
| 42 | #include "util.h" |
| 43 | |
Al Viro | 4e3911f | 2015-06-04 14:13:14 -0400 | [diff] [blame] | 44 | static int ufs_block_to_path(struct inode *inode, sector_t i_block, unsigned offsets[4]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | { |
| 46 | struct ufs_sb_private_info *uspi = UFS_SB(inode->i_sb)->s_uspi; |
| 47 | int ptrs = uspi->s_apb; |
| 48 | int ptrs_bits = uspi->s_apbshift; |
| 49 | const long direct_blocks = UFS_NDADDR, |
| 50 | indirect_blocks = ptrs, |
| 51 | double_blocks = (1 << (ptrs_bits * 2)); |
| 52 | int n = 0; |
| 53 | |
| 54 | |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 55 | UFSD("ptrs=uspi->s_apb = %d,double_blocks=%ld \n",ptrs,double_blocks); |
Roel Kluin | 37044c8 | 2009-06-17 16:26:28 -0700 | [diff] [blame] | 56 | if (i_block < direct_blocks) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | offsets[n++] = i_block; |
| 58 | } else if ((i_block -= direct_blocks) < indirect_blocks) { |
| 59 | offsets[n++] = UFS_IND_BLOCK; |
| 60 | offsets[n++] = i_block; |
| 61 | } else if ((i_block -= indirect_blocks) < double_blocks) { |
| 62 | offsets[n++] = UFS_DIND_BLOCK; |
| 63 | offsets[n++] = i_block >> ptrs_bits; |
| 64 | offsets[n++] = i_block & (ptrs - 1); |
| 65 | } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) { |
| 66 | offsets[n++] = UFS_TIND_BLOCK; |
| 67 | offsets[n++] = i_block >> (ptrs_bits * 2); |
| 68 | offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1); |
| 69 | offsets[n++] = i_block & (ptrs - 1); |
| 70 | } else { |
| 71 | ufs_warning(inode->i_sb, "ufs_block_to_path", "block > big"); |
| 72 | } |
| 73 | return n; |
| 74 | } |
| 75 | |
Al Viro | 724bb09 | 2015-06-17 12:02:56 -0400 | [diff] [blame] | 76 | typedef struct { |
| 77 | void *p; |
| 78 | union { |
| 79 | __fs32 key32; |
| 80 | __fs64 key64; |
| 81 | }; |
| 82 | struct buffer_head *bh; |
| 83 | } Indirect; |
| 84 | |
| 85 | static inline int grow_chain32(struct ufs_inode_info *ufsi, |
| 86 | struct buffer_head *bh, __fs32 *v, |
| 87 | Indirect *from, Indirect *to) |
| 88 | { |
| 89 | Indirect *p; |
| 90 | unsigned seq; |
| 91 | to->bh = bh; |
| 92 | do { |
| 93 | seq = read_seqbegin(&ufsi->meta_lock); |
| 94 | to->key32 = *(__fs32 *)(to->p = v); |
| 95 | for (p = from; p <= to && p->key32 == *(__fs32 *)p->p; p++) |
| 96 | ; |
| 97 | } while (read_seqretry(&ufsi->meta_lock, seq)); |
| 98 | return (p > to); |
| 99 | } |
| 100 | |
| 101 | static inline int grow_chain64(struct ufs_inode_info *ufsi, |
| 102 | struct buffer_head *bh, __fs64 *v, |
| 103 | Indirect *from, Indirect *to) |
| 104 | { |
| 105 | Indirect *p; |
| 106 | unsigned seq; |
| 107 | to->bh = bh; |
| 108 | do { |
| 109 | seq = read_seqbegin(&ufsi->meta_lock); |
| 110 | to->key64 = *(__fs64 *)(to->p = v); |
| 111 | for (p = from; p <= to && p->key64 == *(__fs64 *)p->p; p++) |
| 112 | ; |
| 113 | } while (read_seqretry(&ufsi->meta_lock, seq)); |
| 114 | return (p > to); |
| 115 | } |
| 116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | /* |
| 118 | * Returns the location of the fragment from |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 119 | * the beginning of the filesystem. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | */ |
| 121 | |
Al Viro | 4b7068c | 2015-06-04 14:27:23 -0400 | [diff] [blame] | 122 | static u64 ufs_frag_map(struct inode *inode, unsigned offsets[4], int depth) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | { |
| 124 | struct ufs_inode_info *ufsi = UFS_I(inode); |
| 125 | struct super_block *sb = inode->i_sb; |
| 126 | struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; |
| 127 | u64 mask = (u64) uspi->s_apbmask>>uspi->s_fpbshift; |
| 128 | int shift = uspi->s_apbshift-uspi->s_fpbshift; |
Al Viro | 724bb09 | 2015-06-17 12:02:56 -0400 | [diff] [blame] | 129 | Indirect chain[4], *q = chain; |
Al Viro | 4b7068c | 2015-06-04 14:27:23 -0400 | [diff] [blame] | 130 | unsigned *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | unsigned flags = UFS_SB(sb)->s_flags; |
Al Viro | 724bb09 | 2015-06-17 12:02:56 -0400 | [diff] [blame] | 132 | u64 res = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
Andrew Morton | 7256d81 | 2006-06-29 02:24:29 -0700 | [diff] [blame] | 134 | UFSD(": uspi->s_fpbshift = %d ,uspi->s_apbmask = %x, mask=%llx\n", |
| 135 | uspi->s_fpbshift, uspi->s_apbmask, |
| 136 | (unsigned long long)mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
| 138 | if (depth == 0) |
Al Viro | 724bb09 | 2015-06-17 12:02:56 -0400 | [diff] [blame] | 139 | goto no_block; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
Al Viro | 724bb09 | 2015-06-17 12:02:56 -0400 | [diff] [blame] | 141 | again: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | p = offsets; |
| 143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) |
| 145 | goto ufs2; |
| 146 | |
Al Viro | 724bb09 | 2015-06-17 12:02:56 -0400 | [diff] [blame] | 147 | if (!grow_chain32(ufsi, NULL, &ufsi->i_u1.i_data[*p++], chain, q)) |
| 148 | goto changed; |
| 149 | if (!q->key32) |
| 150 | goto no_block; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | while (--depth) { |
Al Viro | 724bb09 | 2015-06-17 12:02:56 -0400 | [diff] [blame] | 152 | __fs32 *ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | struct buffer_head *bh; |
Al Viro | 4e3911f | 2015-06-04 14:13:14 -0400 | [diff] [blame] | 154 | unsigned n = *p++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
Al Viro | 724bb09 | 2015-06-17 12:02:56 -0400 | [diff] [blame] | 156 | bh = sb_bread(sb, uspi->s_sbbase + |
| 157 | fs32_to_cpu(sb, q->key32) + (n>>shift)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | if (!bh) |
Al Viro | 724bb09 | 2015-06-17 12:02:56 -0400 | [diff] [blame] | 159 | goto no_block; |
| 160 | ptr = (__fs32 *)bh->b_data + (n & mask); |
| 161 | if (!grow_chain32(ufsi, bh, ptr, chain, ++q)) |
| 162 | goto changed; |
| 163 | if (!q->key32) |
| 164 | goto no_block; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | } |
Al Viro | 724bb09 | 2015-06-17 12:02:56 -0400 | [diff] [blame] | 166 | res = fs32_to_cpu(sb, q->key32); |
| 167 | goto found; |
| 168 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | ufs2: |
Al Viro | 724bb09 | 2015-06-17 12:02:56 -0400 | [diff] [blame] | 170 | if (!grow_chain64(ufsi, NULL, &ufsi->i_u1.u2_i_data[*p++], chain, q)) |
| 171 | goto changed; |
| 172 | if (!q->key64) |
| 173 | goto no_block; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
| 175 | while (--depth) { |
Al Viro | 724bb09 | 2015-06-17 12:02:56 -0400 | [diff] [blame] | 176 | __fs64 *ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | struct buffer_head *bh; |
Al Viro | 4e3911f | 2015-06-04 14:13:14 -0400 | [diff] [blame] | 178 | unsigned n = *p++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
Al Viro | 724bb09 | 2015-06-17 12:02:56 -0400 | [diff] [blame] | 180 | bh = sb_bread(sb, uspi->s_sbbase + |
| 181 | fs64_to_cpu(sb, q->key64) + (n>>shift)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | if (!bh) |
Al Viro | 724bb09 | 2015-06-17 12:02:56 -0400 | [diff] [blame] | 183 | goto no_block; |
| 184 | ptr = (__fs64 *)bh->b_data + (n & mask); |
| 185 | if (!grow_chain64(ufsi, bh, ptr, chain, ++q)) |
| 186 | goto changed; |
| 187 | if (!q->key64) |
| 188 | goto no_block; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | } |
Al Viro | 724bb09 | 2015-06-17 12:02:56 -0400 | [diff] [blame] | 190 | res = fs64_to_cpu(sb, q->key64); |
| 191 | found: |
Al Viro | 4b7068c | 2015-06-04 14:27:23 -0400 | [diff] [blame] | 192 | res += uspi->s_sbbase; |
Al Viro | 724bb09 | 2015-06-17 12:02:56 -0400 | [diff] [blame] | 193 | no_block: |
| 194 | while (q > chain) { |
| 195 | brelse(q->bh); |
| 196 | q--; |
| 197 | } |
| 198 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
Al Viro | 724bb09 | 2015-06-17 12:02:56 -0400 | [diff] [blame] | 200 | changed: |
| 201 | while (q > chain) { |
| 202 | brelse(q->bh); |
| 203 | q--; |
| 204 | } |
| 205 | goto again; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Evgeniy Dushistov | 022a6dc | 2006-06-25 05:47:27 -0700 | [diff] [blame] | 208 | /** |
| 209 | * ufs_inode_getfrag() - allocate new fragment(s) |
Fabian Frederick | edc023ca | 2014-08-08 14:21:08 -0700 | [diff] [blame] | 210 | * @inode: pointer to inode |
| 211 | * @fragment: number of `fragment' which hold pointer |
Evgeniy Dushistov | 022a6dc | 2006-06-25 05:47:27 -0700 | [diff] [blame] | 212 | * to new allocated fragment(s) |
Fabian Frederick | edc023ca | 2014-08-08 14:21:08 -0700 | [diff] [blame] | 213 | * @new_fragment: number of new allocated fragment(s) |
| 214 | * @required: how many fragment(s) we require |
| 215 | * @err: we set it if something wrong |
| 216 | * @phys: pointer to where we save physical number of new allocated fragments, |
Evgeniy Dushistov | 022a6dc | 2006-06-25 05:47:27 -0700 | [diff] [blame] | 217 | * NULL if we allocate not data(indirect blocks for example). |
Fabian Frederick | edc023ca | 2014-08-08 14:21:08 -0700 | [diff] [blame] | 218 | * @new: we set it if we allocate new block |
| 219 | * @locked_page: for ufs_new_fragments() |
Evgeniy Dushistov | 022a6dc | 2006-06-25 05:47:27 -0700 | [diff] [blame] | 220 | */ |
| 221 | static struct buffer_head * |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 222 | ufs_inode_getfrag(struct inode *inode, u64 fragment, |
Evgeniy Dushistov | 022a6dc | 2006-06-25 05:47:27 -0700 | [diff] [blame] | 223 | sector_t new_fragment, unsigned int required, int *err, |
| 224 | long *phys, int *new, struct page *locked_page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | { |
| 226 | struct ufs_inode_info *ufsi = UFS_I(inode); |
Evgeniy Dushistov | 022a6dc | 2006-06-25 05:47:27 -0700 | [diff] [blame] | 227 | struct super_block *sb = inode->i_sb; |
| 228 | struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 229 | unsigned blockoff, lastblockoff; |
| 230 | u64 tmp, goal, lastfrag, block, lastblock; |
| 231 | void *p, *p2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 233 | UFSD("ENTER, ino %lu, fragment %llu, new_fragment %llu, required %u, " |
| 234 | "metadata %d\n", inode->i_ino, (unsigned long long)fragment, |
Evgeniy Dushistov | 022a6dc | 2006-06-25 05:47:27 -0700 | [diff] [blame] | 235 | (unsigned long long)new_fragment, required, !phys); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | /* TODO : to be done for write support |
| 238 | if ( (flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) |
| 239 | goto ufs2; |
| 240 | */ |
| 241 | |
| 242 | block = ufs_fragstoblks (fragment); |
| 243 | blockoff = ufs_fragnum (fragment); |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 244 | p = ufs_get_direct_data_ptr(uspi, ufsi, block); |
| 245 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | goal = 0; |
| 247 | |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 248 | tmp = ufs_data_ptr_to_cpu(sb, p); |
| 249 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | lastfrag = ufsi->i_lastfrag; |
Al Viro | bbb3eb9 | 2015-06-19 00:10:00 -0400 | [diff] [blame^] | 251 | if (tmp && fragment < lastfrag) |
| 252 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
| 254 | lastblock = ufs_fragstoblks (lastfrag); |
| 255 | lastblockoff = ufs_fragnum (lastfrag); |
| 256 | /* |
| 257 | * We will extend file into new block beyond last allocated block |
| 258 | */ |
| 259 | if (lastblock < block) { |
| 260 | /* |
| 261 | * We must reallocate last allocated block |
| 262 | */ |
| 263 | if (lastblockoff) { |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 264 | p2 = ufs_get_direct_data_ptr(uspi, ufsi, lastblock); |
| 265 | tmp = ufs_new_fragments(inode, p2, lastfrag, |
| 266 | ufs_data_ptr_to_cpu(sb, p2), |
| 267 | uspi->s_fpb - lastblockoff, |
| 268 | err, locked_page); |
Al Viro | 5a39c25 | 2015-06-18 22:39:46 -0400 | [diff] [blame] | 269 | if (!tmp) |
| 270 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | lastfrag = ufsi->i_lastfrag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | } |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 273 | tmp = ufs_data_ptr_to_cpu(sb, |
| 274 | ufs_get_direct_data_ptr(uspi, ufsi, |
| 275 | lastblock)); |
Evgeniy Dushistov | c37336b | 2006-08-27 01:23:45 -0700 | [diff] [blame] | 276 | if (tmp) |
| 277 | goal = tmp + uspi->s_fpb; |
Al Viro | 010d331 | 2015-06-17 12:44:14 -0400 | [diff] [blame] | 278 | tmp = ufs_new_fragments (inode, p, fragment - blockoff, |
Evgeniy Dushistov | 6ef4d6b | 2006-06-25 05:47:20 -0700 | [diff] [blame] | 279 | goal, required + blockoff, |
Evgeniy Dushistov | a685e26 | 2007-01-29 13:19:54 -0800 | [diff] [blame] | 280 | err, |
| 281 | phys != NULL ? locked_page : NULL); |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 282 | } else if (lastblock == block) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | /* |
| 284 | * We will extend last allocated block |
| 285 | */ |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 286 | tmp = ufs_new_fragments(inode, p, fragment - |
| 287 | (blockoff - lastblockoff), |
| 288 | ufs_data_ptr_to_cpu(sb, p), |
| 289 | required + (blockoff - lastblockoff), |
Evgeniy Dushistov | a685e26 | 2007-01-29 13:19:54 -0800 | [diff] [blame] | 290 | err, phys != NULL ? locked_page : NULL); |
Evgeniy Dushistov | c37336b | 2006-08-27 01:23:45 -0700 | [diff] [blame] | 291 | } else /* (lastblock > block) */ { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | /* |
| 293 | * We will allocate new block before last allocated block |
| 294 | */ |
Evgeniy Dushistov | c37336b | 2006-08-27 01:23:45 -0700 | [diff] [blame] | 295 | if (block) { |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 296 | tmp = ufs_data_ptr_to_cpu(sb, |
| 297 | ufs_get_direct_data_ptr(uspi, ufsi, block - 1)); |
Evgeniy Dushistov | c37336b | 2006-08-27 01:23:45 -0700 | [diff] [blame] | 298 | if (tmp) |
| 299 | goal = tmp + uspi->s_fpb; |
| 300 | } |
Evgeniy Dushistov | 6ef4d6b | 2006-06-25 05:47:20 -0700 | [diff] [blame] | 301 | tmp = ufs_new_fragments(inode, p, fragment - blockoff, |
Evgeniy Dushistov | a685e26 | 2007-01-29 13:19:54 -0800 | [diff] [blame] | 302 | goal, uspi->s_fpb, err, |
| 303 | phys != NULL ? locked_page : NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | } |
| 305 | if (!tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | *err = -ENOSPC; |
| 307 | return NULL; |
| 308 | } |
| 309 | |
Al Viro | bbb3eb9 | 2015-06-19 00:10:00 -0400 | [diff] [blame^] | 310 | if (phys) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | *err = 0; |
| 312 | *new = 1; |
| 313 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | inode->i_ctime = CURRENT_TIME_SEC; |
| 315 | if (IS_SYNC(inode)) |
| 316 | ufs_sync_inode (inode); |
| 317 | mark_inode_dirty(inode); |
Al Viro | bbb3eb9 | 2015-06-19 00:10:00 -0400 | [diff] [blame^] | 318 | out: |
| 319 | tmp += uspi->s_sbbase + blockoff; |
| 320 | if (!phys) { |
| 321 | return sb_getblk(sb, tmp); |
| 322 | } else { |
| 323 | *phys = tmp; |
| 324 | return NULL; |
| 325 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | |
| 327 | /* This part : To be implemented .... |
| 328 | Required only for writing, not required for READ-ONLY. |
| 329 | ufs2: |
| 330 | |
| 331 | u2_block = ufs_fragstoblks(fragment); |
| 332 | u2_blockoff = ufs_fragnum(fragment); |
| 333 | p = ufsi->i_u1.u2_i_data + block; |
| 334 | goal = 0; |
| 335 | |
| 336 | repeat2: |
| 337 | tmp = fs32_to_cpu(sb, *p); |
| 338 | lastfrag = ufsi->i_lastfrag; |
| 339 | |
| 340 | */ |
| 341 | } |
| 342 | |
Evgeniy Dushistov | 022a6dc | 2006-06-25 05:47:27 -0700 | [diff] [blame] | 343 | /** |
| 344 | * ufs_inode_getblock() - allocate new block |
Fabian Frederick | edc023ca | 2014-08-08 14:21:08 -0700 | [diff] [blame] | 345 | * @inode: pointer to inode |
| 346 | * @bh: pointer to block which hold "pointer" to new allocated block |
| 347 | * @fragment: number of `fragment' which hold pointer |
Evgeniy Dushistov | 022a6dc | 2006-06-25 05:47:27 -0700 | [diff] [blame] | 348 | * to new allocated block |
Fabian Frederick | edc023ca | 2014-08-08 14:21:08 -0700 | [diff] [blame] | 349 | * @new_fragment: number of new allocated fragment |
Evgeniy Dushistov | 022a6dc | 2006-06-25 05:47:27 -0700 | [diff] [blame] | 350 | * (block will hold this fragment and also uspi->s_fpb-1) |
Fabian Frederick | edc023ca | 2014-08-08 14:21:08 -0700 | [diff] [blame] | 351 | * @err: see ufs_inode_getfrag() |
| 352 | * @phys: see ufs_inode_getfrag() |
| 353 | * @new: see ufs_inode_getfrag() |
| 354 | * @locked_page: see ufs_inode_getfrag() |
Evgeniy Dushistov | 022a6dc | 2006-06-25 05:47:27 -0700 | [diff] [blame] | 355 | */ |
| 356 | static struct buffer_head * |
| 357 | ufs_inode_getblock(struct inode *inode, struct buffer_head *bh, |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 358 | u64 fragment, sector_t new_fragment, int *err, |
Evgeniy Dushistov | 022a6dc | 2006-06-25 05:47:27 -0700 | [diff] [blame] | 359 | long *phys, int *new, struct page *locked_page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | { |
Evgeniy Dushistov | 022a6dc | 2006-06-25 05:47:27 -0700 | [diff] [blame] | 361 | struct super_block *sb = inode->i_sb; |
| 362 | struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | struct buffer_head * result; |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 364 | unsigned blockoff; |
Al Viro | bbb3eb9 | 2015-06-19 00:10:00 -0400 | [diff] [blame^] | 365 | u64 tmp = 0, goal, block; |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 366 | void *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | block = ufs_fragstoblks (fragment); |
| 369 | blockoff = ufs_fragnum (fragment); |
| 370 | |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 371 | UFSD("ENTER, ino %lu, fragment %llu, new_fragment %llu, metadata %d\n", |
| 372 | inode->i_ino, (unsigned long long)fragment, |
| 373 | (unsigned long long)new_fragment, !phys); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | |
| 375 | result = NULL; |
| 376 | if (!bh) |
| 377 | goto out; |
| 378 | if (!buffer_uptodate(bh)) { |
| 379 | ll_rw_block (READ, 1, &bh); |
| 380 | wait_on_buffer (bh); |
| 381 | if (!buffer_uptodate(bh)) |
| 382 | goto out; |
| 383 | } |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 384 | if (uspi->fs_magic == UFS2_MAGIC) |
| 385 | p = (__fs64 *)bh->b_data + block; |
| 386 | else |
| 387 | p = (__fs32 *)bh->b_data + block; |
Al Viro | 5a39c25 | 2015-06-18 22:39:46 -0400 | [diff] [blame] | 388 | |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 389 | tmp = ufs_data_ptr_to_cpu(sb, p); |
Al Viro | bbb3eb9 | 2015-06-19 00:10:00 -0400 | [diff] [blame^] | 390 | if (tmp) |
Al Viro | 5a39c25 | 2015-06-18 22:39:46 -0400 | [diff] [blame] | 391 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | |
Evgeniy Dushistov | 54fb996 | 2007-02-12 00:54:32 -0800 | [diff] [blame] | 393 | if (block && (uspi->fs_magic == UFS2_MAGIC ? |
| 394 | (tmp = fs64_to_cpu(sb, ((__fs64 *)bh->b_data)[block-1])) : |
| 395 | (tmp = fs32_to_cpu(sb, ((__fs32 *)bh->b_data)[block-1])))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | goal = tmp + uspi->s_fpb; |
| 397 | else |
| 398 | goal = bh->b_blocknr + uspi->s_fpb; |
Evgeniy Dushistov | 6ef4d6b | 2006-06-25 05:47:20 -0700 | [diff] [blame] | 399 | tmp = ufs_new_fragments(inode, p, ufs_blknum(new_fragment), goal, |
| 400 | uspi->s_fpb, err, locked_page); |
Al Viro | 5a39c25 | 2015-06-18 22:39:46 -0400 | [diff] [blame] | 401 | if (!tmp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | goto out; |
Evgeniy Dushistov | c9a27b5 | 2006-06-25 05:47:19 -0700 | [diff] [blame] | 403 | |
Al Viro | bbb3eb9 | 2015-06-19 00:10:00 -0400 | [diff] [blame^] | 404 | if (new) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | *new = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | |
| 407 | mark_buffer_dirty(bh); |
| 408 | if (IS_SYNC(inode)) |
| 409 | sync_dirty_buffer(bh); |
| 410 | inode->i_ctime = CURRENT_TIME_SEC; |
| 411 | mark_inode_dirty(inode); |
| 412 | out: |
| 413 | brelse (bh); |
Al Viro | bbb3eb9 | 2015-06-19 00:10:00 -0400 | [diff] [blame^] | 414 | if (tmp) { |
| 415 | tmp += uspi->s_sbbase + blockoff; |
| 416 | if (phys) { |
| 417 | *phys = tmp; |
| 418 | } else { |
| 419 | result = sb_getblk(sb, tmp); |
| 420 | } |
| 421 | } |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 422 | UFSD("EXIT\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | return result; |
| 424 | } |
| 425 | |
Evgeniy Dushistov | 022a6dc | 2006-06-25 05:47:27 -0700 | [diff] [blame] | 426 | /** |
Alessio Igor Bogani | 7422caa | 2011-04-08 19:33:07 +0200 | [diff] [blame] | 427 | * ufs_getfrag_block() - `get_block_t' function, interface between UFS and |
Evgeniy Dushistov | 022a6dc | 2006-06-25 05:47:27 -0700 | [diff] [blame] | 428 | * readpage, writepage and so on |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | */ |
| 430 | |
Al Viro | 010d331 | 2015-06-17 12:44:14 -0400 | [diff] [blame] | 431 | static int ufs_getfrag_block(struct inode *inode, sector_t fragment, struct buffer_head *bh_result, int create) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | { |
| 433 | struct super_block * sb = inode->i_sb; |
Arnd Bergmann | 788257d | 2011-01-24 10:14:12 +0100 | [diff] [blame] | 434 | struct ufs_sb_info * sbi = UFS_SB(sb); |
| 435 | struct ufs_sb_private_info * uspi = sbi->s_uspi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | struct buffer_head * bh; |
| 437 | int ret, err, new; |
Al Viro | 4b7068c | 2015-06-04 14:27:23 -0400 | [diff] [blame] | 438 | unsigned offsets[4]; |
| 439 | int depth = ufs_block_to_path(inode, fragment >> uspi->s_fpbshift, offsets); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | unsigned long ptr,phys; |
| 441 | u64 phys64 = 0; |
Al Viro | 010d331 | 2015-06-17 12:44:14 -0400 | [diff] [blame] | 442 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | if (!create) { |
Al Viro | 4b7068c | 2015-06-04 14:27:23 -0400 | [diff] [blame] | 444 | phys64 = ufs_frag_map(inode, offsets, depth); |
| 445 | if (phys64) { |
| 446 | phys64 += fragment & uspi->s_fpbmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | map_bh(bh_result, sb, phys64); |
Al Viro | 4b7068c | 2015-06-04 14:27:23 -0400 | [diff] [blame] | 448 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | return 0; |
| 450 | } |
| 451 | |
| 452 | /* This code entered only while writing ....? */ |
| 453 | |
| 454 | err = -EIO; |
| 455 | new = 0; |
| 456 | ret = 0; |
| 457 | bh = NULL; |
| 458 | |
Al Viro | 724bb09 | 2015-06-17 12:02:56 -0400 | [diff] [blame] | 459 | mutex_lock(&UFS_I(inode)->truncate_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 461 | UFSD("ENTER, ino %lu, fragment %llu\n", inode->i_ino, (unsigned long long)fragment); |
Al Viro | 71dd428 | 2015-06-04 14:34:43 -0400 | [diff] [blame] | 462 | if (!depth) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | goto abort_too_big; |
| 464 | |
| 465 | err = 0; |
| 466 | ptr = fragment; |
Al Viro | 010d331 | 2015-06-17 12:44:14 -0400 | [diff] [blame] | 467 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | /* |
| 469 | * ok, these macros clean the logic up a bit and make |
| 470 | * it much more readable: |
| 471 | */ |
| 472 | #define GET_INODE_DATABLOCK(x) \ |
Evgeniy Dushistov | a685e26 | 2007-01-29 13:19:54 -0800 | [diff] [blame] | 473 | ufs_inode_getfrag(inode, x, fragment, 1, &err, &phys, &new,\ |
| 474 | bh_result->b_page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | #define GET_INODE_PTR(x) \ |
Evgeniy Dushistov | a685e26 | 2007-01-29 13:19:54 -0800 | [diff] [blame] | 476 | ufs_inode_getfrag(inode, x, fragment, uspi->s_fpb, &err, NULL, NULL,\ |
| 477 | bh_result->b_page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | #define GET_INDIRECT_DATABLOCK(x) \ |
Evgeniy Dushistov | 022a6dc | 2006-06-25 05:47:27 -0700 | [diff] [blame] | 479 | ufs_inode_getblock(inode, bh, x, fragment, \ |
Evgeniy Dushistov | d63b709 | 2007-01-05 16:37:04 -0800 | [diff] [blame] | 480 | &err, &phys, &new, bh_result->b_page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | #define GET_INDIRECT_PTR(x) \ |
Evgeniy Dushistov | 022a6dc | 2006-06-25 05:47:27 -0700 | [diff] [blame] | 482 | ufs_inode_getblock(inode, bh, x, fragment, \ |
Evgeniy Dushistov | d63b709 | 2007-01-05 16:37:04 -0800 | [diff] [blame] | 483 | &err, NULL, NULL, NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | |
Al Viro | 71dd428 | 2015-06-04 14:34:43 -0400 | [diff] [blame] | 485 | if (depth == 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | bh = GET_INODE_DATABLOCK(ptr); |
| 487 | goto out; |
| 488 | } |
| 489 | ptr -= UFS_NDIR_FRAGMENT; |
Al Viro | 71dd428 | 2015-06-04 14:34:43 -0400 | [diff] [blame] | 490 | if (depth == 2) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | bh = GET_INODE_PTR(UFS_IND_FRAGMENT + (ptr >> uspi->s_apbshift)); |
| 492 | goto get_indirect; |
| 493 | } |
| 494 | ptr -= 1 << (uspi->s_apbshift + uspi->s_fpbshift); |
Al Viro | 71dd428 | 2015-06-04 14:34:43 -0400 | [diff] [blame] | 495 | if (depth == 3) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | bh = GET_INODE_PTR(UFS_DIND_FRAGMENT + (ptr >> uspi->s_2apbshift)); |
| 497 | goto get_double; |
| 498 | } |
| 499 | ptr -= 1 << (uspi->s_2apbshift + uspi->s_fpbshift); |
| 500 | bh = GET_INODE_PTR(UFS_TIND_FRAGMENT + (ptr >> uspi->s_3apbshift)); |
| 501 | bh = GET_INDIRECT_PTR((ptr >> uspi->s_2apbshift) & uspi->s_apbmask); |
| 502 | get_double: |
| 503 | bh = GET_INDIRECT_PTR((ptr >> uspi->s_apbshift) & uspi->s_apbmask); |
| 504 | get_indirect: |
| 505 | bh = GET_INDIRECT_DATABLOCK(ptr & uspi->s_apbmask); |
| 506 | |
| 507 | #undef GET_INODE_DATABLOCK |
| 508 | #undef GET_INODE_PTR |
| 509 | #undef GET_INDIRECT_DATABLOCK |
| 510 | #undef GET_INDIRECT_PTR |
| 511 | |
| 512 | out: |
| 513 | if (err) |
| 514 | goto abort; |
| 515 | if (new) |
| 516 | set_buffer_new(bh_result); |
| 517 | map_bh(bh_result, sb, phys); |
| 518 | abort: |
Al Viro | 724bb09 | 2015-06-17 12:02:56 -0400 | [diff] [blame] | 519 | mutex_unlock(&UFS_I(inode)->truncate_mutex); |
Arnd Bergmann | 788257d | 2011-01-24 10:14:12 +0100 | [diff] [blame] | 520 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | return err; |
| 522 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | abort_too_big: |
| 524 | ufs_warning(sb, "ufs_get_block", "block > big"); |
| 525 | goto abort; |
| 526 | } |
| 527 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | static int ufs_writepage(struct page *page, struct writeback_control *wbc) |
| 529 | { |
| 530 | return block_write_full_page(page,ufs_getfrag_block,wbc); |
| 531 | } |
Nick Piggin | 82b9d1d | 2007-10-16 01:25:19 -0700 | [diff] [blame] | 532 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | static int ufs_readpage(struct file *file, struct page *page) |
| 534 | { |
| 535 | return block_read_full_page(page,ufs_getfrag_block); |
| 536 | } |
Nick Piggin | 82b9d1d | 2007-10-16 01:25:19 -0700 | [diff] [blame] | 537 | |
Christoph Hellwig | f4e420d | 2010-06-04 11:29:56 +0200 | [diff] [blame] | 538 | int ufs_prepare_chunk(struct page *page, loff_t pos, unsigned len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | { |
Christoph Hellwig | 6e1db88 | 2010-06-04 11:29:57 +0200 | [diff] [blame] | 540 | return __block_write_begin(page, pos, len, ufs_getfrag_block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | } |
Nick Piggin | 82b9d1d | 2007-10-16 01:25:19 -0700 | [diff] [blame] | 542 | |
Al Viro | 010d331 | 2015-06-17 12:44:14 -0400 | [diff] [blame] | 543 | static void ufs_truncate_blocks(struct inode *); |
| 544 | |
Marco Stornelli | 83f6e37 | 2012-12-15 11:45:14 +0100 | [diff] [blame] | 545 | static void ufs_write_failed(struct address_space *mapping, loff_t to) |
| 546 | { |
| 547 | struct inode *inode = mapping->host; |
| 548 | |
Al Viro | 3b7a3a0 | 2015-06-16 18:06:40 -0400 | [diff] [blame] | 549 | if (to > inode->i_size) { |
Kirill A. Shutemov | 7caef26 | 2013-09-12 15:13:56 -0700 | [diff] [blame] | 550 | truncate_pagecache(inode, inode->i_size); |
Al Viro | 3b7a3a0 | 2015-06-16 18:06:40 -0400 | [diff] [blame] | 551 | ufs_truncate_blocks(inode); |
| 552 | } |
Marco Stornelli | 83f6e37 | 2012-12-15 11:45:14 +0100 | [diff] [blame] | 553 | } |
| 554 | |
Nick Piggin | 82b9d1d | 2007-10-16 01:25:19 -0700 | [diff] [blame] | 555 | static int ufs_write_begin(struct file *file, struct address_space *mapping, |
| 556 | loff_t pos, unsigned len, unsigned flags, |
| 557 | struct page **pagep, void **fsdata) |
| 558 | { |
Christoph Hellwig | 155130a | 2010-06-04 11:29:58 +0200 | [diff] [blame] | 559 | int ret; |
| 560 | |
| 561 | ret = block_write_begin(mapping, pos, len, flags, pagep, |
Christoph Hellwig | f4e420d | 2010-06-04 11:29:56 +0200 | [diff] [blame] | 562 | ufs_getfrag_block); |
Marco Stornelli | 83f6e37 | 2012-12-15 11:45:14 +0100 | [diff] [blame] | 563 | if (unlikely(ret)) |
| 564 | ufs_write_failed(mapping, pos + len); |
Christoph Hellwig | 155130a | 2010-06-04 11:29:58 +0200 | [diff] [blame] | 565 | |
| 566 | return ret; |
Nick Piggin | 82b9d1d | 2007-10-16 01:25:19 -0700 | [diff] [blame] | 567 | } |
| 568 | |
Al Viro | 3b7a3a0 | 2015-06-16 18:06:40 -0400 | [diff] [blame] | 569 | static int ufs_write_end(struct file *file, struct address_space *mapping, |
| 570 | loff_t pos, unsigned len, unsigned copied, |
| 571 | struct page *page, void *fsdata) |
| 572 | { |
| 573 | int ret; |
| 574 | |
| 575 | ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata); |
| 576 | if (ret < len) |
| 577 | ufs_write_failed(mapping, pos + len); |
| 578 | return ret; |
| 579 | } |
| 580 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | static sector_t ufs_bmap(struct address_space *mapping, sector_t block) |
| 582 | { |
| 583 | return generic_block_bmap(mapping,block,ufs_getfrag_block); |
| 584 | } |
Nick Piggin | 82b9d1d | 2007-10-16 01:25:19 -0700 | [diff] [blame] | 585 | |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 586 | const struct address_space_operations ufs_aops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | .readpage = ufs_readpage, |
| 588 | .writepage = ufs_writepage, |
Nick Piggin | 82b9d1d | 2007-10-16 01:25:19 -0700 | [diff] [blame] | 589 | .write_begin = ufs_write_begin, |
Al Viro | 3b7a3a0 | 2015-06-16 18:06:40 -0400 | [diff] [blame] | 590 | .write_end = ufs_write_end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | .bmap = ufs_bmap |
| 592 | }; |
| 593 | |
Evgeniy Dushistov | 826843a | 2006-06-25 05:47:21 -0700 | [diff] [blame] | 594 | static void ufs_set_inode_ops(struct inode *inode) |
| 595 | { |
| 596 | if (S_ISREG(inode->i_mode)) { |
| 597 | inode->i_op = &ufs_file_inode_operations; |
| 598 | inode->i_fop = &ufs_file_operations; |
| 599 | inode->i_mapping->a_ops = &ufs_aops; |
| 600 | } else if (S_ISDIR(inode->i_mode)) { |
| 601 | inode->i_op = &ufs_dir_inode_operations; |
| 602 | inode->i_fop = &ufs_dir_operations; |
| 603 | inode->i_mapping->a_ops = &ufs_aops; |
| 604 | } else if (S_ISLNK(inode->i_mode)) { |
Al Viro | 4b8061a | 2015-05-02 10:28:56 -0400 | [diff] [blame] | 605 | if (!inode->i_blocks) { |
Evgeniy Dushistov | 826843a | 2006-06-25 05:47:21 -0700 | [diff] [blame] | 606 | inode->i_op = &ufs_fast_symlink_inode_operations; |
Al Viro | 4b8061a | 2015-05-02 10:28:56 -0400 | [diff] [blame] | 607 | inode->i_link = (char *)UFS_I(inode)->i_u1.i_symlink; |
| 608 | } else { |
Dmitry Monakhov | 311b9549 | 2010-04-15 00:56:58 +0200 | [diff] [blame] | 609 | inode->i_op = &ufs_symlink_inode_operations; |
Evgeniy Dushistov | 826843a | 2006-06-25 05:47:21 -0700 | [diff] [blame] | 610 | inode->i_mapping->a_ops = &ufs_aops; |
| 611 | } |
| 612 | } else |
| 613 | init_special_inode(inode, inode->i_mode, |
| 614 | ufs_get_inode_dev(inode->i_sb, UFS_I(inode))); |
| 615 | } |
| 616 | |
Evgeniy Dushistov | 07a0cfe | 2007-04-16 22:53:24 -0700 | [diff] [blame] | 617 | static int ufs1_read_inode(struct inode *inode, struct ufs_inode *ufs_inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | { |
| 619 | struct ufs_inode_info *ufsi = UFS_I(inode); |
Evgeniy Dushistov | 05f225d | 2006-06-27 02:53:59 -0700 | [diff] [blame] | 620 | struct super_block *sb = inode->i_sb; |
Al Viro | 6a9a06d | 2011-07-26 02:49:13 -0400 | [diff] [blame] | 621 | umode_t mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | |
| 623 | /* |
| 624 | * Copy data to the in-core inode. |
| 625 | */ |
| 626 | inode->i_mode = mode = fs16_to_cpu(sb, ufs_inode->ui_mode); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 627 | set_nlink(inode, fs16_to_cpu(sb, ufs_inode->ui_nlink)); |
Evgeniy Dushistov | 07a0cfe | 2007-04-16 22:53:24 -0700 | [diff] [blame] | 628 | if (inode->i_nlink == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | ufs_error (sb, "ufs_read_inode", "inode %lu has zero nlink\n", inode->i_ino); |
Evgeniy Dushistov | 07a0cfe | 2007-04-16 22:53:24 -0700 | [diff] [blame] | 630 | return -1; |
| 631 | } |
Al Viro | 010d331 | 2015-06-17 12:44:14 -0400 | [diff] [blame] | 632 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | /* |
| 634 | * Linux now has 32-bit uid and gid, so we can support EFT. |
| 635 | */ |
Eric W. Biederman | 72235465 | 2012-02-10 12:21:22 -0800 | [diff] [blame] | 636 | i_uid_write(inode, ufs_get_inode_uid(sb, ufs_inode)); |
| 637 | i_gid_write(inode, ufs_get_inode_gid(sb, ufs_inode)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | |
| 639 | inode->i_size = fs64_to_cpu(sb, ufs_inode->ui_size); |
| 640 | inode->i_atime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_atime.tv_sec); |
| 641 | inode->i_ctime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_ctime.tv_sec); |
| 642 | inode->i_mtime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_mtime.tv_sec); |
| 643 | inode->i_mtime.tv_nsec = 0; |
| 644 | inode->i_atime.tv_nsec = 0; |
| 645 | inode->i_ctime.tv_nsec = 0; |
| 646 | inode->i_blocks = fs32_to_cpu(sb, ufs_inode->ui_blocks); |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 647 | inode->i_generation = fs32_to_cpu(sb, ufs_inode->ui_gen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | ufsi->i_flags = fs32_to_cpu(sb, ufs_inode->ui_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | ufsi->i_shadow = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_shadow); |
| 650 | ufsi->i_oeftflag = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_oeftflag); |
Evgeniy Dushistov | 05f225d | 2006-06-27 02:53:59 -0700 | [diff] [blame] | 651 | |
Al Viro | 010d331 | 2015-06-17 12:44:14 -0400 | [diff] [blame] | 652 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | if (S_ISCHR(mode) || S_ISBLK(mode) || inode->i_blocks) { |
Duane Griffin | f33219b | 2009-01-08 22:43:49 +0000 | [diff] [blame] | 654 | memcpy(ufsi->i_u1.i_data, &ufs_inode->ui_u2.ui_addr, |
| 655 | sizeof(ufs_inode->ui_u2.ui_addr)); |
Evgeniy Dushistov | dd187a2 | 2006-06-25 05:47:25 -0700 | [diff] [blame] | 656 | } else { |
Duane Griffin | f33219b | 2009-01-08 22:43:49 +0000 | [diff] [blame] | 657 | memcpy(ufsi->i_u1.i_symlink, ufs_inode->ui_u2.ui_symlink, |
Duane Griffin | b12903f | 2009-01-08 22:43:50 +0000 | [diff] [blame] | 658 | sizeof(ufs_inode->ui_u2.ui_symlink) - 1); |
| 659 | ufsi->i_u1.i_symlink[sizeof(ufs_inode->ui_u2.ui_symlink) - 1] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | } |
Evgeniy Dushistov | 07a0cfe | 2007-04-16 22:53:24 -0700 | [diff] [blame] | 661 | return 0; |
Evgeniy Dushistov | 05f225d | 2006-06-27 02:53:59 -0700 | [diff] [blame] | 662 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | |
Evgeniy Dushistov | 07a0cfe | 2007-04-16 22:53:24 -0700 | [diff] [blame] | 664 | static int ufs2_read_inode(struct inode *inode, struct ufs2_inode *ufs2_inode) |
Evgeniy Dushistov | 05f225d | 2006-06-27 02:53:59 -0700 | [diff] [blame] | 665 | { |
| 666 | struct ufs_inode_info *ufsi = UFS_I(inode); |
| 667 | struct super_block *sb = inode->i_sb; |
Al Viro | 6a9a06d | 2011-07-26 02:49:13 -0400 | [diff] [blame] | 668 | umode_t mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 670 | UFSD("Reading ufs2 inode, ino %lu\n", inode->i_ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | /* |
| 672 | * Copy data to the in-core inode. |
| 673 | */ |
| 674 | inode->i_mode = mode = fs16_to_cpu(sb, ufs2_inode->ui_mode); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 675 | set_nlink(inode, fs16_to_cpu(sb, ufs2_inode->ui_nlink)); |
Evgeniy Dushistov | 07a0cfe | 2007-04-16 22:53:24 -0700 | [diff] [blame] | 676 | if (inode->i_nlink == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | ufs_error (sb, "ufs_read_inode", "inode %lu has zero nlink\n", inode->i_ino); |
Evgeniy Dushistov | 07a0cfe | 2007-04-16 22:53:24 -0700 | [diff] [blame] | 678 | return -1; |
| 679 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | |
| 681 | /* |
| 682 | * Linux now has 32-bit uid and gid, so we can support EFT. |
| 683 | */ |
Eric W. Biederman | 72235465 | 2012-02-10 12:21:22 -0800 | [diff] [blame] | 684 | i_uid_write(inode, fs32_to_cpu(sb, ufs2_inode->ui_uid)); |
| 685 | i_gid_write(inode, fs32_to_cpu(sb, ufs2_inode->ui_gid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | |
| 687 | inode->i_size = fs64_to_cpu(sb, ufs2_inode->ui_size); |
Evgeniy Dushistov | 2189850 | 2007-03-16 13:38:07 -0800 | [diff] [blame] | 688 | inode->i_atime.tv_sec = fs64_to_cpu(sb, ufs2_inode->ui_atime); |
| 689 | inode->i_ctime.tv_sec = fs64_to_cpu(sb, ufs2_inode->ui_ctime); |
| 690 | inode->i_mtime.tv_sec = fs64_to_cpu(sb, ufs2_inode->ui_mtime); |
| 691 | inode->i_atime.tv_nsec = fs32_to_cpu(sb, ufs2_inode->ui_atimensec); |
| 692 | inode->i_ctime.tv_nsec = fs32_to_cpu(sb, ufs2_inode->ui_ctimensec); |
| 693 | inode->i_mtime.tv_nsec = fs32_to_cpu(sb, ufs2_inode->ui_mtimensec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | inode->i_blocks = fs64_to_cpu(sb, ufs2_inode->ui_blocks); |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 695 | inode->i_generation = fs32_to_cpu(sb, ufs2_inode->ui_gen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | ufsi->i_flags = fs32_to_cpu(sb, ufs2_inode->ui_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | /* |
| 698 | ufsi->i_shadow = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_shadow); |
| 699 | ufsi->i_oeftflag = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_oeftflag); |
| 700 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | |
| 702 | if (S_ISCHR(mode) || S_ISBLK(mode) || inode->i_blocks) { |
Duane Griffin | f33219b | 2009-01-08 22:43:49 +0000 | [diff] [blame] | 703 | memcpy(ufsi->i_u1.u2_i_data, &ufs2_inode->ui_u2.ui_addr, |
| 704 | sizeof(ufs2_inode->ui_u2.ui_addr)); |
Evgeniy Dushistov | 05f225d | 2006-06-27 02:53:59 -0700 | [diff] [blame] | 705 | } else { |
Duane Griffin | f33219b | 2009-01-08 22:43:49 +0000 | [diff] [blame] | 706 | memcpy(ufsi->i_u1.i_symlink, ufs2_inode->ui_u2.ui_symlink, |
Duane Griffin | b12903f | 2009-01-08 22:43:50 +0000 | [diff] [blame] | 707 | sizeof(ufs2_inode->ui_u2.ui_symlink) - 1); |
| 708 | ufsi->i_u1.i_symlink[sizeof(ufs2_inode->ui_u2.ui_symlink) - 1] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | } |
Evgeniy Dushistov | 07a0cfe | 2007-04-16 22:53:24 -0700 | [diff] [blame] | 710 | return 0; |
Evgeniy Dushistov | 05f225d | 2006-06-27 02:53:59 -0700 | [diff] [blame] | 711 | } |
| 712 | |
David Howells | b55c460 | 2008-02-07 00:15:48 -0800 | [diff] [blame] | 713 | struct inode *ufs_iget(struct super_block *sb, unsigned long ino) |
Evgeniy Dushistov | 05f225d | 2006-06-27 02:53:59 -0700 | [diff] [blame] | 714 | { |
David Howells | b55c460 | 2008-02-07 00:15:48 -0800 | [diff] [blame] | 715 | struct ufs_inode_info *ufsi; |
| 716 | struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; |
Evgeniy Dushistov | 05f225d | 2006-06-27 02:53:59 -0700 | [diff] [blame] | 717 | struct buffer_head * bh; |
David Howells | b55c460 | 2008-02-07 00:15:48 -0800 | [diff] [blame] | 718 | struct inode *inode; |
Evgeniy Dushistov | 07a0cfe | 2007-04-16 22:53:24 -0700 | [diff] [blame] | 719 | int err; |
Evgeniy Dushistov | 05f225d | 2006-06-27 02:53:59 -0700 | [diff] [blame] | 720 | |
David Howells | b55c460 | 2008-02-07 00:15:48 -0800 | [diff] [blame] | 721 | UFSD("ENTER, ino %lu\n", ino); |
Evgeniy Dushistov | 05f225d | 2006-06-27 02:53:59 -0700 | [diff] [blame] | 722 | |
David Howells | b55c460 | 2008-02-07 00:15:48 -0800 | [diff] [blame] | 723 | if (ino < UFS_ROOTINO || ino > (uspi->s_ncg * uspi->s_ipg)) { |
Evgeniy Dushistov | 05f225d | 2006-06-27 02:53:59 -0700 | [diff] [blame] | 724 | ufs_warning(sb, "ufs_read_inode", "bad inode number (%lu)\n", |
David Howells | b55c460 | 2008-02-07 00:15:48 -0800 | [diff] [blame] | 725 | ino); |
| 726 | return ERR_PTR(-EIO); |
Evgeniy Dushistov | 05f225d | 2006-06-27 02:53:59 -0700 | [diff] [blame] | 727 | } |
| 728 | |
David Howells | b55c460 | 2008-02-07 00:15:48 -0800 | [diff] [blame] | 729 | inode = iget_locked(sb, ino); |
| 730 | if (!inode) |
| 731 | return ERR_PTR(-ENOMEM); |
| 732 | if (!(inode->i_state & I_NEW)) |
| 733 | return inode; |
| 734 | |
| 735 | ufsi = UFS_I(inode); |
| 736 | |
Evgeniy Dushistov | 05f225d | 2006-06-27 02:53:59 -0700 | [diff] [blame] | 737 | bh = sb_bread(sb, uspi->s_sbbase + ufs_inotofsba(inode->i_ino)); |
| 738 | if (!bh) { |
| 739 | ufs_warning(sb, "ufs_read_inode", "unable to read inode %lu\n", |
| 740 | inode->i_ino); |
| 741 | goto bad_inode; |
| 742 | } |
| 743 | if ((UFS_SB(sb)->s_flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) { |
| 744 | struct ufs2_inode *ufs2_inode = (struct ufs2_inode *)bh->b_data; |
| 745 | |
Evgeniy Dushistov | 07a0cfe | 2007-04-16 22:53:24 -0700 | [diff] [blame] | 746 | err = ufs2_read_inode(inode, |
| 747 | ufs2_inode + ufs_inotofsbo(inode->i_ino)); |
Evgeniy Dushistov | 05f225d | 2006-06-27 02:53:59 -0700 | [diff] [blame] | 748 | } else { |
| 749 | struct ufs_inode *ufs_inode = (struct ufs_inode *)bh->b_data; |
| 750 | |
Evgeniy Dushistov | 07a0cfe | 2007-04-16 22:53:24 -0700 | [diff] [blame] | 751 | err = ufs1_read_inode(inode, |
| 752 | ufs_inode + ufs_inotofsbo(inode->i_ino)); |
Evgeniy Dushistov | 05f225d | 2006-06-27 02:53:59 -0700 | [diff] [blame] | 753 | } |
| 754 | |
Evgeniy Dushistov | 07a0cfe | 2007-04-16 22:53:24 -0700 | [diff] [blame] | 755 | if (err) |
| 756 | goto bad_inode; |
Evgeniy Dushistov | 05f225d | 2006-06-27 02:53:59 -0700 | [diff] [blame] | 757 | inode->i_version++; |
| 758 | ufsi->i_lastfrag = |
| 759 | (inode->i_size + uspi->s_fsize - 1) >> uspi->s_fshift; |
| 760 | ufsi->i_dir_start_lookup = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | ufsi->i_osync = 0; |
| 762 | |
Evgeniy Dushistov | 826843a | 2006-06-25 05:47:21 -0700 | [diff] [blame] | 763 | ufs_set_inode_ops(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | |
| 765 | brelse(bh); |
| 766 | |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 767 | UFSD("EXIT\n"); |
David Howells | b55c460 | 2008-02-07 00:15:48 -0800 | [diff] [blame] | 768 | unlock_new_inode(inode); |
| 769 | return inode; |
Evgeniy Dushistov | 05f225d | 2006-06-27 02:53:59 -0700 | [diff] [blame] | 770 | |
| 771 | bad_inode: |
David Howells | b55c460 | 2008-02-07 00:15:48 -0800 | [diff] [blame] | 772 | iget_failed(inode); |
| 773 | return ERR_PTR(-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | } |
| 775 | |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 776 | static void ufs1_update_inode(struct inode *inode, struct ufs_inode *ufs_inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | { |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 778 | struct super_block *sb = inode->i_sb; |
| 779 | struct ufs_inode_info *ufsi = UFS_I(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | |
| 781 | ufs_inode->ui_mode = cpu_to_fs16(sb, inode->i_mode); |
| 782 | ufs_inode->ui_nlink = cpu_to_fs16(sb, inode->i_nlink); |
| 783 | |
Eric W. Biederman | 72235465 | 2012-02-10 12:21:22 -0800 | [diff] [blame] | 784 | ufs_set_inode_uid(sb, ufs_inode, i_uid_read(inode)); |
| 785 | ufs_set_inode_gid(sb, ufs_inode, i_gid_read(inode)); |
Al Viro | 010d331 | 2015-06-17 12:44:14 -0400 | [diff] [blame] | 786 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | ufs_inode->ui_size = cpu_to_fs64(sb, inode->i_size); |
| 788 | ufs_inode->ui_atime.tv_sec = cpu_to_fs32(sb, inode->i_atime.tv_sec); |
| 789 | ufs_inode->ui_atime.tv_usec = 0; |
| 790 | ufs_inode->ui_ctime.tv_sec = cpu_to_fs32(sb, inode->i_ctime.tv_sec); |
| 791 | ufs_inode->ui_ctime.tv_usec = 0; |
| 792 | ufs_inode->ui_mtime.tv_sec = cpu_to_fs32(sb, inode->i_mtime.tv_sec); |
| 793 | ufs_inode->ui_mtime.tv_usec = 0; |
| 794 | ufs_inode->ui_blocks = cpu_to_fs32(sb, inode->i_blocks); |
| 795 | ufs_inode->ui_flags = cpu_to_fs32(sb, ufsi->i_flags); |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 796 | ufs_inode->ui_gen = cpu_to_fs32(sb, inode->i_generation); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 798 | if ((UFS_SB(sb)->s_flags & UFS_UID_MASK) == UFS_UID_EFT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | ufs_inode->ui_u3.ui_sun.ui_shadow = cpu_to_fs32(sb, ufsi->i_shadow); |
| 800 | ufs_inode->ui_u3.ui_sun.ui_oeftflag = cpu_to_fs32(sb, ufsi->i_oeftflag); |
| 801 | } |
| 802 | |
| 803 | if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { |
| 804 | /* ufs_inode->ui_u2.ui_addr.ui_db[0] = cpu_to_fs32(sb, inode->i_rdev); */ |
| 805 | ufs_inode->ui_u2.ui_addr.ui_db[0] = ufsi->i_u1.i_data[0]; |
| 806 | } else if (inode->i_blocks) { |
Duane Griffin | f33219b | 2009-01-08 22:43:49 +0000 | [diff] [blame] | 807 | memcpy(&ufs_inode->ui_u2.ui_addr, ufsi->i_u1.i_data, |
| 808 | sizeof(ufs_inode->ui_u2.ui_addr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | } |
| 810 | else { |
Duane Griffin | f33219b | 2009-01-08 22:43:49 +0000 | [diff] [blame] | 811 | memcpy(&ufs_inode->ui_u2.ui_symlink, ufsi->i_u1.i_symlink, |
| 812 | sizeof(ufs_inode->ui_u2.ui_symlink)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | if (!inode->i_nlink) |
| 816 | memset (ufs_inode, 0, sizeof(struct ufs_inode)); |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | static void ufs2_update_inode(struct inode *inode, struct ufs2_inode *ufs_inode) |
| 820 | { |
| 821 | struct super_block *sb = inode->i_sb; |
| 822 | struct ufs_inode_info *ufsi = UFS_I(inode); |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 823 | |
| 824 | UFSD("ENTER\n"); |
| 825 | ufs_inode->ui_mode = cpu_to_fs16(sb, inode->i_mode); |
| 826 | ufs_inode->ui_nlink = cpu_to_fs16(sb, inode->i_nlink); |
| 827 | |
Eric W. Biederman | 72235465 | 2012-02-10 12:21:22 -0800 | [diff] [blame] | 828 | ufs_inode->ui_uid = cpu_to_fs32(sb, i_uid_read(inode)); |
| 829 | ufs_inode->ui_gid = cpu_to_fs32(sb, i_gid_read(inode)); |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 830 | |
| 831 | ufs_inode->ui_size = cpu_to_fs64(sb, inode->i_size); |
Evgeniy Dushistov | 2189850 | 2007-03-16 13:38:07 -0800 | [diff] [blame] | 832 | ufs_inode->ui_atime = cpu_to_fs64(sb, inode->i_atime.tv_sec); |
| 833 | ufs_inode->ui_atimensec = cpu_to_fs32(sb, inode->i_atime.tv_nsec); |
| 834 | ufs_inode->ui_ctime = cpu_to_fs64(sb, inode->i_ctime.tv_sec); |
| 835 | ufs_inode->ui_ctimensec = cpu_to_fs32(sb, inode->i_ctime.tv_nsec); |
| 836 | ufs_inode->ui_mtime = cpu_to_fs64(sb, inode->i_mtime.tv_sec); |
| 837 | ufs_inode->ui_mtimensec = cpu_to_fs32(sb, inode->i_mtime.tv_nsec); |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 838 | |
| 839 | ufs_inode->ui_blocks = cpu_to_fs64(sb, inode->i_blocks); |
| 840 | ufs_inode->ui_flags = cpu_to_fs32(sb, ufsi->i_flags); |
| 841 | ufs_inode->ui_gen = cpu_to_fs32(sb, inode->i_generation); |
| 842 | |
| 843 | if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { |
| 844 | /* ufs_inode->ui_u2.ui_addr.ui_db[0] = cpu_to_fs32(sb, inode->i_rdev); */ |
| 845 | ufs_inode->ui_u2.ui_addr.ui_db[0] = ufsi->i_u1.u2_i_data[0]; |
| 846 | } else if (inode->i_blocks) { |
Duane Griffin | f33219b | 2009-01-08 22:43:49 +0000 | [diff] [blame] | 847 | memcpy(&ufs_inode->ui_u2.ui_addr, ufsi->i_u1.u2_i_data, |
| 848 | sizeof(ufs_inode->ui_u2.ui_addr)); |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 849 | } else { |
Duane Griffin | f33219b | 2009-01-08 22:43:49 +0000 | [diff] [blame] | 850 | memcpy(&ufs_inode->ui_u2.ui_symlink, ufsi->i_u1.i_symlink, |
| 851 | sizeof(ufs_inode->ui_u2.ui_symlink)); |
Evgeniy Dushistov | 3313e29 | 2007-02-12 00:54:31 -0800 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | if (!inode->i_nlink) |
| 855 | memset (ufs_inode, 0, sizeof(struct ufs2_inode)); |
| 856 | UFSD("EXIT\n"); |
| 857 | } |
| 858 | |
| 859 | static int ufs_update_inode(struct inode * inode, int do_sync) |
| 860 | { |
| 861 | struct super_block *sb = inode->i_sb; |
| 862 | struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; |
| 863 | struct buffer_head * bh; |
| 864 | |
| 865 | UFSD("ENTER, ino %lu\n", inode->i_ino); |
| 866 | |
| 867 | if (inode->i_ino < UFS_ROOTINO || |
| 868 | inode->i_ino > (uspi->s_ncg * uspi->s_ipg)) { |
| 869 | ufs_warning (sb, "ufs_read_inode", "bad inode number (%lu)\n", inode->i_ino); |
| 870 | return -1; |
| 871 | } |
| 872 | |
| 873 | bh = sb_bread(sb, ufs_inotofsba(inode->i_ino)); |
| 874 | if (!bh) { |
| 875 | ufs_warning (sb, "ufs_read_inode", "unable to read inode %lu\n", inode->i_ino); |
| 876 | return -1; |
| 877 | } |
| 878 | if (uspi->fs_magic == UFS2_MAGIC) { |
| 879 | struct ufs2_inode *ufs2_inode = (struct ufs2_inode *)bh->b_data; |
| 880 | |
| 881 | ufs2_update_inode(inode, |
| 882 | ufs2_inode + ufs_inotofsbo(inode->i_ino)); |
| 883 | } else { |
| 884 | struct ufs_inode *ufs_inode = (struct ufs_inode *) bh->b_data; |
| 885 | |
| 886 | ufs1_update_inode(inode, ufs_inode + ufs_inotofsbo(inode->i_ino)); |
| 887 | } |
Al Viro | 010d331 | 2015-06-17 12:44:14 -0400 | [diff] [blame] | 888 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | mark_buffer_dirty(bh); |
| 890 | if (do_sync) |
| 891 | sync_dirty_buffer(bh); |
| 892 | brelse (bh); |
Al Viro | 010d331 | 2015-06-17 12:44:14 -0400 | [diff] [blame] | 893 | |
Evgeniy Dushistov | abf5d15 | 2006-06-25 05:47:24 -0700 | [diff] [blame] | 894 | UFSD("EXIT\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | return 0; |
| 896 | } |
| 897 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 898 | int ufs_write_inode(struct inode *inode, struct writeback_control *wbc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | { |
Al Viro | f3e0f3d | 2015-06-16 02:35:14 -0400 | [diff] [blame] | 900 | return ufs_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | int ufs_sync_inode (struct inode *inode) |
| 904 | { |
| 905 | return ufs_update_inode (inode, 1); |
| 906 | } |
| 907 | |
Al Viro | 58e8268 | 2010-06-05 19:40:56 -0400 | [diff] [blame] | 908 | void ufs_evict_inode(struct inode * inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | { |
Al Viro | 58e8268 | 2010-06-05 19:40:56 -0400 | [diff] [blame] | 910 | int want_delete = 0; |
| 911 | |
| 912 | if (!inode->i_nlink && !is_bad_inode(inode)) |
| 913 | want_delete = 1; |
Evgeniy Dushistov | 10e5dce | 2006-07-01 04:36:24 -0700 | [diff] [blame] | 914 | |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 915 | truncate_inode_pages_final(&inode->i_data); |
Al Viro | 58e8268 | 2010-06-05 19:40:56 -0400 | [diff] [blame] | 916 | if (want_delete) { |
Al Viro | 58e8268 | 2010-06-05 19:40:56 -0400 | [diff] [blame] | 917 | inode->i_size = 0; |
Al Viro | d622f167 | 2015-06-16 18:04:16 -0400 | [diff] [blame] | 918 | if (inode->i_blocks) |
| 919 | ufs_truncate_blocks(inode); |
Al Viro | 58e8268 | 2010-06-05 19:40:56 -0400 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | invalidate_inode_buffers(inode); |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 923 | clear_inode(inode); |
Al Viro | 58e8268 | 2010-06-05 19:40:56 -0400 | [diff] [blame] | 924 | |
Al Viro | f3e0f3d | 2015-06-16 02:35:14 -0400 | [diff] [blame] | 925 | if (want_delete) |
Alexey Khoroshilov | 9ef7db7 | 2014-09-02 11:40:17 +0400 | [diff] [blame] | 926 | ufs_free_inode(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | } |
Al Viro | 010d331 | 2015-06-17 12:44:14 -0400 | [diff] [blame] | 928 | |
Al Viro | a138b4b | 2015-06-18 02:18:54 -0400 | [diff] [blame] | 929 | struct to_free { |
| 930 | struct inode *inode; |
| 931 | u64 to; |
| 932 | unsigned count; |
| 933 | }; |
| 934 | |
| 935 | static inline void free_data(struct to_free *ctx, u64 from, unsigned count) |
| 936 | { |
| 937 | if (ctx->count && ctx->to != from) { |
| 938 | ufs_free_blocks(ctx->inode, ctx->to - ctx->count, ctx->count); |
| 939 | ctx->count = 0; |
| 940 | } |
| 941 | ctx->count += count; |
| 942 | ctx->to = from + count; |
| 943 | } |
| 944 | |
Al Viro | 010d331 | 2015-06-17 12:44:14 -0400 | [diff] [blame] | 945 | #define DIRECT_BLOCK ((inode->i_size + uspi->s_bsize - 1) >> uspi->s_bshift) |
| 946 | #define DIRECT_FRAGMENT ((inode->i_size + uspi->s_fsize - 1) >> uspi->s_fshift) |
| 947 | |
| 948 | static void ufs_trunc_direct(struct inode *inode) |
| 949 | { |
| 950 | struct ufs_inode_info *ufsi = UFS_I(inode); |
| 951 | struct super_block * sb; |
| 952 | struct ufs_sb_private_info * uspi; |
| 953 | void *p; |
| 954 | u64 frag1, frag2, frag3, frag4, block1, block2; |
Al Viro | a138b4b | 2015-06-18 02:18:54 -0400 | [diff] [blame] | 955 | struct to_free ctx = {.inode = inode}; |
Al Viro | 010d331 | 2015-06-17 12:44:14 -0400 | [diff] [blame] | 956 | unsigned i, tmp; |
| 957 | |
| 958 | UFSD("ENTER: ino %lu\n", inode->i_ino); |
| 959 | |
| 960 | sb = inode->i_sb; |
| 961 | uspi = UFS_SB(sb)->s_uspi; |
| 962 | |
Al Viro | 010d331 | 2015-06-17 12:44:14 -0400 | [diff] [blame] | 963 | frag1 = DIRECT_FRAGMENT; |
| 964 | frag4 = min_t(u64, UFS_NDIR_FRAGMENT, ufsi->i_lastfrag); |
| 965 | frag2 = ((frag1 & uspi->s_fpbmask) ? ((frag1 | uspi->s_fpbmask) + 1) : frag1); |
| 966 | frag3 = frag4 & ~uspi->s_fpbmask; |
| 967 | block1 = block2 = 0; |
| 968 | if (frag2 > frag3) { |
| 969 | frag2 = frag4; |
| 970 | frag3 = frag4 = 0; |
| 971 | } else if (frag2 < frag3) { |
| 972 | block1 = ufs_fragstoblks (frag2); |
| 973 | block2 = ufs_fragstoblks (frag3); |
| 974 | } |
| 975 | |
| 976 | UFSD("ino %lu, frag1 %llu, frag2 %llu, block1 %llu, block2 %llu," |
| 977 | " frag3 %llu, frag4 %llu\n", inode->i_ino, |
| 978 | (unsigned long long)frag1, (unsigned long long)frag2, |
| 979 | (unsigned long long)block1, (unsigned long long)block2, |
| 980 | (unsigned long long)frag3, (unsigned long long)frag4); |
| 981 | |
| 982 | if (frag1 >= frag2) |
| 983 | goto next1; |
| 984 | |
| 985 | /* |
| 986 | * Free first free fragments |
| 987 | */ |
| 988 | p = ufs_get_direct_data_ptr(uspi, ufsi, ufs_fragstoblks(frag1)); |
| 989 | tmp = ufs_data_ptr_to_cpu(sb, p); |
| 990 | if (!tmp ) |
| 991 | ufs_panic (sb, "ufs_trunc_direct", "internal error"); |
| 992 | frag2 -= frag1; |
| 993 | frag1 = ufs_fragnum (frag1); |
| 994 | |
| 995 | ufs_free_fragments(inode, tmp + frag1, frag2); |
Al Viro | 010d331 | 2015-06-17 12:44:14 -0400 | [diff] [blame] | 996 | |
| 997 | next1: |
| 998 | /* |
| 999 | * Free whole blocks |
| 1000 | */ |
| 1001 | for (i = block1 ; i < block2; i++) { |
| 1002 | p = ufs_get_direct_data_ptr(uspi, ufsi, i); |
| 1003 | tmp = ufs_data_ptr_to_cpu(sb, p); |
| 1004 | if (!tmp) |
| 1005 | continue; |
| 1006 | write_seqlock(&ufsi->meta_lock); |
| 1007 | ufs_data_ptr_clear(uspi, p); |
| 1008 | write_sequnlock(&ufsi->meta_lock); |
| 1009 | |
Al Viro | a138b4b | 2015-06-18 02:18:54 -0400 | [diff] [blame] | 1010 | free_data(&ctx, tmp, uspi->s_fpb); |
Al Viro | 010d331 | 2015-06-17 12:44:14 -0400 | [diff] [blame] | 1011 | } |
| 1012 | |
Al Viro | a138b4b | 2015-06-18 02:18:54 -0400 | [diff] [blame] | 1013 | free_data(&ctx, 0, 0); |
Al Viro | 010d331 | 2015-06-17 12:44:14 -0400 | [diff] [blame] | 1014 | |
| 1015 | if (frag3 >= frag4) |
| 1016 | goto next3; |
| 1017 | |
| 1018 | /* |
| 1019 | * Free last free fragments |
| 1020 | */ |
| 1021 | p = ufs_get_direct_data_ptr(uspi, ufsi, ufs_fragstoblks(frag3)); |
| 1022 | tmp = ufs_data_ptr_to_cpu(sb, p); |
| 1023 | if (!tmp ) |
| 1024 | ufs_panic(sb, "ufs_truncate_direct", "internal error"); |
| 1025 | frag4 = ufs_fragnum (frag4); |
| 1026 | write_seqlock(&ufsi->meta_lock); |
| 1027 | ufs_data_ptr_clear(uspi, p); |
| 1028 | write_sequnlock(&ufsi->meta_lock); |
| 1029 | |
| 1030 | ufs_free_fragments (inode, tmp, frag4); |
Al Viro | 010d331 | 2015-06-17 12:44:14 -0400 | [diff] [blame] | 1031 | next3: |
| 1032 | |
| 1033 | UFSD("EXIT: ino %lu\n", inode->i_ino); |
| 1034 | } |
| 1035 | |
Al Viro | 163073d | 2015-06-18 20:07:08 -0400 | [diff] [blame] | 1036 | static void free_full_branch(struct inode *inode, u64 ind_block, int depth) |
Al Viro | 010d331 | 2015-06-17 12:44:14 -0400 | [diff] [blame] | 1037 | { |
| 1038 | struct super_block *sb = inode->i_sb; |
| 1039 | struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; |
Al Viro | 163073d | 2015-06-18 20:07:08 -0400 | [diff] [blame] | 1040 | struct ufs_buffer_head *ubh = ubh_bread(sb, ind_block, uspi->s_bsize); |
Al Viro | 18ca51d | 2015-06-18 13:45:07 -0400 | [diff] [blame] | 1041 | unsigned i; |
Al Viro | 010d331 | 2015-06-17 12:44:14 -0400 | [diff] [blame] | 1042 | |
Al Viro | 163073d | 2015-06-18 20:07:08 -0400 | [diff] [blame] | 1043 | if (!ubh) |
Al Viro | 010d331 | 2015-06-17 12:44:14 -0400 | [diff] [blame] | 1044 | return; |
Al Viro | 010d331 | 2015-06-17 12:44:14 -0400 | [diff] [blame] | 1045 | |
Al Viro | 9e0fbbd | 2015-06-18 15:33:47 -0400 | [diff] [blame] | 1046 | if (--depth) { |
Al Viro | 163073d | 2015-06-18 20:07:08 -0400 | [diff] [blame] | 1047 | for (i = 0; i < uspi->s_apb; i++) { |
| 1048 | void *p = ubh_get_data_ptr(uspi, ubh, i); |
| 1049 | u64 block = ufs_data_ptr_to_cpu(sb, p); |
Al Viro | cc7231e | 2015-06-18 20:14:02 -0400 | [diff] [blame] | 1050 | if (block) |
Al Viro | 163073d | 2015-06-18 20:07:08 -0400 | [diff] [blame] | 1051 | free_full_branch(inode, block, depth); |
Al Viro | 6d1ebbc | 2015-06-18 17:11:49 -0400 | [diff] [blame] | 1052 | } |
| 1053 | } else { |
| 1054 | struct to_free ctx = {.inode = inode}; |
| 1055 | |
| 1056 | for (i = 0; i < uspi->s_apb; i++) { |
Al Viro | 163073d | 2015-06-18 20:07:08 -0400 | [diff] [blame] | 1057 | void *p = ubh_get_data_ptr(uspi, ubh, i); |
| 1058 | u64 block = ufs_data_ptr_to_cpu(sb, p); |
Al Viro | cc7231e | 2015-06-18 20:14:02 -0400 | [diff] [blame] | 1059 | if (block) |
Al Viro | 163073d | 2015-06-18 20:07:08 -0400 | [diff] [blame] | 1060 | free_data(&ctx, block, uspi->s_fpb); |
Al Viro | 6d1ebbc | 2015-06-18 17:11:49 -0400 | [diff] [blame] | 1061 | } |
| 1062 | free_data(&ctx, 0, 0); |
| 1063 | } |
Al Viro | 6d1ebbc | 2015-06-18 17:11:49 -0400 | [diff] [blame] | 1064 | |
| 1065 | ubh_bforget(ubh); |
Al Viro | 163073d | 2015-06-18 20:07:08 -0400 | [diff] [blame] | 1066 | ufs_free_blocks(inode, ind_block, uspi->s_fpb); |
Al Viro | 6d1ebbc | 2015-06-18 17:11:49 -0400 | [diff] [blame] | 1067 | } |
| 1068 | |
Al Viro | 7b4e4f7 | 2015-06-18 19:13:02 -0400 | [diff] [blame] | 1069 | static void free_branch_tail(struct inode *inode, unsigned from, struct ufs_buffer_head *ubh, int depth) |
Al Viro | 6d1ebbc | 2015-06-18 17:11:49 -0400 | [diff] [blame] | 1070 | { |
| 1071 | struct super_block *sb = inode->i_sb; |
| 1072 | struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; |
Al Viro | 6d1ebbc | 2015-06-18 17:11:49 -0400 | [diff] [blame] | 1073 | unsigned i; |
| 1074 | |
Al Viro | 6d1ebbc | 2015-06-18 17:11:49 -0400 | [diff] [blame] | 1075 | if (--depth) { |
Al Viro | 7b4e4f7 | 2015-06-18 19:13:02 -0400 | [diff] [blame] | 1076 | for (i = from; i < uspi->s_apb ; i++) { |
Al Viro | 163073d | 2015-06-18 20:07:08 -0400 | [diff] [blame] | 1077 | void *p = ubh_get_data_ptr(uspi, ubh, i); |
| 1078 | u64 block = ufs_data_ptr_to_cpu(sb, p); |
| 1079 | if (block) { |
| 1080 | write_seqlock(&UFS_I(inode)->meta_lock); |
| 1081 | ufs_data_ptr_clear(uspi, p); |
| 1082 | write_sequnlock(&UFS_I(inode)->meta_lock); |
| 1083 | ubh_mark_buffer_dirty(ubh); |
| 1084 | free_full_branch(inode, block, depth); |
| 1085 | } |
Al Viro | a965742 | 2015-06-18 16:13:56 -0400 | [diff] [blame] | 1086 | } |
Al Viro | 9e0fbbd | 2015-06-18 15:33:47 -0400 | [diff] [blame] | 1087 | } else { |
Al Viro | a138b4b | 2015-06-18 02:18:54 -0400 | [diff] [blame] | 1088 | struct to_free ctx = {.inode = inode}; |
Al Viro | 9e0fbbd | 2015-06-18 15:33:47 -0400 | [diff] [blame] | 1089 | |
| 1090 | for (i = from; i < uspi->s_apb; i++) { |
Al Viro | 163073d | 2015-06-18 20:07:08 -0400 | [diff] [blame] | 1091 | void *p = ubh_get_data_ptr(uspi, ubh, i); |
| 1092 | u64 block = ufs_data_ptr_to_cpu(sb, p); |
| 1093 | if (block) { |
| 1094 | write_seqlock(&UFS_I(inode)->meta_lock); |
| 1095 | ufs_data_ptr_clear(uspi, p); |
| 1096 | write_sequnlock(&UFS_I(inode)->meta_lock); |
| 1097 | ubh_mark_buffer_dirty(ubh); |
| 1098 | free_data(&ctx, block, uspi->s_fpb); |
Al Viro | 163073d | 2015-06-18 20:07:08 -0400 | [diff] [blame] | 1099 | } |
Al Viro | 9e0fbbd | 2015-06-18 15:33:47 -0400 | [diff] [blame] | 1100 | } |
Al Viro | a138b4b | 2015-06-18 02:18:54 -0400 | [diff] [blame] | 1101 | free_data(&ctx, 0, 0); |
Al Viro | 010d331 | 2015-06-17 12:44:14 -0400 | [diff] [blame] | 1102 | } |
Al Viro | 9e0fbbd | 2015-06-18 15:33:47 -0400 | [diff] [blame] | 1103 | if (IS_SYNC(inode) && ubh_buffer_dirty(ubh)) |
| 1104 | ubh_sync_block(ubh); |
| 1105 | ubh_brelse(ubh); |
Al Viro | 010d331 | 2015-06-17 12:44:14 -0400 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | static int ufs_alloc_lastblock(struct inode *inode, loff_t size) |
| 1109 | { |
| 1110 | int err = 0; |
| 1111 | struct super_block *sb = inode->i_sb; |
| 1112 | struct address_space *mapping = inode->i_mapping; |
| 1113 | struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; |
| 1114 | unsigned i, end; |
| 1115 | sector_t lastfrag; |
| 1116 | struct page *lastpage; |
| 1117 | struct buffer_head *bh; |
| 1118 | u64 phys64; |
| 1119 | |
| 1120 | lastfrag = (size + uspi->s_fsize - 1) >> uspi->s_fshift; |
| 1121 | |
| 1122 | if (!lastfrag) |
| 1123 | goto out; |
| 1124 | |
| 1125 | lastfrag--; |
| 1126 | |
| 1127 | lastpage = ufs_get_locked_page(mapping, lastfrag >> |
| 1128 | (PAGE_CACHE_SHIFT - inode->i_blkbits)); |
| 1129 | if (IS_ERR(lastpage)) { |
| 1130 | err = -EIO; |
| 1131 | goto out; |
| 1132 | } |
| 1133 | |
| 1134 | end = lastfrag & ((1 << (PAGE_CACHE_SHIFT - inode->i_blkbits)) - 1); |
| 1135 | bh = page_buffers(lastpage); |
| 1136 | for (i = 0; i < end; ++i) |
| 1137 | bh = bh->b_this_page; |
| 1138 | |
| 1139 | |
| 1140 | err = ufs_getfrag_block(inode, lastfrag, bh, 1); |
| 1141 | |
| 1142 | if (unlikely(err)) |
| 1143 | goto out_unlock; |
| 1144 | |
| 1145 | if (buffer_new(bh)) { |
| 1146 | clear_buffer_new(bh); |
| 1147 | unmap_underlying_metadata(bh->b_bdev, |
| 1148 | bh->b_blocknr); |
| 1149 | /* |
| 1150 | * we do not zeroize fragment, because of |
| 1151 | * if it maped to hole, it already contains zeroes |
| 1152 | */ |
| 1153 | set_buffer_uptodate(bh); |
| 1154 | mark_buffer_dirty(bh); |
| 1155 | set_page_dirty(lastpage); |
| 1156 | } |
| 1157 | |
| 1158 | if (lastfrag >= UFS_IND_FRAGMENT) { |
| 1159 | end = uspi->s_fpb - ufs_fragnum(lastfrag) - 1; |
| 1160 | phys64 = bh->b_blocknr + 1; |
| 1161 | for (i = 0; i < end; ++i) { |
| 1162 | bh = sb_getblk(sb, i + phys64); |
| 1163 | lock_buffer(bh); |
| 1164 | memset(bh->b_data, 0, sb->s_blocksize); |
| 1165 | set_buffer_uptodate(bh); |
| 1166 | mark_buffer_dirty(bh); |
| 1167 | unlock_buffer(bh); |
| 1168 | sync_dirty_buffer(bh); |
| 1169 | brelse(bh); |
| 1170 | } |
| 1171 | } |
| 1172 | out_unlock: |
| 1173 | ufs_put_locked_page(lastpage); |
| 1174 | out: |
| 1175 | return err; |
| 1176 | } |
| 1177 | |
| 1178 | static void __ufs_truncate_blocks(struct inode *inode) |
| 1179 | { |
| 1180 | struct ufs_inode_info *ufsi = UFS_I(inode); |
| 1181 | struct super_block *sb = inode->i_sb; |
| 1182 | struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi; |
Al Viro | 7bad593 | 2015-06-18 14:21:09 -0400 | [diff] [blame] | 1183 | unsigned offsets[4]; |
Al Viro | 31cd043 | 2015-06-17 01:10:03 -0400 | [diff] [blame] | 1184 | int depth = ufs_block_to_path(inode, DIRECT_BLOCK, offsets); |
Al Viro | 6775e24 | 2015-06-18 14:55:50 -0400 | [diff] [blame] | 1185 | int depth2; |
Al Viro | 4243273 | 2015-06-18 15:47:17 -0400 | [diff] [blame] | 1186 | unsigned i; |
Al Viro | 7b4e4f7 | 2015-06-18 19:13:02 -0400 | [diff] [blame] | 1187 | struct ufs_buffer_head *ubh[3]; |
| 1188 | void *p; |
| 1189 | u64 block; |
Al Viro | 6775e24 | 2015-06-18 14:55:50 -0400 | [diff] [blame] | 1190 | |
| 1191 | if (!depth) |
| 1192 | return; |
| 1193 | |
| 1194 | /* find the last non-zero in offsets[] */ |
| 1195 | for (depth2 = depth - 1; depth2; depth2--) |
| 1196 | if (offsets[depth2]) |
| 1197 | break; |
Al Viro | 010d331 | 2015-06-17 12:44:14 -0400 | [diff] [blame] | 1198 | |
| 1199 | mutex_lock(&ufsi->truncate_mutex); |
Al Viro | 4243273 | 2015-06-18 15:47:17 -0400 | [diff] [blame] | 1200 | if (depth == 1) { |
Al Viro | 31cd043 | 2015-06-17 01:10:03 -0400 | [diff] [blame] | 1201 | ufs_trunc_direct(inode); |
Al Viro | 4243273 | 2015-06-18 15:47:17 -0400 | [diff] [blame] | 1202 | offsets[0] = UFS_IND_BLOCK; |
| 1203 | } else { |
Al Viro | 7b4e4f7 | 2015-06-18 19:13:02 -0400 | [diff] [blame] | 1204 | /* get the blocks that should be partially emptied */ |
| 1205 | p = ufs_get_direct_data_ptr(uspi, ufsi, offsets[0]); |
| 1206 | for (i = 0; i < depth2; i++) { |
| 1207 | offsets[i]++; /* next branch is fully freed */ |
| 1208 | block = ufs_data_ptr_to_cpu(sb, p); |
| 1209 | if (!block) |
| 1210 | break; |
| 1211 | ubh[i] = ubh_bread(sb, block, uspi->s_bsize); |
| 1212 | if (!ubh[i]) { |
| 1213 | write_seqlock(&ufsi->meta_lock); |
| 1214 | ufs_data_ptr_clear(uspi, p); |
| 1215 | write_sequnlock(&ufsi->meta_lock); |
| 1216 | break; |
| 1217 | } |
| 1218 | p = ubh_get_data_ptr(uspi, ubh[i], offsets[i + 1]); |
| 1219 | } |
Al Viro | f53bd14 | 2015-06-18 20:17:32 -0400 | [diff] [blame] | 1220 | while (i--) |
Al Viro | 7b4e4f7 | 2015-06-18 19:13:02 -0400 | [diff] [blame] | 1221 | free_branch_tail(inode, offsets[i + 1], ubh[i], depth - i - 1); |
Al Viro | 31cd043 | 2015-06-17 01:10:03 -0400 | [diff] [blame] | 1222 | } |
Al Viro | 4243273 | 2015-06-18 15:47:17 -0400 | [diff] [blame] | 1223 | for (i = offsets[0]; i <= UFS_TIND_BLOCK; i++) { |
Al Viro | 163073d | 2015-06-18 20:07:08 -0400 | [diff] [blame] | 1224 | p = ufs_get_direct_data_ptr(uspi, ufsi, i); |
| 1225 | block = ufs_data_ptr_to_cpu(sb, p); |
| 1226 | if (block) { |
| 1227 | write_seqlock(&ufsi->meta_lock); |
| 1228 | ufs_data_ptr_clear(uspi, p); |
| 1229 | write_sequnlock(&ufsi->meta_lock); |
| 1230 | free_full_branch(inode, block, i - UFS_IND_BLOCK + 1); |
| 1231 | } |
Al Viro | 4243273 | 2015-06-18 15:47:17 -0400 | [diff] [blame] | 1232 | } |
Al Viro | 010d331 | 2015-06-17 12:44:14 -0400 | [diff] [blame] | 1233 | ufsi->i_lastfrag = DIRECT_FRAGMENT; |
Al Viro | b6eede0 | 2015-06-18 20:09:39 -0400 | [diff] [blame] | 1234 | mark_inode_dirty(inode); |
Al Viro | 010d331 | 2015-06-17 12:44:14 -0400 | [diff] [blame] | 1235 | mutex_unlock(&ufsi->truncate_mutex); |
| 1236 | } |
| 1237 | |
| 1238 | static int ufs_truncate(struct inode *inode, loff_t size) |
| 1239 | { |
| 1240 | int err = 0; |
| 1241 | |
| 1242 | UFSD("ENTER: ino %lu, i_size: %llu, old_i_size: %llu\n", |
| 1243 | inode->i_ino, (unsigned long long)size, |
| 1244 | (unsigned long long)i_size_read(inode)); |
| 1245 | |
| 1246 | if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || |
| 1247 | S_ISLNK(inode->i_mode))) |
| 1248 | return -EINVAL; |
| 1249 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) |
| 1250 | return -EPERM; |
| 1251 | |
| 1252 | err = ufs_alloc_lastblock(inode, size); |
| 1253 | |
| 1254 | if (err) |
| 1255 | goto out; |
| 1256 | |
| 1257 | block_truncate_page(inode->i_mapping, size, ufs_getfrag_block); |
| 1258 | |
| 1259 | truncate_setsize(inode, size); |
| 1260 | |
| 1261 | __ufs_truncate_blocks(inode); |
| 1262 | inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC; |
| 1263 | mark_inode_dirty(inode); |
| 1264 | out: |
| 1265 | UFSD("EXIT: err %d\n", err); |
| 1266 | return err; |
| 1267 | } |
| 1268 | |
| 1269 | void ufs_truncate_blocks(struct inode *inode) |
| 1270 | { |
| 1271 | if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || |
| 1272 | S_ISLNK(inode->i_mode))) |
| 1273 | return; |
| 1274 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) |
| 1275 | return; |
| 1276 | __ufs_truncate_blocks(inode); |
| 1277 | } |
| 1278 | |
| 1279 | int ufs_setattr(struct dentry *dentry, struct iattr *attr) |
| 1280 | { |
| 1281 | struct inode *inode = d_inode(dentry); |
| 1282 | unsigned int ia_valid = attr->ia_valid; |
| 1283 | int error; |
| 1284 | |
| 1285 | error = inode_change_ok(inode, attr); |
| 1286 | if (error) |
| 1287 | return error; |
| 1288 | |
| 1289 | if (ia_valid & ATTR_SIZE && attr->ia_size != inode->i_size) { |
| 1290 | error = ufs_truncate(inode, attr->ia_size); |
| 1291 | if (error) |
| 1292 | return error; |
| 1293 | } |
| 1294 | |
| 1295 | setattr_copy(inode, attr); |
| 1296 | mark_inode_dirty(inode); |
| 1297 | return 0; |
| 1298 | } |
| 1299 | |
| 1300 | const struct inode_operations ufs_file_inode_operations = { |
| 1301 | .setattr = ufs_setattr, |
| 1302 | }; |