blob: 7cf33379fd467825d91957befaab253bcc78f4a1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
29#include <asm/system.h>
30
31#include <linux/errno.h>
32#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/time.h>
34#include <linux/stat.h>
35#include <linux/string.h>
36#include <linux/mm.h>
37#include <linux/smp_lock.h>
38#include <linux/buffer_head.h>
39
Mike Frysingere5420592008-02-08 04:21:31 -080040#include "ufs_fs.h"
Christoph Hellwigbcd6d4e2007-10-16 23:26:51 -070041#include "ufs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "swab.h"
43#include "util.h"
44
Adrian Bunk138bb682006-06-25 05:47:32 -070045static u64 ufs_frag_map(struct inode *inode, sector_t frag);
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047static int ufs_block_to_path(struct inode *inode, sector_t i_block, sector_t offsets[4])
48{
49 struct ufs_sb_private_info *uspi = UFS_SB(inode->i_sb)->s_uspi;
50 int ptrs = uspi->s_apb;
51 int ptrs_bits = uspi->s_apbshift;
52 const long direct_blocks = UFS_NDADDR,
53 indirect_blocks = ptrs,
54 double_blocks = (1 << (ptrs_bits * 2));
55 int n = 0;
56
57
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -070058 UFSD("ptrs=uspi->s_apb = %d,double_blocks=%ld \n",ptrs,double_blocks);
Roel Kluin37044c82009-06-17 16:26:28 -070059 if (i_block < direct_blocks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 offsets[n++] = i_block;
61 } else if ((i_block -= direct_blocks) < indirect_blocks) {
62 offsets[n++] = UFS_IND_BLOCK;
63 offsets[n++] = i_block;
64 } else if ((i_block -= indirect_blocks) < double_blocks) {
65 offsets[n++] = UFS_DIND_BLOCK;
66 offsets[n++] = i_block >> ptrs_bits;
67 offsets[n++] = i_block & (ptrs - 1);
68 } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
69 offsets[n++] = UFS_TIND_BLOCK;
70 offsets[n++] = i_block >> (ptrs_bits * 2);
71 offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
72 offsets[n++] = i_block & (ptrs - 1);
73 } else {
74 ufs_warning(inode->i_sb, "ufs_block_to_path", "block > big");
75 }
76 return n;
77}
78
79/*
80 * Returns the location of the fragment from
81 * the begining of the filesystem.
82 */
83
Adrian Bunk138bb682006-06-25 05:47:32 -070084static u64 ufs_frag_map(struct inode *inode, sector_t frag)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
86 struct ufs_inode_info *ufsi = UFS_I(inode);
87 struct super_block *sb = inode->i_sb;
88 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
89 u64 mask = (u64) uspi->s_apbmask>>uspi->s_fpbshift;
90 int shift = uspi->s_apbshift-uspi->s_fpbshift;
91 sector_t offsets[4], *p;
92 int depth = ufs_block_to_path(inode, frag >> uspi->s_fpbshift, offsets);
93 u64 ret = 0L;
94 __fs32 block;
95 __fs64 u2_block = 0L;
96 unsigned flags = UFS_SB(sb)->s_flags;
97 u64 temp = 0L;
98
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -070099 UFSD(": frag = %llu depth = %d\n", (unsigned long long)frag, depth);
Andrew Morton7256d812006-06-29 02:24:29 -0700100 UFSD(": uspi->s_fpbshift = %d ,uspi->s_apbmask = %x, mask=%llx\n",
101 uspi->s_fpbshift, uspi->s_apbmask,
102 (unsigned long long)mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 if (depth == 0)
105 return 0;
106
107 p = offsets;
108
109 lock_kernel();
110 if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
111 goto ufs2;
112
113 block = ufsi->i_u1.i_data[*p++];
114 if (!block)
115 goto out;
116 while (--depth) {
117 struct buffer_head *bh;
118 sector_t n = *p++;
119
120 bh = sb_bread(sb, uspi->s_sbbase + fs32_to_cpu(sb, block)+(n>>shift));
121 if (!bh)
122 goto out;
123 block = ((__fs32 *) bh->b_data)[n & mask];
124 brelse (bh);
125 if (!block)
126 goto out;
127 }
128 ret = (u64) (uspi->s_sbbase + fs32_to_cpu(sb, block) + (frag & uspi->s_fpbmask));
129 goto out;
130ufs2:
131 u2_block = ufsi->i_u1.u2_i_data[*p++];
132 if (!u2_block)
133 goto out;
134
135
136 while (--depth) {
137 struct buffer_head *bh;
138 sector_t n = *p++;
139
140
141 temp = (u64)(uspi->s_sbbase) + fs64_to_cpu(sb, u2_block);
142 bh = sb_bread(sb, temp +(u64) (n>>shift));
143 if (!bh)
144 goto out;
145 u2_block = ((__fs64 *)bh->b_data)[n & mask];
146 brelse(bh);
147 if (!u2_block)
148 goto out;
149 }
150 temp = (u64)uspi->s_sbbase + fs64_to_cpu(sb, u2_block);
151 ret = temp + (u64) (frag & uspi->s_fpbmask);
152
153out:
154 unlock_kernel();
155 return ret;
156}
157
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700158/**
159 * ufs_inode_getfrag() - allocate new fragment(s)
160 * @inode - pointer to inode
161 * @fragment - number of `fragment' which hold pointer
162 * to new allocated fragment(s)
163 * @new_fragment - number of new allocated fragment(s)
164 * @required - how many fragment(s) we require
165 * @err - we set it if something wrong
166 * @phys - pointer to where we save physical number of new allocated fragments,
167 * NULL if we allocate not data(indirect blocks for example).
168 * @new - we set it if we allocate new block
169 * @locked_page - for ufs_new_fragments()
170 */
171static struct buffer_head *
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800172ufs_inode_getfrag(struct inode *inode, u64 fragment,
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700173 sector_t new_fragment, unsigned int required, int *err,
174 long *phys, int *new, struct page *locked_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 struct ufs_inode_info *ufsi = UFS_I(inode);
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700177 struct super_block *sb = inode->i_sb;
178 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 struct buffer_head * result;
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800180 unsigned blockoff, lastblockoff;
181 u64 tmp, goal, lastfrag, block, lastblock;
182 void *p, *p2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800184 UFSD("ENTER, ino %lu, fragment %llu, new_fragment %llu, required %u, "
185 "metadata %d\n", inode->i_ino, (unsigned long long)fragment,
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700186 (unsigned long long)new_fragment, required, !phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 /* TODO : to be done for write support
189 if ( (flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
190 goto ufs2;
191 */
192
193 block = ufs_fragstoblks (fragment);
194 blockoff = ufs_fragnum (fragment);
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800195 p = ufs_get_direct_data_ptr(uspi, ufsi, block);
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 goal = 0;
198
199repeat:
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800200 tmp = ufs_data_ptr_to_cpu(sb, p);
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 lastfrag = ufsi->i_lastfrag;
203 if (tmp && fragment < lastfrag) {
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700204 if (!phys) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 result = sb_getblk(sb, uspi->s_sbbase + tmp + blockoff);
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800206 if (tmp == ufs_data_ptr_to_cpu(sb, p)) {
207 UFSD("EXIT, result %llu\n",
208 (unsigned long long)tmp + blockoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return result;
210 }
211 brelse (result);
212 goto repeat;
213 } else {
Evgeniy Dushistov4b25a372007-03-16 13:38:09 -0800214 *phys = uspi->s_sbbase + tmp + blockoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return NULL;
216 }
217 }
218
219 lastblock = ufs_fragstoblks (lastfrag);
220 lastblockoff = ufs_fragnum (lastfrag);
221 /*
222 * We will extend file into new block beyond last allocated block
223 */
224 if (lastblock < block) {
225 /*
226 * We must reallocate last allocated block
227 */
228 if (lastblockoff) {
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800229 p2 = ufs_get_direct_data_ptr(uspi, ufsi, lastblock);
230 tmp = ufs_new_fragments(inode, p2, lastfrag,
231 ufs_data_ptr_to_cpu(sb, p2),
232 uspi->s_fpb - lastblockoff,
233 err, locked_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 if (!tmp) {
235 if (lastfrag != ufsi->i_lastfrag)
236 goto repeat;
237 else
238 return NULL;
239 }
240 lastfrag = ufsi->i_lastfrag;
241
242 }
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800243 tmp = ufs_data_ptr_to_cpu(sb,
244 ufs_get_direct_data_ptr(uspi, ufsi,
245 lastblock));
Evgeniy Dushistovc37336b2006-08-27 01:23:45 -0700246 if (tmp)
247 goal = tmp + uspi->s_fpb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 tmp = ufs_new_fragments (inode, p, fragment - blockoff,
Evgeniy Dushistov6ef4d6b2006-06-25 05:47:20 -0700249 goal, required + blockoff,
Evgeniy Dushistova685e262007-01-29 13:19:54 -0800250 err,
251 phys != NULL ? locked_page : NULL);
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800252 } else if (lastblock == block) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 /*
254 * We will extend last allocated block
255 */
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800256 tmp = ufs_new_fragments(inode, p, fragment -
257 (blockoff - lastblockoff),
258 ufs_data_ptr_to_cpu(sb, p),
259 required + (blockoff - lastblockoff),
Evgeniy Dushistova685e262007-01-29 13:19:54 -0800260 err, phys != NULL ? locked_page : NULL);
Evgeniy Dushistovc37336b2006-08-27 01:23:45 -0700261 } else /* (lastblock > block) */ {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 /*
263 * We will allocate new block before last allocated block
264 */
Evgeniy Dushistovc37336b2006-08-27 01:23:45 -0700265 if (block) {
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800266 tmp = ufs_data_ptr_to_cpu(sb,
267 ufs_get_direct_data_ptr(uspi, ufsi, block - 1));
Evgeniy Dushistovc37336b2006-08-27 01:23:45 -0700268 if (tmp)
269 goal = tmp + uspi->s_fpb;
270 }
Evgeniy Dushistov6ef4d6b2006-06-25 05:47:20 -0700271 tmp = ufs_new_fragments(inode, p, fragment - blockoff,
Evgeniy Dushistova685e262007-01-29 13:19:54 -0800272 goal, uspi->s_fpb, err,
273 phys != NULL ? locked_page : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 }
275 if (!tmp) {
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800276 if ((!blockoff && ufs_data_ptr_to_cpu(sb, p)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 (blockoff && lastfrag != ufsi->i_lastfrag))
278 goto repeat;
279 *err = -ENOSPC;
280 return NULL;
281 }
282
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700283 if (!phys) {
Evgeniy Dushistov4b25a372007-03-16 13:38:09 -0800284 result = sb_getblk(sb, uspi->s_sbbase + tmp + blockoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 } else {
Evgeniy Dushistov4b25a372007-03-16 13:38:09 -0800286 *phys = uspi->s_sbbase + tmp + blockoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 result = NULL;
288 *err = 0;
289 *new = 1;
290 }
291
292 inode->i_ctime = CURRENT_TIME_SEC;
293 if (IS_SYNC(inode))
294 ufs_sync_inode (inode);
295 mark_inode_dirty(inode);
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800296 UFSD("EXIT, result %llu\n", (unsigned long long)tmp + blockoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return result;
298
299 /* This part : To be implemented ....
300 Required only for writing, not required for READ-ONLY.
301ufs2:
302
303 u2_block = ufs_fragstoblks(fragment);
304 u2_blockoff = ufs_fragnum(fragment);
305 p = ufsi->i_u1.u2_i_data + block;
306 goal = 0;
307
308repeat2:
309 tmp = fs32_to_cpu(sb, *p);
310 lastfrag = ufsi->i_lastfrag;
311
312 */
313}
314
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700315/**
316 * ufs_inode_getblock() - allocate new block
317 * @inode - pointer to inode
318 * @bh - pointer to block which hold "pointer" to new allocated block
319 * @fragment - number of `fragment' which hold pointer
320 * to new allocated block
321 * @new_fragment - number of new allocated fragment
322 * (block will hold this fragment and also uspi->s_fpb-1)
323 * @err - see ufs_inode_getfrag()
324 * @phys - see ufs_inode_getfrag()
325 * @new - see ufs_inode_getfrag()
326 * @locked_page - see ufs_inode_getfrag()
327 */
328static struct buffer_head *
329ufs_inode_getblock(struct inode *inode, struct buffer_head *bh,
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800330 u64 fragment, sector_t new_fragment, int *err,
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700331 long *phys, int *new, struct page *locked_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700333 struct super_block *sb = inode->i_sb;
334 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 struct buffer_head * result;
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800336 unsigned blockoff;
337 u64 tmp, goal, block;
338 void *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 block = ufs_fragstoblks (fragment);
341 blockoff = ufs_fragnum (fragment);
342
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800343 UFSD("ENTER, ino %lu, fragment %llu, new_fragment %llu, metadata %d\n",
344 inode->i_ino, (unsigned long long)fragment,
345 (unsigned long long)new_fragment, !phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 result = NULL;
348 if (!bh)
349 goto out;
350 if (!buffer_uptodate(bh)) {
351 ll_rw_block (READ, 1, &bh);
352 wait_on_buffer (bh);
353 if (!buffer_uptodate(bh))
354 goto out;
355 }
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800356 if (uspi->fs_magic == UFS2_MAGIC)
357 p = (__fs64 *)bh->b_data + block;
358 else
359 p = (__fs32 *)bh->b_data + block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360repeat:
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800361 tmp = ufs_data_ptr_to_cpu(sb, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (tmp) {
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700363 if (!phys) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 result = sb_getblk(sb, uspi->s_sbbase + tmp + blockoff);
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800365 if (tmp == ufs_data_ptr_to_cpu(sb, p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 goto out;
367 brelse (result);
368 goto repeat;
369 } else {
Evgeniy Dushistov4b25a372007-03-16 13:38:09 -0800370 *phys = uspi->s_sbbase + tmp + blockoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 goto out;
372 }
373 }
374
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800375 if (block && (uspi->fs_magic == UFS2_MAGIC ?
376 (tmp = fs64_to_cpu(sb, ((__fs64 *)bh->b_data)[block-1])) :
377 (tmp = fs32_to_cpu(sb, ((__fs32 *)bh->b_data)[block-1]))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 goal = tmp + uspi->s_fpb;
379 else
380 goal = bh->b_blocknr + uspi->s_fpb;
Evgeniy Dushistov6ef4d6b2006-06-25 05:47:20 -0700381 tmp = ufs_new_fragments(inode, p, ufs_blknum(new_fragment), goal,
382 uspi->s_fpb, err, locked_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (!tmp) {
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800384 if (ufs_data_ptr_to_cpu(sb, p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 goto repeat;
386 goto out;
387 }
388
Evgeniy Dushistovc9a27b52006-06-25 05:47:19 -0700389
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700390 if (!phys) {
Evgeniy Dushistov4b25a372007-03-16 13:38:09 -0800391 result = sb_getblk(sb, uspi->s_sbbase + tmp + blockoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 } else {
Evgeniy Dushistov4b25a372007-03-16 13:38:09 -0800393 *phys = uspi->s_sbbase + tmp + blockoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 *new = 1;
395 }
396
397 mark_buffer_dirty(bh);
398 if (IS_SYNC(inode))
399 sync_dirty_buffer(bh);
400 inode->i_ctime = CURRENT_TIME_SEC;
401 mark_inode_dirty(inode);
Evgeniy Dushistov54fb9962007-02-12 00:54:32 -0800402 UFSD("result %llu\n", (unsigned long long)tmp + blockoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403out:
404 brelse (bh);
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700405 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return result;
407}
408
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700409/**
410 * ufs_getfrag_bloc() - `get_block_t' function, interface between UFS and
411 * readpage, writepage and so on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 */
413
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700414int ufs_getfrag_block(struct inode *inode, sector_t fragment, struct buffer_head *bh_result, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
416 struct super_block * sb = inode->i_sb;
417 struct ufs_sb_private_info * uspi = UFS_SB(sb)->s_uspi;
418 struct buffer_head * bh;
419 int ret, err, new;
420 unsigned long ptr,phys;
421 u64 phys64 = 0;
422
423 if (!create) {
424 phys64 = ufs_frag_map(inode, fragment);
Andrew Morton7256d812006-06-29 02:24:29 -0700425 UFSD("phys64 = %llu\n", (unsigned long long)phys64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (phys64)
427 map_bh(bh_result, sb, phys64);
428 return 0;
429 }
430
431 /* This code entered only while writing ....? */
432
433 err = -EIO;
434 new = 0;
435 ret = 0;
436 bh = NULL;
437
438 lock_kernel();
439
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700440 UFSD("ENTER, ino %lu, fragment %llu\n", inode->i_ino, (unsigned long long)fragment);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (fragment >
442 ((UFS_NDADDR + uspi->s_apb + uspi->s_2apb + uspi->s_3apb)
443 << uspi->s_fpbshift))
444 goto abort_too_big;
445
446 err = 0;
447 ptr = fragment;
448
449 /*
450 * ok, these macros clean the logic up a bit and make
451 * it much more readable:
452 */
453#define GET_INODE_DATABLOCK(x) \
Evgeniy Dushistova685e262007-01-29 13:19:54 -0800454 ufs_inode_getfrag(inode, x, fragment, 1, &err, &phys, &new,\
455 bh_result->b_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456#define GET_INODE_PTR(x) \
Evgeniy Dushistova685e262007-01-29 13:19:54 -0800457 ufs_inode_getfrag(inode, x, fragment, uspi->s_fpb, &err, NULL, NULL,\
458 bh_result->b_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459#define GET_INDIRECT_DATABLOCK(x) \
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700460 ufs_inode_getblock(inode, bh, x, fragment, \
Evgeniy Dushistovd63b7092007-01-05 16:37:04 -0800461 &err, &phys, &new, bh_result->b_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462#define GET_INDIRECT_PTR(x) \
Evgeniy Dushistov022a6dc2006-06-25 05:47:27 -0700463 ufs_inode_getblock(inode, bh, x, fragment, \
Evgeniy Dushistovd63b7092007-01-05 16:37:04 -0800464 &err, NULL, NULL, NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 if (ptr < UFS_NDIR_FRAGMENT) {
467 bh = GET_INODE_DATABLOCK(ptr);
468 goto out;
469 }
470 ptr -= UFS_NDIR_FRAGMENT;
471 if (ptr < (1 << (uspi->s_apbshift + uspi->s_fpbshift))) {
472 bh = GET_INODE_PTR(UFS_IND_FRAGMENT + (ptr >> uspi->s_apbshift));
473 goto get_indirect;
474 }
475 ptr -= 1 << (uspi->s_apbshift + uspi->s_fpbshift);
476 if (ptr < (1 << (uspi->s_2apbshift + uspi->s_fpbshift))) {
477 bh = GET_INODE_PTR(UFS_DIND_FRAGMENT + (ptr >> uspi->s_2apbshift));
478 goto get_double;
479 }
480 ptr -= 1 << (uspi->s_2apbshift + uspi->s_fpbshift);
481 bh = GET_INODE_PTR(UFS_TIND_FRAGMENT + (ptr >> uspi->s_3apbshift));
482 bh = GET_INDIRECT_PTR((ptr >> uspi->s_2apbshift) & uspi->s_apbmask);
483get_double:
484 bh = GET_INDIRECT_PTR((ptr >> uspi->s_apbshift) & uspi->s_apbmask);
485get_indirect:
486 bh = GET_INDIRECT_DATABLOCK(ptr & uspi->s_apbmask);
487
488#undef GET_INODE_DATABLOCK
489#undef GET_INODE_PTR
490#undef GET_INDIRECT_DATABLOCK
491#undef GET_INDIRECT_PTR
492
493out:
494 if (err)
495 goto abort;
496 if (new)
497 set_buffer_new(bh_result);
498 map_bh(bh_result, sb, phys);
499abort:
500 unlock_kernel();
501 return err;
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503abort_too_big:
504 ufs_warning(sb, "ufs_get_block", "block > big");
505 goto abort;
506}
507
Adrian Bunk138bb682006-06-25 05:47:32 -0700508static struct buffer_head *ufs_getfrag(struct inode *inode,
509 unsigned int fragment,
510 int create, int *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
512 struct buffer_head dummy;
513 int error;
514
515 dummy.b_state = 0;
516 dummy.b_blocknr = -1000;
517 error = ufs_getfrag_block(inode, fragment, &dummy, create);
518 *err = error;
519 if (!error && buffer_mapped(&dummy)) {
520 struct buffer_head *bh;
521 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
522 if (buffer_new(&dummy)) {
523 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
524 set_buffer_uptodate(bh);
525 mark_buffer_dirty(bh);
526 }
527 return bh;
528 }
529 return NULL;
530}
531
532struct buffer_head * ufs_bread (struct inode * inode, unsigned fragment,
533 int create, int * err)
534{
535 struct buffer_head * bh;
536
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700537 UFSD("ENTER, ino %lu, fragment %u\n", inode->i_ino, fragment);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 bh = ufs_getfrag (inode, fragment, create, err);
539 if (!bh || buffer_uptodate(bh))
540 return bh;
541 ll_rw_block (READ, 1, &bh);
542 wait_on_buffer (bh);
543 if (buffer_uptodate(bh))
544 return bh;
545 brelse (bh);
546 *err = -EIO;
547 return NULL;
548}
549
550static int ufs_writepage(struct page *page, struct writeback_control *wbc)
551{
552 return block_write_full_page(page,ufs_getfrag_block,wbc);
553}
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555static int ufs_readpage(struct file *file, struct page *page)
556{
557 return block_read_full_page(page,ufs_getfrag_block);
558}
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700559
560int __ufs_write_begin(struct file *file, struct address_space *mapping,
561 loff_t pos, unsigned len, unsigned flags,
562 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700564 return block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
565 ufs_getfrag_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700567
568static int ufs_write_begin(struct file *file, struct address_space *mapping,
569 loff_t pos, unsigned len, unsigned flags,
570 struct page **pagep, void **fsdata)
571{
572 *pagep = NULL;
573 return __ufs_write_begin(file, mapping, pos, len, flags, pagep, fsdata);
574}
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576static sector_t ufs_bmap(struct address_space *mapping, sector_t block)
577{
578 return generic_block_bmap(mapping,block,ufs_getfrag_block);
579}
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700580
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700581const struct address_space_operations ufs_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 .readpage = ufs_readpage,
583 .writepage = ufs_writepage,
584 .sync_page = block_sync_page,
Nick Piggin82b9d1d2007-10-16 01:25:19 -0700585 .write_begin = ufs_write_begin,
586 .write_end = generic_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 .bmap = ufs_bmap
588};
589
Evgeniy Dushistov826843a2006-06-25 05:47:21 -0700590static void ufs_set_inode_ops(struct inode *inode)
591{
592 if (S_ISREG(inode->i_mode)) {
593 inode->i_op = &ufs_file_inode_operations;
594 inode->i_fop = &ufs_file_operations;
595 inode->i_mapping->a_ops = &ufs_aops;
596 } else if (S_ISDIR(inode->i_mode)) {
597 inode->i_op = &ufs_dir_inode_operations;
598 inode->i_fop = &ufs_dir_operations;
599 inode->i_mapping->a_ops = &ufs_aops;
600 } else if (S_ISLNK(inode->i_mode)) {
601 if (!inode->i_blocks)
602 inode->i_op = &ufs_fast_symlink_inode_operations;
603 else {
604 inode->i_op = &page_symlink_inode_operations;
605 inode->i_mapping->a_ops = &ufs_aops;
606 }
607 } else
608 init_special_inode(inode, inode->i_mode,
609 ufs_get_inode_dev(inode->i_sb, UFS_I(inode)));
610}
611
Evgeniy Dushistov07a0cfe2007-04-16 22:53:24 -0700612static int ufs1_read_inode(struct inode *inode, struct ufs_inode *ufs_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
614 struct ufs_inode_info *ufsi = UFS_I(inode);
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700615 struct super_block *sb = inode->i_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 mode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 /*
619 * Copy data to the in-core inode.
620 */
621 inode->i_mode = mode = fs16_to_cpu(sb, ufs_inode->ui_mode);
622 inode->i_nlink = fs16_to_cpu(sb, ufs_inode->ui_nlink);
Evgeniy Dushistov07a0cfe2007-04-16 22:53:24 -0700623 if (inode->i_nlink == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 ufs_error (sb, "ufs_read_inode", "inode %lu has zero nlink\n", inode->i_ino);
Evgeniy Dushistov07a0cfe2007-04-16 22:53:24 -0700625 return -1;
626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 /*
629 * Linux now has 32-bit uid and gid, so we can support EFT.
630 */
631 inode->i_uid = ufs_get_inode_uid(sb, ufs_inode);
632 inode->i_gid = ufs_get_inode_gid(sb, ufs_inode);
633
634 inode->i_size = fs64_to_cpu(sb, ufs_inode->ui_size);
635 inode->i_atime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_atime.tv_sec);
636 inode->i_ctime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_ctime.tv_sec);
637 inode->i_mtime.tv_sec = fs32_to_cpu(sb, ufs_inode->ui_mtime.tv_sec);
638 inode->i_mtime.tv_nsec = 0;
639 inode->i_atime.tv_nsec = 0;
640 inode->i_ctime.tv_nsec = 0;
641 inode->i_blocks = fs32_to_cpu(sb, ufs_inode->ui_blocks);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800642 inode->i_generation = fs32_to_cpu(sb, ufs_inode->ui_gen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 ufsi->i_flags = fs32_to_cpu(sb, ufs_inode->ui_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 ufsi->i_shadow = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_shadow);
645 ufsi->i_oeftflag = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_oeftflag);
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 if (S_ISCHR(mode) || S_ISBLK(mode) || inode->i_blocks) {
Duane Griffinf33219b2009-01-08 22:43:49 +0000649 memcpy(ufsi->i_u1.i_data, &ufs_inode->ui_u2.ui_addr,
650 sizeof(ufs_inode->ui_u2.ui_addr));
Evgeniy Dushistovdd187a22006-06-25 05:47:25 -0700651 } else {
Duane Griffinf33219b2009-01-08 22:43:49 +0000652 memcpy(ufsi->i_u1.i_symlink, ufs_inode->ui_u2.ui_symlink,
Duane Griffinb12903f2009-01-08 22:43:50 +0000653 sizeof(ufs_inode->ui_u2.ui_symlink) - 1);
654 ufsi->i_u1.i_symlink[sizeof(ufs_inode->ui_u2.ui_symlink) - 1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
Evgeniy Dushistov07a0cfe2007-04-16 22:53:24 -0700656 return 0;
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700657}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Evgeniy Dushistov07a0cfe2007-04-16 22:53:24 -0700659static int ufs2_read_inode(struct inode *inode, struct ufs2_inode *ufs2_inode)
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700660{
661 struct ufs_inode_info *ufsi = UFS_I(inode);
662 struct super_block *sb = inode->i_sb;
663 mode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700665 UFSD("Reading ufs2 inode, ino %lu\n", inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 /*
667 * Copy data to the in-core inode.
668 */
669 inode->i_mode = mode = fs16_to_cpu(sb, ufs2_inode->ui_mode);
670 inode->i_nlink = fs16_to_cpu(sb, ufs2_inode->ui_nlink);
Evgeniy Dushistov07a0cfe2007-04-16 22:53:24 -0700671 if (inode->i_nlink == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 ufs_error (sb, "ufs_read_inode", "inode %lu has zero nlink\n", inode->i_ino);
Evgeniy Dushistov07a0cfe2007-04-16 22:53:24 -0700673 return -1;
674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 /*
677 * Linux now has 32-bit uid and gid, so we can support EFT.
678 */
679 inode->i_uid = fs32_to_cpu(sb, ufs2_inode->ui_uid);
680 inode->i_gid = fs32_to_cpu(sb, ufs2_inode->ui_gid);
681
682 inode->i_size = fs64_to_cpu(sb, ufs2_inode->ui_size);
Evgeniy Dushistov21898502007-03-16 13:38:07 -0800683 inode->i_atime.tv_sec = fs64_to_cpu(sb, ufs2_inode->ui_atime);
684 inode->i_ctime.tv_sec = fs64_to_cpu(sb, ufs2_inode->ui_ctime);
685 inode->i_mtime.tv_sec = fs64_to_cpu(sb, ufs2_inode->ui_mtime);
686 inode->i_atime.tv_nsec = fs32_to_cpu(sb, ufs2_inode->ui_atimensec);
687 inode->i_ctime.tv_nsec = fs32_to_cpu(sb, ufs2_inode->ui_ctimensec);
688 inode->i_mtime.tv_nsec = fs32_to_cpu(sb, ufs2_inode->ui_mtimensec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 inode->i_blocks = fs64_to_cpu(sb, ufs2_inode->ui_blocks);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800690 inode->i_generation = fs32_to_cpu(sb, ufs2_inode->ui_gen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 ufsi->i_flags = fs32_to_cpu(sb, ufs2_inode->ui_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 /*
693 ufsi->i_shadow = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_shadow);
694 ufsi->i_oeftflag = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_oeftflag);
695 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 if (S_ISCHR(mode) || S_ISBLK(mode) || inode->i_blocks) {
Duane Griffinf33219b2009-01-08 22:43:49 +0000698 memcpy(ufsi->i_u1.u2_i_data, &ufs2_inode->ui_u2.ui_addr,
699 sizeof(ufs2_inode->ui_u2.ui_addr));
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700700 } else {
Duane Griffinf33219b2009-01-08 22:43:49 +0000701 memcpy(ufsi->i_u1.i_symlink, ufs2_inode->ui_u2.ui_symlink,
Duane Griffinb12903f2009-01-08 22:43:50 +0000702 sizeof(ufs2_inode->ui_u2.ui_symlink) - 1);
703 ufsi->i_u1.i_symlink[sizeof(ufs2_inode->ui_u2.ui_symlink) - 1] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
Evgeniy Dushistov07a0cfe2007-04-16 22:53:24 -0700705 return 0;
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700706}
707
David Howellsb55c4602008-02-07 00:15:48 -0800708struct inode *ufs_iget(struct super_block *sb, unsigned long ino)
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700709{
David Howellsb55c4602008-02-07 00:15:48 -0800710 struct ufs_inode_info *ufsi;
711 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700712 struct buffer_head * bh;
David Howellsb55c4602008-02-07 00:15:48 -0800713 struct inode *inode;
Evgeniy Dushistov07a0cfe2007-04-16 22:53:24 -0700714 int err;
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700715
David Howellsb55c4602008-02-07 00:15:48 -0800716 UFSD("ENTER, ino %lu\n", ino);
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700717
David Howellsb55c4602008-02-07 00:15:48 -0800718 if (ino < UFS_ROOTINO || ino > (uspi->s_ncg * uspi->s_ipg)) {
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700719 ufs_warning(sb, "ufs_read_inode", "bad inode number (%lu)\n",
David Howellsb55c4602008-02-07 00:15:48 -0800720 ino);
721 return ERR_PTR(-EIO);
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700722 }
723
David Howellsb55c4602008-02-07 00:15:48 -0800724 inode = iget_locked(sb, ino);
725 if (!inode)
726 return ERR_PTR(-ENOMEM);
727 if (!(inode->i_state & I_NEW))
728 return inode;
729
730 ufsi = UFS_I(inode);
731
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700732 bh = sb_bread(sb, uspi->s_sbbase + ufs_inotofsba(inode->i_ino));
733 if (!bh) {
734 ufs_warning(sb, "ufs_read_inode", "unable to read inode %lu\n",
735 inode->i_ino);
736 goto bad_inode;
737 }
738 if ((UFS_SB(sb)->s_flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) {
739 struct ufs2_inode *ufs2_inode = (struct ufs2_inode *)bh->b_data;
740
Evgeniy Dushistov07a0cfe2007-04-16 22:53:24 -0700741 err = ufs2_read_inode(inode,
742 ufs2_inode + ufs_inotofsbo(inode->i_ino));
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700743 } else {
744 struct ufs_inode *ufs_inode = (struct ufs_inode *)bh->b_data;
745
Evgeniy Dushistov07a0cfe2007-04-16 22:53:24 -0700746 err = ufs1_read_inode(inode,
747 ufs_inode + ufs_inotofsbo(inode->i_ino));
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700748 }
749
Evgeniy Dushistov07a0cfe2007-04-16 22:53:24 -0700750 if (err)
751 goto bad_inode;
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700752 inode->i_version++;
753 ufsi->i_lastfrag =
754 (inode->i_size + uspi->s_fsize - 1) >> uspi->s_fshift;
755 ufsi->i_dir_start_lookup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 ufsi->i_osync = 0;
757
Evgeniy Dushistov826843a2006-06-25 05:47:21 -0700758 ufs_set_inode_ops(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 brelse(bh);
761
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700762 UFSD("EXIT\n");
David Howellsb55c4602008-02-07 00:15:48 -0800763 unlock_new_inode(inode);
764 return inode;
Evgeniy Dushistov05f225d2006-06-27 02:53:59 -0700765
766bad_inode:
David Howellsb55c4602008-02-07 00:15:48 -0800767 iget_failed(inode);
768 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}
770
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800771static void ufs1_update_inode(struct inode *inode, struct ufs_inode *ufs_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800773 struct super_block *sb = inode->i_sb;
774 struct ufs_inode_info *ufsi = UFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 ufs_inode->ui_mode = cpu_to_fs16(sb, inode->i_mode);
777 ufs_inode->ui_nlink = cpu_to_fs16(sb, inode->i_nlink);
778
779 ufs_set_inode_uid(sb, ufs_inode, inode->i_uid);
780 ufs_set_inode_gid(sb, ufs_inode, inode->i_gid);
781
782 ufs_inode->ui_size = cpu_to_fs64(sb, inode->i_size);
783 ufs_inode->ui_atime.tv_sec = cpu_to_fs32(sb, inode->i_atime.tv_sec);
784 ufs_inode->ui_atime.tv_usec = 0;
785 ufs_inode->ui_ctime.tv_sec = cpu_to_fs32(sb, inode->i_ctime.tv_sec);
786 ufs_inode->ui_ctime.tv_usec = 0;
787 ufs_inode->ui_mtime.tv_sec = cpu_to_fs32(sb, inode->i_mtime.tv_sec);
788 ufs_inode->ui_mtime.tv_usec = 0;
789 ufs_inode->ui_blocks = cpu_to_fs32(sb, inode->i_blocks);
790 ufs_inode->ui_flags = cpu_to_fs32(sb, ufsi->i_flags);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800791 ufs_inode->ui_gen = cpu_to_fs32(sb, inode->i_generation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800793 if ((UFS_SB(sb)->s_flags & UFS_UID_MASK) == UFS_UID_EFT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 ufs_inode->ui_u3.ui_sun.ui_shadow = cpu_to_fs32(sb, ufsi->i_shadow);
795 ufs_inode->ui_u3.ui_sun.ui_oeftflag = cpu_to_fs32(sb, ufsi->i_oeftflag);
796 }
797
798 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
799 /* ufs_inode->ui_u2.ui_addr.ui_db[0] = cpu_to_fs32(sb, inode->i_rdev); */
800 ufs_inode->ui_u2.ui_addr.ui_db[0] = ufsi->i_u1.i_data[0];
801 } else if (inode->i_blocks) {
Duane Griffinf33219b2009-01-08 22:43:49 +0000802 memcpy(&ufs_inode->ui_u2.ui_addr, ufsi->i_u1.i_data,
803 sizeof(ufs_inode->ui_u2.ui_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
805 else {
Duane Griffinf33219b2009-01-08 22:43:49 +0000806 memcpy(&ufs_inode->ui_u2.ui_symlink, ufsi->i_u1.i_symlink,
807 sizeof(ufs_inode->ui_u2.ui_symlink));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
809
810 if (!inode->i_nlink)
811 memset (ufs_inode, 0, sizeof(struct ufs_inode));
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800812}
813
814static void ufs2_update_inode(struct inode *inode, struct ufs2_inode *ufs_inode)
815{
816 struct super_block *sb = inode->i_sb;
817 struct ufs_inode_info *ufsi = UFS_I(inode);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800818
819 UFSD("ENTER\n");
820 ufs_inode->ui_mode = cpu_to_fs16(sb, inode->i_mode);
821 ufs_inode->ui_nlink = cpu_to_fs16(sb, inode->i_nlink);
822
823 ufs_inode->ui_uid = cpu_to_fs32(sb, inode->i_uid);
824 ufs_inode->ui_gid = cpu_to_fs32(sb, inode->i_gid);
825
826 ufs_inode->ui_size = cpu_to_fs64(sb, inode->i_size);
Evgeniy Dushistov21898502007-03-16 13:38:07 -0800827 ufs_inode->ui_atime = cpu_to_fs64(sb, inode->i_atime.tv_sec);
828 ufs_inode->ui_atimensec = cpu_to_fs32(sb, inode->i_atime.tv_nsec);
829 ufs_inode->ui_ctime = cpu_to_fs64(sb, inode->i_ctime.tv_sec);
830 ufs_inode->ui_ctimensec = cpu_to_fs32(sb, inode->i_ctime.tv_nsec);
831 ufs_inode->ui_mtime = cpu_to_fs64(sb, inode->i_mtime.tv_sec);
832 ufs_inode->ui_mtimensec = cpu_to_fs32(sb, inode->i_mtime.tv_nsec);
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800833
834 ufs_inode->ui_blocks = cpu_to_fs64(sb, inode->i_blocks);
835 ufs_inode->ui_flags = cpu_to_fs32(sb, ufsi->i_flags);
836 ufs_inode->ui_gen = cpu_to_fs32(sb, inode->i_generation);
837
838 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
839 /* ufs_inode->ui_u2.ui_addr.ui_db[0] = cpu_to_fs32(sb, inode->i_rdev); */
840 ufs_inode->ui_u2.ui_addr.ui_db[0] = ufsi->i_u1.u2_i_data[0];
841 } else if (inode->i_blocks) {
Duane Griffinf33219b2009-01-08 22:43:49 +0000842 memcpy(&ufs_inode->ui_u2.ui_addr, ufsi->i_u1.u2_i_data,
843 sizeof(ufs_inode->ui_u2.ui_addr));
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800844 } else {
Duane Griffinf33219b2009-01-08 22:43:49 +0000845 memcpy(&ufs_inode->ui_u2.ui_symlink, ufsi->i_u1.i_symlink,
846 sizeof(ufs_inode->ui_u2.ui_symlink));
Evgeniy Dushistov3313e292007-02-12 00:54:31 -0800847 }
848
849 if (!inode->i_nlink)
850 memset (ufs_inode, 0, sizeof(struct ufs2_inode));
851 UFSD("EXIT\n");
852}
853
854static int ufs_update_inode(struct inode * inode, int do_sync)
855{
856 struct super_block *sb = inode->i_sb;
857 struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
858 struct buffer_head * bh;
859
860 UFSD("ENTER, ino %lu\n", inode->i_ino);
861
862 if (inode->i_ino < UFS_ROOTINO ||
863 inode->i_ino > (uspi->s_ncg * uspi->s_ipg)) {
864 ufs_warning (sb, "ufs_read_inode", "bad inode number (%lu)\n", inode->i_ino);
865 return -1;
866 }
867
868 bh = sb_bread(sb, ufs_inotofsba(inode->i_ino));
869 if (!bh) {
870 ufs_warning (sb, "ufs_read_inode", "unable to read inode %lu\n", inode->i_ino);
871 return -1;
872 }
873 if (uspi->fs_magic == UFS2_MAGIC) {
874 struct ufs2_inode *ufs2_inode = (struct ufs2_inode *)bh->b_data;
875
876 ufs2_update_inode(inode,
877 ufs2_inode + ufs_inotofsbo(inode->i_ino));
878 } else {
879 struct ufs_inode *ufs_inode = (struct ufs_inode *) bh->b_data;
880
881 ufs1_update_inode(inode, ufs_inode + ufs_inotofsbo(inode->i_ino));
882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884 mark_buffer_dirty(bh);
885 if (do_sync)
886 sync_dirty_buffer(bh);
887 brelse (bh);
888
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700889 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 return 0;
891}
892
893int ufs_write_inode (struct inode * inode, int wait)
894{
895 int ret;
896 lock_kernel();
897 ret = ufs_update_inode (inode, wait);
898 unlock_kernel();
899 return ret;
900}
901
902int ufs_sync_inode (struct inode *inode)
903{
904 return ufs_update_inode (inode, 1);
905}
906
907void ufs_delete_inode (struct inode * inode)
908{
Evgeniy Dushistov10e5dce2006-07-01 04:36:24 -0700909 loff_t old_i_size;
910
Mark Fashehfef26652005-09-09 13:01:31 -0700911 truncate_inode_pages(&inode->i_data, 0);
Evgeniy Dushistov07a0cfe2007-04-16 22:53:24 -0700912 if (is_bad_inode(inode))
913 goto no_delete;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 /*UFS_I(inode)->i_dtime = CURRENT_TIME;*/
915 lock_kernel();
916 mark_inode_dirty(inode);
917 ufs_update_inode(inode, IS_SYNC(inode));
Evgeniy Dushistov10e5dce2006-07-01 04:36:24 -0700918 old_i_size = inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 inode->i_size = 0;
Evgeniy Dushistov10e5dce2006-07-01 04:36:24 -0700920 if (inode->i_blocks && ufs_truncate(inode, old_i_size))
Harvey Harrison9746077a72008-04-28 02:16:17 -0700921 ufs_warning(inode->i_sb, __func__, "ufs_truncate failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 ufs_free_inode (inode);
923 unlock_kernel();
Evgeniy Dushistov07a0cfe2007-04-16 22:53:24 -0700924 return;
925no_delete:
926 clear_inode(inode); /* We must guarantee clearing of inode... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}