blob: 6f34510449e892b3afc807760d1108cba16d0dc2 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "affs.h"
15
David Howells210f8552008-02-07 00:15:29 -080016struct inode *affs_iget(struct super_block *sb, unsigned long ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017{
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 struct affs_sb_info *sbi = AFFS_SB(sb);
19 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 struct affs_tail *tail;
David Howells210f8552008-02-07 00:15:29 -080021 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 u32 block;
23 u32 size;
24 u32 prot;
25 u16 id;
26
David Howells210f8552008-02-07 00:15:29 -080027 inode = iget_locked(sb, ino);
28 if (!inode)
29 return ERR_PTR(-ENOMEM);
30 if (!(inode->i_state & I_NEW))
31 return inode;
32
Fabian Frederick9606d9a2014-06-06 14:37:25 -070033 pr_debug("affs_iget(%lu)\n", inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35 block = inode->i_ino;
36 bh = affs_bread(sb, block);
37 if (!bh) {
38 affs_warning(sb, "read_inode", "Cannot read block %d", block);
39 goto bad_inode;
40 }
41 if (affs_checksum_block(sb, bh) || be32_to_cpu(AFFS_HEAD(bh)->ptype) != T_SHORT) {
42 affs_warning(sb,"read_inode",
43 "Checksum or type (ptype=%d) error on inode %d",
44 AFFS_HEAD(bh)->ptype, block);
45 goto bad_inode;
46 }
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 tail = AFFS_TAIL(sb, bh);
49 prot = be32_to_cpu(tail->protect);
50
51 inode->i_size = 0;
Miklos Szeredibfe86842011-10-28 14:13:29 +020052 set_nlink(inode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 inode->i_mode = 0;
54 AFFS_I(inode)->i_extcnt = 1;
55 AFFS_I(inode)->i_ext_last = ~1;
56 AFFS_I(inode)->i_protect = prot;
Roman Zippeldca3c332008-04-29 17:02:20 +020057 atomic_set(&AFFS_I(inode)->i_opencnt, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 AFFS_I(inode)->i_blkcnt = 0;
59 AFFS_I(inode)->i_lc = NULL;
60 AFFS_I(inode)->i_lc_size = 0;
61 AFFS_I(inode)->i_lc_shift = 0;
62 AFFS_I(inode)->i_lc_mask = 0;
63 AFFS_I(inode)->i_ac = NULL;
64 AFFS_I(inode)->i_ext_bh = NULL;
65 AFFS_I(inode)->mmu_private = 0;
66 AFFS_I(inode)->i_lastalloc = 0;
67 AFFS_I(inode)->i_pa_cnt = 0;
68
69 if (sbi->s_flags & SF_SETMODE)
70 inode->i_mode = sbi->s_mode;
71 else
72 inode->i_mode = prot_to_mode(prot);
73
74 id = be16_to_cpu(tail->uid);
75 if (id == 0 || sbi->s_flags & SF_SETUID)
76 inode->i_uid = sbi->s_uid;
77 else if (id == 0xFFFF && sbi->s_flags & SF_MUFS)
Eric W. Biederman8fed10b2012-02-07 16:20:16 -080078 i_uid_write(inode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 else
Eric W. Biederman8fed10b2012-02-07 16:20:16 -080080 i_uid_write(inode, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 id = be16_to_cpu(tail->gid);
83 if (id == 0 || sbi->s_flags & SF_SETGID)
84 inode->i_gid = sbi->s_gid;
85 else if (id == 0xFFFF && sbi->s_flags & SF_MUFS)
Eric W. Biederman8fed10b2012-02-07 16:20:16 -080086 i_gid_write(inode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 else
Eric W. Biederman8fed10b2012-02-07 16:20:16 -080088 i_gid_write(inode, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 switch (be32_to_cpu(tail->stype)) {
91 case ST_ROOT:
92 inode->i_uid = sbi->s_uid;
93 inode->i_gid = sbi->s_gid;
94 /* fall through */
95 case ST_USERDIR:
96 if (be32_to_cpu(tail->stype) == ST_USERDIR ||
97 sbi->s_flags & SF_SETMODE) {
98 if (inode->i_mode & S_IRUSR)
99 inode->i_mode |= S_IXUSR;
100 if (inode->i_mode & S_IRGRP)
101 inode->i_mode |= S_IXGRP;
102 if (inode->i_mode & S_IROTH)
103 inode->i_mode |= S_IXOTH;
104 inode->i_mode |= S_IFDIR;
105 } else
106 inode->i_mode = S_IRUGO | S_IXUGO | S_IWUSR | S_IFDIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 /* Maybe it should be controlled by mount parameter? */
108 //inode->i_mode |= S_ISVTX;
109 inode->i_op = &affs_dir_inode_operations;
110 inode->i_fop = &affs_dir_operations;
111 break;
112 case ST_LINKDIR:
113#if 0
114 affs_warning(sb, "read_inode", "inode is LINKDIR");
115 goto bad_inode;
116#else
117 inode->i_mode |= S_IFDIR;
Al Viroc765d472008-12-04 09:50:55 -0500118 /* ... and leave ->i_op and ->i_fop pointing to empty */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 break;
120#endif
121 case ST_LINKFILE:
122 affs_warning(sb, "read_inode", "inode is LINKFILE");
123 goto bad_inode;
124 case ST_FILE:
125 size = be32_to_cpu(tail->size);
126 inode->i_mode |= S_IFREG;
127 AFFS_I(inode)->mmu_private = inode->i_size = size;
128 if (inode->i_size) {
129 AFFS_I(inode)->i_blkcnt = (size - 1) /
130 sbi->s_data_blksize + 1;
131 AFFS_I(inode)->i_extcnt = (AFFS_I(inode)->i_blkcnt - 1) /
132 sbi->s_hashsize + 1;
133 }
134 if (tail->link_chain)
Miklos Szeredibfe86842011-10-28 14:13:29 +0200135 set_nlink(inode, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 inode->i_mapping->a_ops = (sbi->s_flags & SF_OFS) ? &affs_aops_ofs : &affs_aops;
137 inode->i_op = &affs_file_inode_operations;
138 inode->i_fop = &affs_file_operations;
139 break;
140 case ST_SOFTLINK:
141 inode->i_mode |= S_IFLNK;
142 inode->i_op = &affs_symlink_inode_operations;
143 inode->i_data.a_ops = &affs_symlink_aops;
144 break;
145 }
146
147 inode->i_mtime.tv_sec = inode->i_atime.tv_sec = inode->i_ctime.tv_sec
148 = (be32_to_cpu(tail->change.days) * (24 * 60 * 60) +
149 be32_to_cpu(tail->change.mins) * 60 +
150 be32_to_cpu(tail->change.ticks) / 50 +
151 ((8 * 365 + 2) * 24 * 60 * 60)) +
152 sys_tz.tz_minuteswest * 60;
153 inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = inode->i_atime.tv_nsec = 0;
154 affs_brelse(bh);
David Howells210f8552008-02-07 00:15:29 -0800155 unlock_new_inode(inode);
156 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158bad_inode:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 affs_brelse(bh);
David Howells210f8552008-02-07 00:15:29 -0800160 iget_failed(inode);
161 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
164int
Christoph Hellwiga9185b42010-03-05 09:21:37 +0100165affs_write_inode(struct inode *inode, struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 struct super_block *sb = inode->i_sb;
168 struct buffer_head *bh;
169 struct affs_tail *tail;
170 uid_t uid;
171 gid_t gid;
172
Fabian Frederick9606d9a2014-06-06 14:37:25 -0700173 pr_debug("write_inode(%lu)\n", inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 if (!inode->i_nlink)
176 // possibly free block
177 return 0;
178 bh = affs_bread(sb, inode->i_ino);
179 if (!bh) {
180 affs_error(sb,"write_inode","Cannot read block %lu",inode->i_ino);
181 return -EIO;
182 }
183 tail = AFFS_TAIL(sb, bh);
184 if (tail->stype == cpu_to_be32(ST_ROOT)) {
185 secs_to_datestamp(inode->i_mtime.tv_sec,&AFFS_ROOT_TAIL(sb, bh)->root_change);
186 } else {
187 tail->protect = cpu_to_be32(AFFS_I(inode)->i_protect);
188 tail->size = cpu_to_be32(inode->i_size);
189 secs_to_datestamp(inode->i_mtime.tv_sec,&tail->change);
190 if (!(inode->i_ino == AFFS_SB(sb)->s_root_block)) {
Eric W. Biederman8fed10b2012-02-07 16:20:16 -0800191 uid = i_uid_read(inode);
192 gid = i_gid_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (AFFS_SB(sb)->s_flags & SF_MUFS) {
Eric W. Biederman8fed10b2012-02-07 16:20:16 -0800194 if (uid == 0 || uid == 0xFFFF)
195 uid = uid ^ ~0;
196 if (gid == 0 || gid == 0xFFFF)
197 gid = gid ^ ~0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
199 if (!(AFFS_SB(sb)->s_flags & SF_SETUID))
200 tail->uid = cpu_to_be16(uid);
201 if (!(AFFS_SB(sb)->s_flags & SF_SETGID))
202 tail->gid = cpu_to_be16(gid);
203 }
204 }
205 affs_fix_checksum(sb, bh);
206 mark_buffer_dirty_inode(bh, inode);
207 affs_brelse(bh);
208 affs_free_prealloc(inode);
209 return 0;
210}
211
212int
213affs_notify_change(struct dentry *dentry, struct iattr *attr)
214{
215 struct inode *inode = dentry->d_inode;
216 int error;
217
Fabian Frederick9606d9a2014-06-06 14:37:25 -0700218 pr_debug("notify_change(%lu,0x%x)\n", inode->i_ino, attr->ia_valid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 error = inode_change_ok(inode,attr);
221 if (error)
222 goto out;
223
224 if (((attr->ia_valid & ATTR_UID) && (AFFS_SB(inode->i_sb)->s_flags & SF_SETUID)) ||
225 ((attr->ia_valid & ATTR_GID) && (AFFS_SB(inode->i_sb)->s_flags & SF_SETGID)) ||
226 ((attr->ia_valid & ATTR_MODE) &&
227 (AFFS_SB(inode->i_sb)->s_flags & (SF_SETMODE | SF_IMMUTABLE)))) {
228 if (!(AFFS_SB(inode->i_sb)->s_flags & SF_QUIET))
229 error = -EPERM;
230 goto out;
231 }
232
Christoph Hellwig10257742010-06-04 11:30:02 +0200233 if ((attr->ia_valid & ATTR_SIZE) &&
234 attr->ia_size != i_size_read(inode)) {
Marco Stornelli1dc18342012-12-15 11:51:53 +0100235 error = inode_newsize_ok(inode, attr->ia_size);
Christoph Hellwig10257742010-06-04 11:30:02 +0200236 if (error)
237 return error;
Marco Stornelli1dc18342012-12-15 11:51:53 +0100238
239 truncate_setsize(inode, attr->ia_size);
240 affs_truncate(inode);
Christoph Hellwig10257742010-06-04 11:30:02 +0200241 }
242
243 setattr_copy(inode, attr);
244 mark_inode_dirty(inode);
245
246 if (attr->ia_valid & ATTR_MODE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 mode_to_prot(inode);
248out:
249 return error;
250}
251
252void
Al Virof053ddd2010-06-06 10:16:41 -0400253affs_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Roman Zippeldca3c332008-04-29 17:02:20 +0200255 unsigned long cache_page;
Fabian Frederick9606d9a2014-06-06 14:37:25 -0700256 pr_debug("evict_inode(ino=%lu, nlink=%u)\n",
257 inode->i_ino, inode->i_nlink);
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700258 truncate_inode_pages_final(&inode->i_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Al Virof053ddd2010-06-06 10:16:41 -0400260 if (!inode->i_nlink) {
261 inode->i_size = 0;
262 affs_truncate(inode);
263 }
Roman Zippeldca3c332008-04-29 17:02:20 +0200264
Al Virof053ddd2010-06-06 10:16:41 -0400265 invalidate_inode_buffers(inode);
Jan Karadbd57682012-05-03 14:48:02 +0200266 clear_inode(inode);
Roman Zippeldca3c332008-04-29 17:02:20 +0200267 affs_free_prealloc(inode);
268 cache_page = (unsigned long)AFFS_I(inode)->i_lc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (cache_page) {
Fabian Frederick9606d9a2014-06-06 14:37:25 -0700270 pr_debug("freeing ext cache\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 AFFS_I(inode)->i_lc = NULL;
272 AFFS_I(inode)->i_ac = NULL;
273 free_page(cache_page);
274 }
275 affs_brelse(AFFS_I(inode)->i_ext_bh);
276 AFFS_I(inode)->i_ext_last = ~1;
277 AFFS_I(inode)->i_ext_bh = NULL;
Al Virof053ddd2010-06-06 10:16:41 -0400278
279 if (!inode->i_nlink)
280 affs_free_block(inode->i_sb, inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
282
283struct inode *
284affs_new_inode(struct inode *dir)
285{
286 struct super_block *sb = dir->i_sb;
287 struct inode *inode;
288 u32 block;
289 struct buffer_head *bh;
290
291 if (!(inode = new_inode(sb)))
292 goto err_inode;
293
294 if (!(block = affs_alloc_block(dir, dir->i_ino)))
295 goto err_block;
296
297 bh = affs_getzeroblk(sb, block);
298 if (!bh)
299 goto err_bh;
300 mark_buffer_dirty_inode(bh, inode);
301 affs_brelse(bh);
302
David Howells21559982008-11-14 10:38:45 +1100303 inode->i_uid = current_fsuid();
304 inode->i_gid = current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 inode->i_ino = block;
Miklos Szeredibfe86842011-10-28 14:13:29 +0200306 set_nlink(inode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
Roman Zippeldca3c332008-04-29 17:02:20 +0200308 atomic_set(&AFFS_I(inode)->i_opencnt, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 AFFS_I(inode)->i_blkcnt = 0;
310 AFFS_I(inode)->i_lc = NULL;
311 AFFS_I(inode)->i_lc_size = 0;
312 AFFS_I(inode)->i_lc_shift = 0;
313 AFFS_I(inode)->i_lc_mask = 0;
314 AFFS_I(inode)->i_ac = NULL;
315 AFFS_I(inode)->i_ext_bh = NULL;
316 AFFS_I(inode)->mmu_private = 0;
317 AFFS_I(inode)->i_protect = 0;
318 AFFS_I(inode)->i_lastalloc = 0;
319 AFFS_I(inode)->i_pa_cnt = 0;
320 AFFS_I(inode)->i_extcnt = 1;
321 AFFS_I(inode)->i_ext_last = ~1;
322
323 insert_inode_hash(inode);
324
325 return inode;
326
327err_bh:
328 affs_free_block(sb, block);
329err_block:
330 iput(inode);
331err_inode:
332 return NULL;
333}
334
335/*
336 * Add an entry to a directory. Create the header block
337 * and insert it into the hash table.
338 */
339
340int
341affs_add_entry(struct inode *dir, struct inode *inode, struct dentry *dentry, s32 type)
342{
343 struct super_block *sb = dir->i_sb;
344 struct buffer_head *inode_bh = NULL;
345 struct buffer_head *bh = NULL;
346 u32 block = 0;
347 int retval;
348
Geert Uytterhoeven08fe1002015-02-17 13:46:10 -0800349 pr_debug("%s(dir=%lu, inode=%lu, \"%pd\", type=%d)\n", __func__,
350 dir->i_ino, inode->i_ino, dentry, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 retval = -EIO;
353 bh = affs_bread(sb, inode->i_ino);
354 if (!bh)
355 goto done;
356
357 affs_lock_link(inode);
358 switch (type) {
359 case ST_LINKFILE:
360 case ST_LINKDIR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 retval = -ENOSPC;
362 block = affs_alloc_block(dir, dir->i_ino);
363 if (!block)
364 goto err;
365 retval = -EIO;
Roman Zippeldca3c332008-04-29 17:02:20 +0200366 inode_bh = bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 bh = affs_getzeroblk(sb, block);
368 if (!bh)
369 goto err;
370 break;
371 default:
372 break;
373 }
374
375 AFFS_HEAD(bh)->ptype = cpu_to_be32(T_SHORT);
376 AFFS_HEAD(bh)->key = cpu_to_be32(bh->b_blocknr);
377 affs_copy_name(AFFS_TAIL(sb, bh)->name, dentry);
378 AFFS_TAIL(sb, bh)->stype = cpu_to_be32(type);
379 AFFS_TAIL(sb, bh)->parent = cpu_to_be32(dir->i_ino);
380
381 if (inode_bh) {
382 __be32 chain;
383 chain = AFFS_TAIL(sb, inode_bh)->link_chain;
384 AFFS_TAIL(sb, bh)->original = cpu_to_be32(inode->i_ino);
385 AFFS_TAIL(sb, bh)->link_chain = chain;
386 AFFS_TAIL(sb, inode_bh)->link_chain = cpu_to_be32(block);
387 affs_adjust_checksum(inode_bh, block - be32_to_cpu(chain));
388 mark_buffer_dirty_inode(inode_bh, inode);
Miklos Szeredibfe86842011-10-28 14:13:29 +0200389 set_nlink(inode, 2);
Al Viro7de9c6ee2010-10-23 11:11:40 -0400390 ihold(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
392 affs_fix_checksum(sb, bh);
393 mark_buffer_dirty_inode(bh, inode);
394 dentry->d_fsdata = (void *)(long)bh->b_blocknr;
395
396 affs_lock_dir(dir);
397 retval = affs_insert_hash(dir, bh);
398 mark_buffer_dirty_inode(bh, inode);
399 affs_unlock_dir(dir);
400 affs_unlock_link(inode);
401
402 d_instantiate(dentry, inode);
403done:
404 affs_brelse(inode_bh);
405 affs_brelse(bh);
406 return retval;
407err:
408 if (block)
409 affs_free_block(sb, block);
410 affs_unlock_link(inode);
411 goto done;
412}