xref: /openbmc/linux/fs/affs/inode.c (revision b627b4ed)
1 /*
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  */
12 #include <linux/sched.h>
13 #include "affs.h"
14 
15 extern const struct inode_operations affs_symlink_inode_operations;
16 extern struct timezone sys_tz;
17 
18 struct inode *affs_iget(struct super_block *sb, unsigned long ino)
19 {
20 	struct affs_sb_info	*sbi = AFFS_SB(sb);
21 	struct buffer_head	*bh;
22 	struct affs_head	*head;
23 	struct affs_tail	*tail;
24 	struct inode		*inode;
25 	u32			 block;
26 	u32			 size;
27 	u32			 prot;
28 	u16			 id;
29 
30 	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);
37 
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 	atomic_set(&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 		/* Maybe it should be controlled by mount parameter? */
112 		//inode->i_mode |= S_ISVTX;
113 		inode->i_op = &affs_dir_inode_operations;
114 		inode->i_fop = &affs_dir_operations;
115 		break;
116 	case ST_LINKDIR:
117 #if 0
118 		affs_warning(sb, "read_inode", "inode is LINKDIR");
119 		goto bad_inode;
120 #else
121 		inode->i_mode |= S_IFDIR;
122 		/* ... and leave ->i_op and ->i_fop pointing to empty */
123 		break;
124 #endif
125 	case ST_LINKFILE:
126 		affs_warning(sb, "read_inode", "inode is LINKFILE");
127 		goto bad_inode;
128 	case ST_FILE:
129 		size = be32_to_cpu(tail->size);
130 		inode->i_mode |= S_IFREG;
131 		AFFS_I(inode)->mmu_private = inode->i_size = size;
132 		if (inode->i_size) {
133 			AFFS_I(inode)->i_blkcnt = (size - 1) /
134 					       sbi->s_data_blksize + 1;
135 			AFFS_I(inode)->i_extcnt = (AFFS_I(inode)->i_blkcnt - 1) /
136 					       sbi->s_hashsize + 1;
137 		}
138 		if (tail->link_chain)
139 			inode->i_nlink = 2;
140 		inode->i_mapping->a_ops = (sbi->s_flags & SF_OFS) ? &affs_aops_ofs : &affs_aops;
141 		inode->i_op = &affs_file_inode_operations;
142 		inode->i_fop = &affs_file_operations;
143 		break;
144 	case ST_SOFTLINK:
145 		inode->i_mode |= S_IFLNK;
146 		inode->i_op = &affs_symlink_inode_operations;
147 		inode->i_data.a_ops = &affs_symlink_aops;
148 		break;
149 	}
150 
151 	inode->i_mtime.tv_sec = inode->i_atime.tv_sec = inode->i_ctime.tv_sec
152 		       = (be32_to_cpu(tail->change.days) * (24 * 60 * 60) +
153 		         be32_to_cpu(tail->change.mins) * 60 +
154 			 be32_to_cpu(tail->change.ticks) / 50 +
155 			 ((8 * 365 + 2) * 24 * 60 * 60)) +
156 			 sys_tz.tz_minuteswest * 60;
157 	inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = inode->i_atime.tv_nsec = 0;
158 	affs_brelse(bh);
159 	unlock_new_inode(inode);
160 	return inode;
161 
162 bad_inode:
163 	affs_brelse(bh);
164 	iget_failed(inode);
165 	return ERR_PTR(-EIO);
166 }
167 
168 int
169 affs_write_inode(struct inode *inode, int unused)
170 {
171 	struct super_block	*sb = inode->i_sb;
172 	struct buffer_head	*bh;
173 	struct affs_tail	*tail;
174 	uid_t			 uid;
175 	gid_t			 gid;
176 
177 	pr_debug("AFFS: write_inode(%lu)\n",inode->i_ino);
178 
179 	if (!inode->i_nlink)
180 		// possibly free block
181 		return 0;
182 	bh = affs_bread(sb, inode->i_ino);
183 	if (!bh) {
184 		affs_error(sb,"write_inode","Cannot read block %lu",inode->i_ino);
185 		return -EIO;
186 	}
187 	tail = AFFS_TAIL(sb, bh);
188 	if (tail->stype == cpu_to_be32(ST_ROOT)) {
189 		secs_to_datestamp(inode->i_mtime.tv_sec,&AFFS_ROOT_TAIL(sb, bh)->root_change);
190 	} else {
191 		tail->protect = cpu_to_be32(AFFS_I(inode)->i_protect);
192 		tail->size = cpu_to_be32(inode->i_size);
193 		secs_to_datestamp(inode->i_mtime.tv_sec,&tail->change);
194 		if (!(inode->i_ino == AFFS_SB(sb)->s_root_block)) {
195 			uid = inode->i_uid;
196 			gid = inode->i_gid;
197 			if (AFFS_SB(sb)->s_flags & SF_MUFS) {
198 				if (inode->i_uid == 0 || inode->i_uid == 0xFFFF)
199 					uid = inode->i_uid ^ ~0;
200 				if (inode->i_gid == 0 || inode->i_gid == 0xFFFF)
201 					gid = inode->i_gid ^ ~0;
202 			}
203 			if (!(AFFS_SB(sb)->s_flags & SF_SETUID))
204 				tail->uid = cpu_to_be16(uid);
205 			if (!(AFFS_SB(sb)->s_flags & SF_SETGID))
206 				tail->gid = cpu_to_be16(gid);
207 		}
208 	}
209 	affs_fix_checksum(sb, bh);
210 	mark_buffer_dirty_inode(bh, inode);
211 	affs_brelse(bh);
212 	affs_free_prealloc(inode);
213 	return 0;
214 }
215 
216 int
217 affs_notify_change(struct dentry *dentry, struct iattr *attr)
218 {
219 	struct inode *inode = dentry->d_inode;
220 	int error;
221 
222 	pr_debug("AFFS: notify_change(%lu,0x%x)\n",inode->i_ino,attr->ia_valid);
223 
224 	error = inode_change_ok(inode,attr);
225 	if (error)
226 		goto out;
227 
228 	if (((attr->ia_valid & ATTR_UID) && (AFFS_SB(inode->i_sb)->s_flags & SF_SETUID)) ||
229 	    ((attr->ia_valid & ATTR_GID) && (AFFS_SB(inode->i_sb)->s_flags & SF_SETGID)) ||
230 	    ((attr->ia_valid & ATTR_MODE) &&
231 	     (AFFS_SB(inode->i_sb)->s_flags & (SF_SETMODE | SF_IMMUTABLE)))) {
232 		if (!(AFFS_SB(inode->i_sb)->s_flags & SF_QUIET))
233 			error = -EPERM;
234 		goto out;
235 	}
236 
237 	error = inode_setattr(inode, attr);
238 	if (!error && (attr->ia_valid & ATTR_MODE))
239 		mode_to_prot(inode);
240 out:
241 	return error;
242 }
243 
244 void
245 affs_delete_inode(struct inode *inode)
246 {
247 	pr_debug("AFFS: delete_inode(ino=%lu, nlink=%u)\n", inode->i_ino, inode->i_nlink);
248 	truncate_inode_pages(&inode->i_data, 0);
249 	inode->i_size = 0;
250 	affs_truncate(inode);
251 	clear_inode(inode);
252 	affs_free_block(inode->i_sb, inode->i_ino);
253 }
254 
255 void
256 affs_clear_inode(struct inode *inode)
257 {
258 	unsigned long cache_page;
259 
260 	pr_debug("AFFS: clear_inode(ino=%lu, nlink=%u)\n", inode->i_ino, inode->i_nlink);
261 
262 	affs_free_prealloc(inode);
263 	cache_page = (unsigned long)AFFS_I(inode)->i_lc;
264 	if (cache_page) {
265 		pr_debug("AFFS: freeing ext cache\n");
266 		AFFS_I(inode)->i_lc = NULL;
267 		AFFS_I(inode)->i_ac = NULL;
268 		free_page(cache_page);
269 	}
270 	affs_brelse(AFFS_I(inode)->i_ext_bh);
271 	AFFS_I(inode)->i_ext_last = ~1;
272 	AFFS_I(inode)->i_ext_bh = NULL;
273 }
274 
275 struct inode *
276 affs_new_inode(struct inode *dir)
277 {
278 	struct super_block	*sb = dir->i_sb;
279 	struct inode		*inode;
280 	u32			 block;
281 	struct buffer_head	*bh;
282 
283 	if (!(inode = new_inode(sb)))
284 		goto err_inode;
285 
286 	if (!(block = affs_alloc_block(dir, dir->i_ino)))
287 		goto err_block;
288 
289 	bh = affs_getzeroblk(sb, block);
290 	if (!bh)
291 		goto err_bh;
292 	mark_buffer_dirty_inode(bh, inode);
293 	affs_brelse(bh);
294 
295 	inode->i_uid     = current_fsuid();
296 	inode->i_gid     = current_fsgid();
297 	inode->i_ino     = block;
298 	inode->i_nlink   = 1;
299 	inode->i_mtime   = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
300 	atomic_set(&AFFS_I(inode)->i_opencnt, 0);
301 	AFFS_I(inode)->i_blkcnt = 0;
302 	AFFS_I(inode)->i_lc = NULL;
303 	AFFS_I(inode)->i_lc_size = 0;
304 	AFFS_I(inode)->i_lc_shift = 0;
305 	AFFS_I(inode)->i_lc_mask = 0;
306 	AFFS_I(inode)->i_ac = NULL;
307 	AFFS_I(inode)->i_ext_bh = NULL;
308 	AFFS_I(inode)->mmu_private = 0;
309 	AFFS_I(inode)->i_protect = 0;
310 	AFFS_I(inode)->i_lastalloc = 0;
311 	AFFS_I(inode)->i_pa_cnt = 0;
312 	AFFS_I(inode)->i_extcnt = 1;
313 	AFFS_I(inode)->i_ext_last = ~1;
314 
315 	insert_inode_hash(inode);
316 
317 	return inode;
318 
319 err_bh:
320 	affs_free_block(sb, block);
321 err_block:
322 	iput(inode);
323 err_inode:
324 	return NULL;
325 }
326 
327 /*
328  * Add an entry to a directory. Create the header block
329  * and insert it into the hash table.
330  */
331 
332 int
333 affs_add_entry(struct inode *dir, struct inode *inode, struct dentry *dentry, s32 type)
334 {
335 	struct super_block *sb = dir->i_sb;
336 	struct buffer_head *inode_bh = NULL;
337 	struct buffer_head *bh = NULL;
338 	u32 block = 0;
339 	int retval;
340 
341 	pr_debug("AFFS: add_entry(dir=%u, inode=%u, \"%*s\", type=%d)\n", (u32)dir->i_ino,
342 	         (u32)inode->i_ino, (int)dentry->d_name.len, dentry->d_name.name, type);
343 
344 	retval = -EIO;
345 	bh = affs_bread(sb, inode->i_ino);
346 	if (!bh)
347 		goto done;
348 
349 	affs_lock_link(inode);
350 	switch (type) {
351 	case ST_LINKFILE:
352 	case ST_LINKDIR:
353 		retval = -ENOSPC;
354 		block = affs_alloc_block(dir, dir->i_ino);
355 		if (!block)
356 			goto err;
357 		retval = -EIO;
358 		inode_bh = bh;
359 		bh = affs_getzeroblk(sb, block);
360 		if (!bh)
361 			goto err;
362 		break;
363 	default:
364 		break;
365 	}
366 
367 	AFFS_HEAD(bh)->ptype = cpu_to_be32(T_SHORT);
368 	AFFS_HEAD(bh)->key = cpu_to_be32(bh->b_blocknr);
369 	affs_copy_name(AFFS_TAIL(sb, bh)->name, dentry);
370 	AFFS_TAIL(sb, bh)->stype = cpu_to_be32(type);
371 	AFFS_TAIL(sb, bh)->parent = cpu_to_be32(dir->i_ino);
372 
373 	if (inode_bh) {
374 		__be32 chain;
375 	       	chain = AFFS_TAIL(sb, inode_bh)->link_chain;
376 		AFFS_TAIL(sb, bh)->original = cpu_to_be32(inode->i_ino);
377 		AFFS_TAIL(sb, bh)->link_chain = chain;
378 		AFFS_TAIL(sb, inode_bh)->link_chain = cpu_to_be32(block);
379 		affs_adjust_checksum(inode_bh, block - be32_to_cpu(chain));
380 		mark_buffer_dirty_inode(inode_bh, inode);
381 		inode->i_nlink = 2;
382 		atomic_inc(&inode->i_count);
383 	}
384 	affs_fix_checksum(sb, bh);
385 	mark_buffer_dirty_inode(bh, inode);
386 	dentry->d_fsdata = (void *)(long)bh->b_blocknr;
387 
388 	affs_lock_dir(dir);
389 	retval = affs_insert_hash(dir, bh);
390 	mark_buffer_dirty_inode(bh, inode);
391 	affs_unlock_dir(dir);
392 	affs_unlock_link(inode);
393 
394 	d_instantiate(dentry, inode);
395 done:
396 	affs_brelse(inode_bh);
397 	affs_brelse(bh);
398 	return retval;
399 err:
400 	if (block)
401 		affs_free_block(sb, block);
402 	affs_unlock_link(inode);
403 	goto done;
404 }
405