blob: eb8b4baf0f2e0630d60a11cbac4d2efc976df209 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/fs/hpfs/inode.c
4 *
5 * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
6 *
7 * inode VFS functions
8 */
9
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Eric W. Biederman0e1a43c2012-02-07 16:27:53 -080011#include <linux/user_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "hpfs_fn.h"
13
14void hpfs_init_inode(struct inode *i)
15{
16 struct super_block *sb = i->i_sb;
17 struct hpfs_inode_info *hpfs_inode = hpfs_i(i);
18
19 i->i_uid = hpfs_sb(sb)->sb_uid;
20 i->i_gid = hpfs_sb(sb)->sb_gid;
21 i->i_mode = hpfs_sb(sb)->sb_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 i->i_size = -1;
23 i->i_blocks = -1;
24
25 hpfs_inode->i_dno = 0;
26 hpfs_inode->i_n_secs = 0;
27 hpfs_inode->i_file_sec = 0;
28 hpfs_inode->i_disk_sec = 0;
29 hpfs_inode->i_dpos = 0;
30 hpfs_inode->i_dsubdno = 0;
31 hpfs_inode->i_ea_mode = 0;
32 hpfs_inode->i_ea_uid = 0;
33 hpfs_inode->i_ea_gid = 0;
34 hpfs_inode->i_ea_size = 0;
35
36 hpfs_inode->i_rddir_off = NULL;
37 hpfs_inode->i_dirty = 0;
38
39 i->i_ctime.tv_sec = i->i_ctime.tv_nsec = 0;
40 i->i_mtime.tv_sec = i->i_mtime.tv_nsec = 0;
41 i->i_atime.tv_sec = i->i_atime.tv_nsec = 0;
42}
43
44void hpfs_read_inode(struct inode *i)
45{
46 struct buffer_head *bh;
47 struct fnode *fnode;
48 struct super_block *sb = i->i_sb;
49 struct hpfs_inode_info *hpfs_inode = hpfs_i(i);
Al Viro7e7742e2010-01-31 17:09:29 -050050 void *ea;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 int ea_size;
52
53 if (!(fnode = hpfs_map_fnode(sb, i->i_ino, &bh))) {
54 /*i->i_mode |= S_IFREG;
55 i->i_mode &= ~0111;
56 i->i_op = &hpfs_file_iops;
57 i->i_fop = &hpfs_file_ops;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +020058 clear_nlink(i);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 make_bad_inode(i);
60 return;
61 }
62 if (hpfs_sb(i->i_sb)->sb_eas) {
63 if ((ea = hpfs_get_ea(i->i_sb, fnode, "UID", &ea_size))) {
64 if (ea_size == 2) {
Eric W. Biederman0e1a43c2012-02-07 16:27:53 -080065 i_uid_write(i, le16_to_cpu(*(__le16*)ea));
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 hpfs_inode->i_ea_uid = 1;
67 }
68 kfree(ea);
69 }
70 if ((ea = hpfs_get_ea(i->i_sb, fnode, "GID", &ea_size))) {
71 if (ea_size == 2) {
Eric W. Biederman0e1a43c2012-02-07 16:27:53 -080072 i_gid_write(i, le16_to_cpu(*(__le16*)ea));
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 hpfs_inode->i_ea_gid = 1;
74 }
75 kfree(ea);
76 }
77 if ((ea = hpfs_get_ea(i->i_sb, fnode, "SYMLINK", &ea_size))) {
78 kfree(ea);
79 i->i_mode = S_IFLNK | 0777;
80 i->i_op = &page_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -050081 inode_nohighmem(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 i->i_data.a_ops = &hpfs_symlink_aops;
Miklos Szeredibfe86842011-10-28 14:13:29 +020083 set_nlink(i, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 i->i_size = ea_size;
85 i->i_blocks = 1;
86 brelse(bh);
87 return;
88 }
89 if ((ea = hpfs_get_ea(i->i_sb, fnode, "MODE", &ea_size))) {
90 int rdev = 0;
91 umode_t mode = hpfs_sb(sb)->sb_mode;
92 if (ea_size == 2) {
Al Viro2ef10312005-12-24 14:31:53 -050093 mode = le16_to_cpu(*(__le16*)ea);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 hpfs_inode->i_ea_mode = 1;
95 }
96 kfree(ea);
97 i->i_mode = mode;
98 if (S_ISBLK(mode) || S_ISCHR(mode)) {
99 if ((ea = hpfs_get_ea(i->i_sb, fnode, "DEV", &ea_size))) {
100 if (ea_size == 4)
Al Viro2ef10312005-12-24 14:31:53 -0500101 rdev = le32_to_cpu(*(__le32*)ea);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 kfree(ea);
103 }
104 }
105 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode)) {
106 brelse(bh);
Miklos Szeredibfe86842011-10-28 14:13:29 +0200107 set_nlink(i, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 i->i_size = 0;
109 i->i_blocks = 1;
110 init_special_inode(i, mode,
111 new_decode_dev(rdev));
112 return;
113 }
114 }
115 }
Al Viroc4c99542012-04-06 14:30:07 -0400116 if (fnode_is_dir(fnode)) {
Al Viro7e7742e2010-01-31 17:09:29 -0500117 int n_dnodes, n_subdirs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 i->i_mode |= S_IFDIR;
119 i->i_op = &hpfs_dir_iops;
120 i->i_fop = &hpfs_dir_ops;
Mikulas Patocka0b697602011-05-08 20:44:26 +0200121 hpfs_inode->i_parent_dir = le32_to_cpu(fnode->up);
122 hpfs_inode->i_dno = le32_to_cpu(fnode->u.external[0].disk_secno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 if (hpfs_sb(sb)->sb_chk >= 2) {
124 struct buffer_head *bh0;
125 if (hpfs_map_fnode(sb, hpfs_inode->i_parent_dir, &bh0)) brelse(bh0);
126 }
127 n_dnodes = 0; n_subdirs = 0;
128 hpfs_count_dnodes(i->i_sb, hpfs_inode->i_dno, &n_dnodes, &n_subdirs, NULL);
129 i->i_blocks = 4 * n_dnodes;
130 i->i_size = 2048 * n_dnodes;
Miklos Szeredibfe86842011-10-28 14:13:29 +0200131 set_nlink(i, 2 + n_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 } else {
133 i->i_mode |= S_IFREG;
134 if (!hpfs_inode->i_ea_mode) i->i_mode &= ~0111;
135 i->i_op = &hpfs_file_iops;
136 i->i_fop = &hpfs_file_ops;
Miklos Szeredibfe86842011-10-28 14:13:29 +0200137 set_nlink(i, 1);
Mikulas Patocka0b697602011-05-08 20:44:26 +0200138 i->i_size = le32_to_cpu(fnode->file_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 i->i_blocks = ((i->i_size + 511) >> 9) + 1;
140 i->i_data.a_ops = &hpfs_aops;
141 hpfs_i(i)->mmu_private = i->i_size;
142 }
143 brelse(bh);
144}
145
146static void hpfs_write_inode_ea(struct inode *i, struct fnode *fnode)
147{
148 struct hpfs_inode_info *hpfs_inode = hpfs_i(i);
Mikulas Patocka0b697602011-05-08 20:44:26 +0200149 /*if (le32_to_cpu(fnode->acl_size_l) || le16_to_cpu(fnode->acl_size_s)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 Some unknown structures like ACL may be in fnode,
151 we'd better not overwrite them
Masanari Iida8a168ca2012-12-29 02:00:09 +0900152 hpfs_error(i->i_sb, "fnode %08x has some unknown HPFS386 structures", i->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 } else*/ if (hpfs_sb(i->i_sb)->sb_eas >= 2) {
Al Viro2ef10312005-12-24 14:31:53 -0500154 __le32 ea;
Eric W. Biederman0e1a43c2012-02-07 16:27:53 -0800155 if (!uid_eq(i->i_uid, hpfs_sb(i->i_sb)->sb_uid) || hpfs_inode->i_ea_uid) {
156 ea = cpu_to_le32(i_uid_read(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 hpfs_set_ea(i, fnode, "UID", (char*)&ea, 2);
158 hpfs_inode->i_ea_uid = 1;
159 }
Eric W. Biederman0e1a43c2012-02-07 16:27:53 -0800160 if (!gid_eq(i->i_gid, hpfs_sb(i->i_sb)->sb_gid) || hpfs_inode->i_ea_gid) {
161 ea = cpu_to_le32(i_gid_read(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 hpfs_set_ea(i, fnode, "GID", (char *)&ea, 2);
163 hpfs_inode->i_ea_gid = 1;
164 }
165 if (!S_ISLNK(i->i_mode))
166 if ((i->i_mode != ((hpfs_sb(i->i_sb)->sb_mode & ~(S_ISDIR(i->i_mode) ? 0 : 0111))
167 | (S_ISDIR(i->i_mode) ? S_IFDIR : S_IFREG))
168 && i->i_mode != ((hpfs_sb(i->i_sb)->sb_mode & ~(S_ISDIR(i->i_mode) ? 0222 : 0333))
169 | (S_ISDIR(i->i_mode) ? S_IFDIR : S_IFREG))) || hpfs_inode->i_ea_mode) {
170 ea = cpu_to_le32(i->i_mode);
Al Viro2ef10312005-12-24 14:31:53 -0500171 /* sick, but legal */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 hpfs_set_ea(i, fnode, "MODE", (char *)&ea, 2);
173 hpfs_inode->i_ea_mode = 1;
174 }
175 if (S_ISBLK(i->i_mode) || S_ISCHR(i->i_mode)) {
176 ea = cpu_to_le32(new_encode_dev(i->i_rdev));
177 hpfs_set_ea(i, fnode, "DEV", (char *)&ea, 4);
178 }
179 }
180}
181
182void hpfs_write_inode(struct inode *i)
183{
184 struct hpfs_inode_info *hpfs_inode = hpfs_i(i);
185 struct inode *parent;
186 if (i->i_ino == hpfs_sb(i->i_sb)->sb_root) return;
187 if (hpfs_inode->i_rddir_off && !atomic_read(&i->i_count)) {
Fabian Frederickb7cb1ce2014-06-06 14:36:34 -0700188 if (*hpfs_inode->i_rddir_off)
Fabian Fredericka19189e2014-06-06 14:36:36 -0700189 pr_err("write_inode: some position still there\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 kfree(hpfs_inode->i_rddir_off);
191 hpfs_inode->i_rddir_off = NULL;
192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (!i->i_nlink) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return;
195 }
196 parent = iget_locked(i->i_sb, hpfs_inode->i_parent_dir);
197 if (parent) {
198 hpfs_inode->i_dirty = 0;
199 if (parent->i_state & I_NEW) {
200 hpfs_init_inode(parent);
201 hpfs_read_inode(parent);
202 unlock_new_inode(parent);
203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 hpfs_write_inode_nolock(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 iput(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
209void hpfs_write_inode_nolock(struct inode *i)
210{
211 struct hpfs_inode_info *hpfs_inode = hpfs_i(i);
212 struct buffer_head *bh;
213 struct fnode *fnode;
214 struct quad_buffer_head qbh;
215 struct hpfs_dirent *de;
216 if (i->i_ino == hpfs_sb(i->i_sb)->sb_root) return;
217 if (!(fnode = hpfs_map_fnode(i->i_sb, i->i_ino, &bh))) return;
218 if (i->i_ino != hpfs_sb(i->i_sb)->sb_root && i->i_nlink) {
219 if (!(de = map_fnode_dirent(i->i_sb, i->i_ino, fnode, &qbh))) {
220 brelse(bh);
221 return;
222 }
223 } else de = NULL;
224 if (S_ISREG(i->i_mode)) {
Mikulas Patocka0b697602011-05-08 20:44:26 +0200225 fnode->file_size = cpu_to_le32(i->i_size);
226 if (de) de->file_size = cpu_to_le32(i->i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 } else if (S_ISDIR(i->i_mode)) {
Mikulas Patocka0b697602011-05-08 20:44:26 +0200228 fnode->file_size = cpu_to_le32(0);
229 if (de) de->file_size = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
231 hpfs_write_inode_ea(i, fnode);
232 if (de) {
Mikulas Patocka0b697602011-05-08 20:44:26 +0200233 de->write_date = cpu_to_le32(gmt_to_local(i->i_sb, i->i_mtime.tv_sec));
234 de->read_date = cpu_to_le32(gmt_to_local(i->i_sb, i->i_atime.tv_sec));
235 de->creation_date = cpu_to_le32(gmt_to_local(i->i_sb, i->i_ctime.tv_sec));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 de->read_only = !(i->i_mode & 0222);
Mikulas Patocka0b697602011-05-08 20:44:26 +0200237 de->ea_size = cpu_to_le32(hpfs_inode->i_ea_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 hpfs_mark_4buffers_dirty(&qbh);
239 hpfs_brelse4(&qbh);
240 }
241 if (S_ISDIR(i->i_mode)) {
242 if ((de = map_dirent(i, hpfs_inode->i_dno, "\001\001", 2, NULL, &qbh))) {
Mikulas Patocka0b697602011-05-08 20:44:26 +0200243 de->write_date = cpu_to_le32(gmt_to_local(i->i_sb, i->i_mtime.tv_sec));
244 de->read_date = cpu_to_le32(gmt_to_local(i->i_sb, i->i_atime.tv_sec));
245 de->creation_date = cpu_to_le32(gmt_to_local(i->i_sb, i->i_ctime.tv_sec));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 de->read_only = !(i->i_mode & 0222);
Mikulas Patocka0b697602011-05-08 20:44:26 +0200247 de->ea_size = cpu_to_le32(/*hpfs_inode->i_ea_size*/0);
248 de->file_size = cpu_to_le32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 hpfs_mark_4buffers_dirty(&qbh);
250 hpfs_brelse4(&qbh);
Randy Dunlap18debbb2006-12-06 20:37:05 -0800251 } else
252 hpfs_error(i->i_sb,
253 "directory %08lx doesn't have '.' entry",
254 (unsigned long)i->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 }
256 mark_buffer_dirty(bh);
257 brelse(bh);
258}
259
Christoph Hellwigca30bc92008-08-11 00:27:59 +0200260int hpfs_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
David Howells2b0143b2015-03-17 22:25:59 +0000262 struct inode *inode = d_inode(dentry);
Christoph Hellwigca30bc92008-08-11 00:27:59 +0200263 int error = -EINVAL;
264
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100265 hpfs_lock(inode->i_sb);
Christoph Hellwigca30bc92008-08-11 00:27:59 +0200266 if (inode->i_ino == hpfs_sb(inode->i_sb)->sb_root)
267 goto out_unlock;
Eric W. Biederman0e1a43c2012-02-07 16:27:53 -0800268 if ((attr->ia_valid & ATTR_UID) &&
269 from_kuid(&init_user_ns, attr->ia_uid) >= 0x10000)
Mikulas Patocka48f10e82011-05-08 20:44:00 +0200270 goto out_unlock;
Eric W. Biederman0e1a43c2012-02-07 16:27:53 -0800271 if ((attr->ia_valid & ATTR_GID) &&
272 from_kgid(&init_user_ns, attr->ia_gid) >= 0x10000)
Mikulas Patocka48f10e82011-05-08 20:44:00 +0200273 goto out_unlock;
Christoph Hellwigca30bc92008-08-11 00:27:59 +0200274 if ((attr->ia_valid & ATTR_SIZE) && attr->ia_size > inode->i_size)
275 goto out_unlock;
276
Jan Kara31051c82016-05-26 16:55:18 +0200277 error = setattr_prepare(dentry, attr);
Christoph Hellwigca30bc92008-08-11 00:27:59 +0200278 if (error)
279 goto out_unlock;
280
Christoph Hellwig10257742010-06-04 11:30:02 +0200281 if ((attr->ia_valid & ATTR_SIZE) &&
282 attr->ia_size != i_size_read(inode)) {
Marco Stornelli70b31c42012-12-15 11:53:50 +0100283 error = inode_newsize_ok(inode, attr->ia_size);
Christoph Hellwig10257742010-06-04 11:30:02 +0200284 if (error)
Dr. David Alan Gilbert274052e2010-12-13 17:09:52 +0000285 goto out_unlock;
Marco Stornelli70b31c42012-12-15 11:53:50 +0100286
287 truncate_setsize(inode, attr->ia_size);
288 hpfs_truncate(inode);
Christoph Hellwig10257742010-06-04 11:30:02 +0200289 }
290
291 setattr_copy(inode, attr);
Christoph Hellwigca30bc92008-08-11 00:27:59 +0200292
293 hpfs_write_inode(inode);
294
295 out_unlock:
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100296 hpfs_unlock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return error;
298}
299
300void hpfs_write_if_changed(struct inode *inode)
301{
302 struct hpfs_inode_info *hpfs_inode = hpfs_i(inode);
303
304 if (hpfs_inode->i_dirty)
305 hpfs_write_inode(inode);
306}
307
Al Viroea544002010-06-07 00:18:40 -0400308void hpfs_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700310 truncate_inode_pages_final(&inode->i_data);
Jan Karadbd57682012-05-03 14:48:02 +0200311 clear_inode(inode);
Al Viroea544002010-06-07 00:18:40 -0400312 if (!inode->i_nlink) {
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100313 hpfs_lock(inode->i_sb);
Al Viroea544002010-06-07 00:18:40 -0400314 hpfs_remove_fnode(inode->i_sb, inode->i_ino);
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100315 hpfs_unlock(inode->i_sb);
Al Viroea544002010-06-07 00:18:40 -0400316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}