blob: 27fe6cbe43ae84bc1582f8e86131cebb1566eb1e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/affs/inode.c
3 *
4 * (c) 1996 Hans-Joachim Widmaier - Rewritten
5 *
6 * (C) 1993 Ray Burr - Modified for Amiga FFS filesystem.
7 *
8 * (C) 1992 Eric Youngdale Modified for ISO9660 filesystem.
9 *
10 * (C) 1991 Linus Torvalds - minix filesystem
11 */
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040012#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "affs.h"
14
Arjan van de Ven754661f2007-02-12 00:55:38 -080015extern const struct inode_operations affs_symlink_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016extern struct timezone sys_tz;
17
David Howells210f8552008-02-07 00:15:29 -080018struct inode *affs_iget(struct super_block *sb, unsigned long ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -070019{
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 struct affs_sb_info *sbi = AFFS_SB(sb);
21 struct buffer_head *bh;
22 struct affs_head *head;
23 struct affs_tail *tail;
David Howells210f8552008-02-07 00:15:29 -080024 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 u32 block;
26 u32 size;
27 u32 prot;
28 u16 id;
29
David Howells210f8552008-02-07 00:15:29 -080030 inode = iget_locked(sb, ino);
31 if (!inode)
32 return ERR_PTR(-ENOMEM);
33 if (!(inode->i_state & I_NEW))
34 return inode;
35
36 pr_debug("AFFS: affs_iget(%lu)\n", inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38 block = inode->i_ino;
39 bh = affs_bread(sb, block);
40 if (!bh) {
41 affs_warning(sb, "read_inode", "Cannot read block %d", block);
42 goto bad_inode;
43 }
44 if (affs_checksum_block(sb, bh) || be32_to_cpu(AFFS_HEAD(bh)->ptype) != T_SHORT) {
45 affs_warning(sb,"read_inode",
46 "Checksum or type (ptype=%d) error on inode %d",
47 AFFS_HEAD(bh)->ptype, block);
48 goto bad_inode;
49 }
50
51 head = AFFS_HEAD(bh);
52 tail = AFFS_TAIL(sb, bh);
53 prot = be32_to_cpu(tail->protect);
54
55 inode->i_size = 0;
56 inode->i_nlink = 1;
57 inode->i_mode = 0;
58 AFFS_I(inode)->i_extcnt = 1;
59 AFFS_I(inode)->i_ext_last = ~1;
60 AFFS_I(inode)->i_protect = prot;
61 AFFS_I(inode)->i_opencnt = 0;
62 AFFS_I(inode)->i_blkcnt = 0;
63 AFFS_I(inode)->i_lc = NULL;
64 AFFS_I(inode)->i_lc_size = 0;
65 AFFS_I(inode)->i_lc_shift = 0;
66 AFFS_I(inode)->i_lc_mask = 0;
67 AFFS_I(inode)->i_ac = NULL;
68 AFFS_I(inode)->i_ext_bh = NULL;
69 AFFS_I(inode)->mmu_private = 0;
70 AFFS_I(inode)->i_lastalloc = 0;
71 AFFS_I(inode)->i_pa_cnt = 0;
72
73 if (sbi->s_flags & SF_SETMODE)
74 inode->i_mode = sbi->s_mode;
75 else
76 inode->i_mode = prot_to_mode(prot);
77
78 id = be16_to_cpu(tail->uid);
79 if (id == 0 || sbi->s_flags & SF_SETUID)
80 inode->i_uid = sbi->s_uid;
81 else if (id == 0xFFFF && sbi->s_flags & SF_MUFS)
82 inode->i_uid = 0;
83 else
84 inode->i_uid = id;
85
86 id = be16_to_cpu(tail->gid);
87 if (id == 0 || sbi->s_flags & SF_SETGID)
88 inode->i_gid = sbi->s_gid;
89 else if (id == 0xFFFF && sbi->s_flags & SF_MUFS)
90 inode->i_gid = 0;
91 else
92 inode->i_gid = id;
93
94 switch (be32_to_cpu(tail->stype)) {
95 case ST_ROOT:
96 inode->i_uid = sbi->s_uid;
97 inode->i_gid = sbi->s_gid;
98 /* fall through */
99 case ST_USERDIR:
100 if (be32_to_cpu(tail->stype) == ST_USERDIR ||
101 sbi->s_flags & SF_SETMODE) {
102 if (inode->i_mode & S_IRUSR)
103 inode->i_mode |= S_IXUSR;
104 if (inode->i_mode & S_IRGRP)
105 inode->i_mode |= S_IXGRP;
106 if (inode->i_mode & S_IROTH)
107 inode->i_mode |= S_IXOTH;
108 inode->i_mode |= S_IFDIR;
109 } else
110 inode->i_mode = S_IRUGO | S_IXUGO | S_IWUSR | S_IFDIR;
111 if (tail->link_chain)
112 inode->i_nlink = 2;
113 /* Maybe it should be controlled by mount parameter? */
114 //inode->i_mode |= S_ISVTX;
115 inode->i_op = &affs_dir_inode_operations;
116 inode->i_fop = &affs_dir_operations;
117 break;
118 case ST_LINKDIR:
119#if 0
120 affs_warning(sb, "read_inode", "inode is LINKDIR");
121 goto bad_inode;
122#else
123 inode->i_mode |= S_IFDIR;
124 inode->i_op = NULL;
125 inode->i_fop = NULL;
126 break;
127#endif
128 case ST_LINKFILE:
129 affs_warning(sb, "read_inode", "inode is LINKFILE");
130 goto bad_inode;
131 case ST_FILE:
132 size = be32_to_cpu(tail->size);
133 inode->i_mode |= S_IFREG;
134 AFFS_I(inode)->mmu_private = inode->i_size = size;
135 if (inode->i_size) {
136 AFFS_I(inode)->i_blkcnt = (size - 1) /
137 sbi->s_data_blksize + 1;
138 AFFS_I(inode)->i_extcnt = (AFFS_I(inode)->i_blkcnt - 1) /
139 sbi->s_hashsize + 1;
140 }
141 if (tail->link_chain)
142 inode->i_nlink = 2;
143 inode->i_mapping->a_ops = (sbi->s_flags & SF_OFS) ? &affs_aops_ofs : &affs_aops;
144 inode->i_op = &affs_file_inode_operations;
145 inode->i_fop = &affs_file_operations;
146 break;
147 case ST_SOFTLINK:
148 inode->i_mode |= S_IFLNK;
149 inode->i_op = &affs_symlink_inode_operations;
150 inode->i_data.a_ops = &affs_symlink_aops;
151 break;
152 }
153
154 inode->i_mtime.tv_sec = inode->i_atime.tv_sec = inode->i_ctime.tv_sec
155 = (be32_to_cpu(tail->change.days) * (24 * 60 * 60) +
156 be32_to_cpu(tail->change.mins) * 60 +
157 be32_to_cpu(tail->change.ticks) / 50 +
158 ((8 * 365 + 2) * 24 * 60 * 60)) +
159 sys_tz.tz_minuteswest * 60;
160 inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = inode->i_atime.tv_nsec = 0;
161 affs_brelse(bh);
David Howells210f8552008-02-07 00:15:29 -0800162 unlock_new_inode(inode);
163 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165bad_inode:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 affs_brelse(bh);
David Howells210f8552008-02-07 00:15:29 -0800167 iget_failed(inode);
168 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
171int
172affs_write_inode(struct inode *inode, int unused)
173{
174 struct super_block *sb = inode->i_sb;
175 struct buffer_head *bh;
176 struct affs_tail *tail;
177 uid_t uid;
178 gid_t gid;
179
180 pr_debug("AFFS: write_inode(%lu)\n",inode->i_ino);
181
182 if (!inode->i_nlink)
183 // possibly free block
184 return 0;
185 bh = affs_bread(sb, inode->i_ino);
186 if (!bh) {
187 affs_error(sb,"write_inode","Cannot read block %lu",inode->i_ino);
188 return -EIO;
189 }
190 tail = AFFS_TAIL(sb, bh);
191 if (tail->stype == cpu_to_be32(ST_ROOT)) {
192 secs_to_datestamp(inode->i_mtime.tv_sec,&AFFS_ROOT_TAIL(sb, bh)->root_change);
193 } else {
194 tail->protect = cpu_to_be32(AFFS_I(inode)->i_protect);
195 tail->size = cpu_to_be32(inode->i_size);
196 secs_to_datestamp(inode->i_mtime.tv_sec,&tail->change);
197 if (!(inode->i_ino == AFFS_SB(sb)->s_root_block)) {
198 uid = inode->i_uid;
199 gid = inode->i_gid;
200 if (AFFS_SB(sb)->s_flags & SF_MUFS) {
201 if (inode->i_uid == 0 || inode->i_uid == 0xFFFF)
202 uid = inode->i_uid ^ ~0;
203 if (inode->i_gid == 0 || inode->i_gid == 0xFFFF)
204 gid = inode->i_gid ^ ~0;
205 }
206 if (!(AFFS_SB(sb)->s_flags & SF_SETUID))
207 tail->uid = cpu_to_be16(uid);
208 if (!(AFFS_SB(sb)->s_flags & SF_SETGID))
209 tail->gid = cpu_to_be16(gid);
210 }
211 }
212 affs_fix_checksum(sb, bh);
213 mark_buffer_dirty_inode(bh, inode);
214 affs_brelse(bh);
215 affs_free_prealloc(inode);
216 return 0;
217}
218
219int
220affs_notify_change(struct dentry *dentry, struct iattr *attr)
221{
222 struct inode *inode = dentry->d_inode;
223 int error;
224
225 pr_debug("AFFS: notify_change(%lu,0x%x)\n",inode->i_ino,attr->ia_valid);
226
227 error = inode_change_ok(inode,attr);
228 if (error)
229 goto out;
230
231 if (((attr->ia_valid & ATTR_UID) && (AFFS_SB(inode->i_sb)->s_flags & SF_SETUID)) ||
232 ((attr->ia_valid & ATTR_GID) && (AFFS_SB(inode->i_sb)->s_flags & SF_SETGID)) ||
233 ((attr->ia_valid & ATTR_MODE) &&
234 (AFFS_SB(inode->i_sb)->s_flags & (SF_SETMODE | SF_IMMUTABLE)))) {
235 if (!(AFFS_SB(inode->i_sb)->s_flags & SF_QUIET))
236 error = -EPERM;
237 goto out;
238 }
239
240 error = inode_setattr(inode, attr);
241 if (!error && (attr->ia_valid & ATTR_MODE))
242 mode_to_prot(inode);
243out:
244 return error;
245}
246
247void
248affs_put_inode(struct inode *inode)
249{
250 pr_debug("AFFS: put_inode(ino=%lu, nlink=%u)\n", inode->i_ino, inode->i_nlink);
251 affs_free_prealloc(inode);
Christoph Hellwig2be3c792007-02-20 13:58:11 -0800252}
253
254void
255affs_drop_inode(struct inode *inode)
256{
257 mutex_lock(&inode->i_mutex);
258 if (inode->i_size != AFFS_I(inode)->mmu_private)
259 affs_truncate(inode);
260 mutex_unlock(&inode->i_mutex);
261
262 generic_drop_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
265void
266affs_delete_inode(struct inode *inode)
267{
268 pr_debug("AFFS: delete_inode(ino=%lu, nlink=%u)\n", inode->i_ino, inode->i_nlink);
Mark Fashehfef26652005-09-09 13:01:31 -0700269 truncate_inode_pages(&inode->i_data, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 inode->i_size = 0;
271 if (S_ISREG(inode->i_mode))
272 affs_truncate(inode);
273 clear_inode(inode);
274 affs_free_block(inode->i_sb, inode->i_ino);
275}
276
277void
278affs_clear_inode(struct inode *inode)
279{
280 unsigned long cache_page = (unsigned long) AFFS_I(inode)->i_lc;
281
282 pr_debug("AFFS: clear_inode(ino=%lu, nlink=%u)\n", inode->i_ino, inode->i_nlink);
283 if (cache_page) {
284 pr_debug("AFFS: freeing ext cache\n");
285 AFFS_I(inode)->i_lc = NULL;
286 AFFS_I(inode)->i_ac = NULL;
287 free_page(cache_page);
288 }
289 affs_brelse(AFFS_I(inode)->i_ext_bh);
290 AFFS_I(inode)->i_ext_last = ~1;
291 AFFS_I(inode)->i_ext_bh = NULL;
292}
293
294struct inode *
295affs_new_inode(struct inode *dir)
296{
297 struct super_block *sb = dir->i_sb;
298 struct inode *inode;
299 u32 block;
300 struct buffer_head *bh;
301
302 if (!(inode = new_inode(sb)))
303 goto err_inode;
304
305 if (!(block = affs_alloc_block(dir, dir->i_ino)))
306 goto err_block;
307
308 bh = affs_getzeroblk(sb, block);
309 if (!bh)
310 goto err_bh;
311 mark_buffer_dirty_inode(bh, inode);
312 affs_brelse(bh);
313
314 inode->i_uid = current->fsuid;
315 inode->i_gid = current->fsgid;
316 inode->i_ino = block;
317 inode->i_nlink = 1;
318 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
319 AFFS_I(inode)->i_opencnt = 0;
320 AFFS_I(inode)->i_blkcnt = 0;
321 AFFS_I(inode)->i_lc = NULL;
322 AFFS_I(inode)->i_lc_size = 0;
323 AFFS_I(inode)->i_lc_shift = 0;
324 AFFS_I(inode)->i_lc_mask = 0;
325 AFFS_I(inode)->i_ac = NULL;
326 AFFS_I(inode)->i_ext_bh = NULL;
327 AFFS_I(inode)->mmu_private = 0;
328 AFFS_I(inode)->i_protect = 0;
329 AFFS_I(inode)->i_lastalloc = 0;
330 AFFS_I(inode)->i_pa_cnt = 0;
331 AFFS_I(inode)->i_extcnt = 1;
332 AFFS_I(inode)->i_ext_last = ~1;
333
334 insert_inode_hash(inode);
335
336 return inode;
337
338err_bh:
339 affs_free_block(sb, block);
340err_block:
341 iput(inode);
342err_inode:
343 return NULL;
344}
345
346/*
347 * Add an entry to a directory. Create the header block
348 * and insert it into the hash table.
349 */
350
351int
352affs_add_entry(struct inode *dir, struct inode *inode, struct dentry *dentry, s32 type)
353{
354 struct super_block *sb = dir->i_sb;
355 struct buffer_head *inode_bh = NULL;
356 struct buffer_head *bh = NULL;
357 u32 block = 0;
358 int retval;
359
360 pr_debug("AFFS: add_entry(dir=%u, inode=%u, \"%*s\", type=%d)\n", (u32)dir->i_ino,
361 (u32)inode->i_ino, (int)dentry->d_name.len, dentry->d_name.name, type);
362
363 retval = -EIO;
364 bh = affs_bread(sb, inode->i_ino);
365 if (!bh)
366 goto done;
367
368 affs_lock_link(inode);
369 switch (type) {
370 case ST_LINKFILE:
371 case ST_LINKDIR:
372 inode_bh = bh;
373 retval = -ENOSPC;
374 block = affs_alloc_block(dir, dir->i_ino);
375 if (!block)
376 goto err;
377 retval = -EIO;
378 bh = affs_getzeroblk(sb, block);
379 if (!bh)
380 goto err;
381 break;
382 default:
383 break;
384 }
385
386 AFFS_HEAD(bh)->ptype = cpu_to_be32(T_SHORT);
387 AFFS_HEAD(bh)->key = cpu_to_be32(bh->b_blocknr);
388 affs_copy_name(AFFS_TAIL(sb, bh)->name, dentry);
389 AFFS_TAIL(sb, bh)->stype = cpu_to_be32(type);
390 AFFS_TAIL(sb, bh)->parent = cpu_to_be32(dir->i_ino);
391
392 if (inode_bh) {
393 __be32 chain;
394 chain = AFFS_TAIL(sb, inode_bh)->link_chain;
395 AFFS_TAIL(sb, bh)->original = cpu_to_be32(inode->i_ino);
396 AFFS_TAIL(sb, bh)->link_chain = chain;
397 AFFS_TAIL(sb, inode_bh)->link_chain = cpu_to_be32(block);
398 affs_adjust_checksum(inode_bh, block - be32_to_cpu(chain));
399 mark_buffer_dirty_inode(inode_bh, inode);
400 inode->i_nlink = 2;
401 atomic_inc(&inode->i_count);
402 }
403 affs_fix_checksum(sb, bh);
404 mark_buffer_dirty_inode(bh, inode);
405 dentry->d_fsdata = (void *)(long)bh->b_blocknr;
406
407 affs_lock_dir(dir);
408 retval = affs_insert_hash(dir, bh);
409 mark_buffer_dirty_inode(bh, inode);
410 affs_unlock_dir(dir);
411 affs_unlock_link(inode);
412
413 d_instantiate(dentry, inode);
414done:
415 affs_brelse(inode_bh);
416 affs_brelse(bh);
417 return retval;
418err:
419 if (block)
420 affs_free_block(sb, block);
421 affs_unlock_link(inode);
422 goto done;
423}