xref: /openbmc/linux/fs/affs/inode.c (revision aac5987a)
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 <linux/cred.h>
14 #include <linux/gfp.h>
15 #include "affs.h"
16 
17 struct inode *affs_iget(struct super_block *sb, unsigned long ino)
18 {
19 	struct affs_sb_info	*sbi = AFFS_SB(sb);
20 	struct buffer_head	*bh;
21 	struct affs_tail	*tail;
22 	struct inode		*inode;
23 	u32			 block;
24 	u32			 size;
25 	u32			 prot;
26 	u16			 id;
27 
28 	inode = iget_locked(sb, ino);
29 	if (!inode)
30 		return ERR_PTR(-ENOMEM);
31 	if (!(inode->i_state & I_NEW))
32 		return inode;
33 
34 	pr_debug("affs_iget(%lu)\n", inode->i_ino);
35 
36 	block = inode->i_ino;
37 	bh = affs_bread(sb, block);
38 	if (!bh) {
39 		affs_warning(sb, "read_inode", "Cannot read block %d", block);
40 		goto bad_inode;
41 	}
42 	if (affs_checksum_block(sb, bh) || be32_to_cpu(AFFS_HEAD(bh)->ptype) != T_SHORT) {
43 		affs_warning(sb,"read_inode",
44 			   "Checksum or type (ptype=%d) error on inode %d",
45 			   AFFS_HEAD(bh)->ptype, block);
46 		goto bad_inode;
47 	}
48 
49 	tail = AFFS_TAIL(sb, bh);
50 	prot = be32_to_cpu(tail->protect);
51 
52 	inode->i_size = 0;
53 	set_nlink(inode, 1);
54 	inode->i_mode = 0;
55 	AFFS_I(inode)->i_extcnt = 1;
56 	AFFS_I(inode)->i_ext_last = ~1;
57 	AFFS_I(inode)->i_protect = prot;
58 	atomic_set(&AFFS_I(inode)->i_opencnt, 0);
59 	AFFS_I(inode)->i_blkcnt = 0;
60 	AFFS_I(inode)->i_lc = NULL;
61 	AFFS_I(inode)->i_lc_size = 0;
62 	AFFS_I(inode)->i_lc_shift = 0;
63 	AFFS_I(inode)->i_lc_mask = 0;
64 	AFFS_I(inode)->i_ac = NULL;
65 	AFFS_I(inode)->i_ext_bh = NULL;
66 	AFFS_I(inode)->mmu_private = 0;
67 	AFFS_I(inode)->i_lastalloc = 0;
68 	AFFS_I(inode)->i_pa_cnt = 0;
69 
70 	if (affs_test_opt(sbi->s_flags, SF_SETMODE))
71 		inode->i_mode = sbi->s_mode;
72 	else
73 		inode->i_mode = affs_prot_to_mode(prot);
74 
75 	id = be16_to_cpu(tail->uid);
76 	if (id == 0 || affs_test_opt(sbi->s_flags, SF_SETUID))
77 		inode->i_uid = sbi->s_uid;
78 	else if (id == 0xFFFF && affs_test_opt(sbi->s_flags, SF_MUFS))
79 		i_uid_write(inode, 0);
80 	else
81 		i_uid_write(inode, id);
82 
83 	id = be16_to_cpu(tail->gid);
84 	if (id == 0 || affs_test_opt(sbi->s_flags, SF_SETGID))
85 		inode->i_gid = sbi->s_gid;
86 	else if (id == 0xFFFF && affs_test_opt(sbi->s_flags, SF_MUFS))
87 		i_gid_write(inode, 0);
88 	else
89 		i_gid_write(inode, id);
90 
91 	switch (be32_to_cpu(tail->stype)) {
92 	case ST_ROOT:
93 		inode->i_uid = sbi->s_uid;
94 		inode->i_gid = sbi->s_gid;
95 		/* fall through */
96 	case ST_USERDIR:
97 		if (be32_to_cpu(tail->stype) == ST_USERDIR ||
98 		    affs_test_opt(sbi->s_flags, SF_SETMODE)) {
99 			if (inode->i_mode & S_IRUSR)
100 				inode->i_mode |= S_IXUSR;
101 			if (inode->i_mode & S_IRGRP)
102 				inode->i_mode |= S_IXGRP;
103 			if (inode->i_mode & S_IROTH)
104 				inode->i_mode |= S_IXOTH;
105 			inode->i_mode |= S_IFDIR;
106 		} else
107 			inode->i_mode = S_IRUGO | S_IXUGO | S_IWUSR | S_IFDIR;
108 		/* Maybe it should be controlled by mount parameter? */
109 		//inode->i_mode |= S_ISVTX;
110 		inode->i_op = &affs_dir_inode_operations;
111 		inode->i_fop = &affs_dir_operations;
112 		break;
113 	case ST_LINKDIR:
114 #if 0
115 		affs_warning(sb, "read_inode", "inode is LINKDIR");
116 		goto bad_inode;
117 #else
118 		inode->i_mode |= S_IFDIR;
119 		/* ... and leave ->i_op and ->i_fop pointing to empty */
120 		break;
121 #endif
122 	case ST_LINKFILE:
123 		affs_warning(sb, "read_inode", "inode is LINKFILE");
124 		goto bad_inode;
125 	case ST_FILE:
126 		size = be32_to_cpu(tail->size);
127 		inode->i_mode |= S_IFREG;
128 		AFFS_I(inode)->mmu_private = inode->i_size = size;
129 		if (inode->i_size) {
130 			AFFS_I(inode)->i_blkcnt = (size - 1) /
131 					       sbi->s_data_blksize + 1;
132 			AFFS_I(inode)->i_extcnt = (AFFS_I(inode)->i_blkcnt - 1) /
133 					       sbi->s_hashsize + 1;
134 		}
135 		if (tail->link_chain)
136 			set_nlink(inode, 2);
137 		inode->i_mapping->a_ops = affs_test_opt(sbi->s_flags, SF_OFS) ?
138 					  &affs_aops_ofs : &affs_aops;
139 		inode->i_op = &affs_file_inode_operations;
140 		inode->i_fop = &affs_file_operations;
141 		break;
142 	case ST_SOFTLINK:
143 		inode->i_mode |= S_IFLNK;
144 		inode_nohighmem(inode);
145 		inode->i_op = &affs_symlink_inode_operations;
146 		inode->i_data.a_ops = &affs_symlink_aops;
147 		break;
148 	}
149 
150 	inode->i_mtime.tv_sec = inode->i_atime.tv_sec = inode->i_ctime.tv_sec
151 		       = (be32_to_cpu(tail->change.days) * (24 * 60 * 60) +
152 		         be32_to_cpu(tail->change.mins) * 60 +
153 			 be32_to_cpu(tail->change.ticks) / 50 +
154 			 ((8 * 365 + 2) * 24 * 60 * 60)) +
155 			 sys_tz.tz_minuteswest * 60;
156 	inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = inode->i_atime.tv_nsec = 0;
157 	affs_brelse(bh);
158 	unlock_new_inode(inode);
159 	return inode;
160 
161 bad_inode:
162 	affs_brelse(bh);
163 	iget_failed(inode);
164 	return ERR_PTR(-EIO);
165 }
166 
167 int
168 affs_write_inode(struct inode *inode, struct writeback_control *wbc)
169 {
170 	struct super_block	*sb = inode->i_sb;
171 	struct buffer_head	*bh;
172 	struct affs_tail	*tail;
173 	uid_t			 uid;
174 	gid_t			 gid;
175 
176 	pr_debug("write_inode(%lu)\n", inode->i_ino);
177 
178 	if (!inode->i_nlink)
179 		// possibly free block
180 		return 0;
181 	bh = affs_bread(sb, inode->i_ino);
182 	if (!bh) {
183 		affs_error(sb,"write_inode","Cannot read block %lu",inode->i_ino);
184 		return -EIO;
185 	}
186 	tail = AFFS_TAIL(sb, bh);
187 	if (tail->stype == cpu_to_be32(ST_ROOT)) {
188 		affs_secs_to_datestamp(inode->i_mtime.tv_sec,
189 				       &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 		affs_secs_to_datestamp(inode->i_mtime.tv_sec, &tail->change);
194 		if (!(inode->i_ino == AFFS_SB(sb)->s_root_block)) {
195 			uid = i_uid_read(inode);
196 			gid = i_gid_read(inode);
197 			if (affs_test_opt(AFFS_SB(sb)->s_flags, SF_MUFS)) {
198 				if (uid == 0 || uid == 0xFFFF)
199 					uid = uid ^ ~0;
200 				if (gid == 0 || gid == 0xFFFF)
201 					gid = gid ^ ~0;
202 			}
203 			if (!affs_test_opt(AFFS_SB(sb)->s_flags, SF_SETUID))
204 				tail->uid = cpu_to_be16(uid);
205 			if (!affs_test_opt(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 = d_inode(dentry);
220 	int error;
221 
222 	pr_debug("notify_change(%lu,0x%x)\n", inode->i_ino, attr->ia_valid);
223 
224 	error = setattr_prepare(dentry, attr);
225 	if (error)
226 		goto out;
227 
228 	if (((attr->ia_valid & ATTR_UID) &&
229 	      affs_test_opt(AFFS_SB(inode->i_sb)->s_flags, SF_SETUID)) ||
230 	    ((attr->ia_valid & ATTR_GID) &&
231 	      affs_test_opt(AFFS_SB(inode->i_sb)->s_flags, SF_SETGID)) ||
232 	    ((attr->ia_valid & ATTR_MODE) &&
233 	     (AFFS_SB(inode->i_sb)->s_flags &
234 	      (AFFS_MOUNT_SF_SETMODE | AFFS_MOUNT_SF_IMMUTABLE)))) {
235 		if (!affs_test_opt(AFFS_SB(inode->i_sb)->s_flags, SF_QUIET))
236 			error = -EPERM;
237 		goto out;
238 	}
239 
240 	if ((attr->ia_valid & ATTR_SIZE) &&
241 	    attr->ia_size != i_size_read(inode)) {
242 		error = inode_newsize_ok(inode, attr->ia_size);
243 		if (error)
244 			return error;
245 
246 		truncate_setsize(inode, attr->ia_size);
247 		affs_truncate(inode);
248 	}
249 
250 	setattr_copy(inode, attr);
251 	mark_inode_dirty(inode);
252 
253 	if (attr->ia_valid & ATTR_MODE)
254 		affs_mode_to_prot(inode);
255 out:
256 	return error;
257 }
258 
259 void
260 affs_evict_inode(struct inode *inode)
261 {
262 	unsigned long cache_page;
263 	pr_debug("evict_inode(ino=%lu, nlink=%u)\n",
264 		 inode->i_ino, inode->i_nlink);
265 	truncate_inode_pages_final(&inode->i_data);
266 
267 	if (!inode->i_nlink) {
268 		inode->i_size = 0;
269 		affs_truncate(inode);
270 	}
271 
272 	invalidate_inode_buffers(inode);
273 	clear_inode(inode);
274 	affs_free_prealloc(inode);
275 	cache_page = (unsigned long)AFFS_I(inode)->i_lc;
276 	if (cache_page) {
277 		pr_debug("freeing ext cache\n");
278 		AFFS_I(inode)->i_lc = NULL;
279 		AFFS_I(inode)->i_ac = NULL;
280 		free_page(cache_page);
281 	}
282 	affs_brelse(AFFS_I(inode)->i_ext_bh);
283 	AFFS_I(inode)->i_ext_last = ~1;
284 	AFFS_I(inode)->i_ext_bh = NULL;
285 
286 	if (!inode->i_nlink)
287 		affs_free_block(inode->i_sb, inode->i_ino);
288 }
289 
290 struct inode *
291 affs_new_inode(struct inode *dir)
292 {
293 	struct super_block	*sb = dir->i_sb;
294 	struct inode		*inode;
295 	u32			 block;
296 	struct buffer_head	*bh;
297 
298 	if (!(inode = new_inode(sb)))
299 		goto err_inode;
300 
301 	if (!(block = affs_alloc_block(dir, dir->i_ino)))
302 		goto err_block;
303 
304 	bh = affs_getzeroblk(sb, block);
305 	if (!bh)
306 		goto err_bh;
307 	mark_buffer_dirty_inode(bh, inode);
308 	affs_brelse(bh);
309 
310 	inode->i_uid     = current_fsuid();
311 	inode->i_gid     = current_fsgid();
312 	inode->i_ino     = block;
313 	set_nlink(inode, 1);
314 	inode->i_mtime   = inode->i_atime = inode->i_ctime = current_time(inode);
315 	atomic_set(&AFFS_I(inode)->i_opencnt, 0);
316 	AFFS_I(inode)->i_blkcnt = 0;
317 	AFFS_I(inode)->i_lc = NULL;
318 	AFFS_I(inode)->i_lc_size = 0;
319 	AFFS_I(inode)->i_lc_shift = 0;
320 	AFFS_I(inode)->i_lc_mask = 0;
321 	AFFS_I(inode)->i_ac = NULL;
322 	AFFS_I(inode)->i_ext_bh = NULL;
323 	AFFS_I(inode)->mmu_private = 0;
324 	AFFS_I(inode)->i_protect = 0;
325 	AFFS_I(inode)->i_lastalloc = 0;
326 	AFFS_I(inode)->i_pa_cnt = 0;
327 	AFFS_I(inode)->i_extcnt = 1;
328 	AFFS_I(inode)->i_ext_last = ~1;
329 
330 	insert_inode_hash(inode);
331 
332 	return inode;
333 
334 err_bh:
335 	affs_free_block(sb, block);
336 err_block:
337 	iput(inode);
338 err_inode:
339 	return NULL;
340 }
341 
342 /*
343  * Add an entry to a directory. Create the header block
344  * and insert it into the hash table.
345  */
346 
347 int
348 affs_add_entry(struct inode *dir, struct inode *inode, struct dentry *dentry, s32 type)
349 {
350 	struct super_block *sb = dir->i_sb;
351 	struct buffer_head *inode_bh = NULL;
352 	struct buffer_head *bh;
353 	u32 block = 0;
354 	int retval;
355 
356 	pr_debug("%s(dir=%lu, inode=%lu, \"%pd\", type=%d)\n", __func__,
357 		 dir->i_ino, inode->i_ino, dentry, type);
358 
359 	retval = -EIO;
360 	bh = affs_bread(sb, inode->i_ino);
361 	if (!bh)
362 		goto done;
363 
364 	affs_lock_link(inode);
365 	switch (type) {
366 	case ST_LINKFILE:
367 	case ST_LINKDIR:
368 		retval = -ENOSPC;
369 		block = affs_alloc_block(dir, dir->i_ino);
370 		if (!block)
371 			goto err;
372 		retval = -EIO;
373 		inode_bh = bh;
374 		bh = affs_getzeroblk(sb, block);
375 		if (!bh)
376 			goto err;
377 		break;
378 	default:
379 		break;
380 	}
381 
382 	AFFS_HEAD(bh)->ptype = cpu_to_be32(T_SHORT);
383 	AFFS_HEAD(bh)->key = cpu_to_be32(bh->b_blocknr);
384 	affs_copy_name(AFFS_TAIL(sb, bh)->name, dentry);
385 	AFFS_TAIL(sb, bh)->stype = cpu_to_be32(type);
386 	AFFS_TAIL(sb, bh)->parent = cpu_to_be32(dir->i_ino);
387 
388 	if (inode_bh) {
389 		__be32 chain;
390 	       	chain = AFFS_TAIL(sb, inode_bh)->link_chain;
391 		AFFS_TAIL(sb, bh)->original = cpu_to_be32(inode->i_ino);
392 		AFFS_TAIL(sb, bh)->link_chain = chain;
393 		AFFS_TAIL(sb, inode_bh)->link_chain = cpu_to_be32(block);
394 		affs_adjust_checksum(inode_bh, block - be32_to_cpu(chain));
395 		mark_buffer_dirty_inode(inode_bh, inode);
396 		set_nlink(inode, 2);
397 		ihold(inode);
398 	}
399 	affs_fix_checksum(sb, bh);
400 	mark_buffer_dirty_inode(bh, inode);
401 	dentry->d_fsdata = (void *)(long)bh->b_blocknr;
402 
403 	affs_lock_dir(dir);
404 	retval = affs_insert_hash(dir, bh);
405 	mark_buffer_dirty_inode(bh, inode);
406 	affs_unlock_dir(dir);
407 	affs_unlock_link(inode);
408 
409 	d_instantiate(dentry, inode);
410 done:
411 	affs_brelse(inode_bh);
412 	affs_brelse(bh);
413 	return retval;
414 err:
415 	if (block)
416 		affs_free_block(sb, block);
417 	affs_unlock_link(inode);
418 	goto done;
419 }
420