blob: d73f8a67168e981886a7dfa8529e2b81c6790db0 [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/namei.c
4 *
5 * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
6 *
7 * adding & removing files & directories
8 */
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +04009#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include "hpfs_fn.h"
11
Mikulas Patockaf49a26e2015-09-02 22:51:53 +020012static void hpfs_update_directory_times(struct inode *dir)
13{
Arnd Bergmannf08957d02018-08-17 15:43:54 -070014 time64_t t = local_to_gmt(dir->i_sb, local_get_seconds(dir->i_sb));
Mikulas Patockaf49a26e2015-09-02 22:51:53 +020015 if (t == dir->i_mtime.tv_sec &&
16 t == dir->i_ctime.tv_sec)
17 return;
18 dir->i_mtime.tv_sec = dir->i_ctime.tv_sec = t;
19 dir->i_mtime.tv_nsec = dir->i_ctime.tv_nsec = 0;
20 hpfs_write_inode_nolock(dir);
21}
22
Christian Brauner549c7292021-01-21 14:19:43 +010023static int hpfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
24 struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
Al Viro7e7742e2010-01-31 17:09:29 -050026 const unsigned char *name = dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 unsigned len = dentry->d_name.len;
28 struct quad_buffer_head qbh0;
29 struct buffer_head *bh;
30 struct hpfs_dirent *de;
31 struct fnode *fnode;
32 struct dnode *dnode;
33 struct inode *result;
34 fnode_secno fno;
35 dnode_secno dno;
36 int r;
37 struct hpfs_dirent dee;
38 int err;
Al Viro7e7742e2010-01-31 17:09:29 -050039 if ((err = hpfs_chk_name(name, &len))) return err==-ENOENT ? -EINVAL : err;
Arnd Bergmann9a311b92011-01-22 20:26:12 +010040 hpfs_lock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 err = -ENOSPC;
42 fnode = hpfs_alloc_fnode(dir->i_sb, hpfs_i(dir)->i_dno, &fno, &bh);
43 if (!fnode)
44 goto bail;
Mikulas Patocka7d23ce32011-05-08 20:43:06 +020045 dnode = hpfs_alloc_dnode(dir->i_sb, fno, &dno, &qbh0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 if (!dnode)
47 goto bail1;
48 memset(&dee, 0, sizeof dee);
49 dee.directory = 1;
50 if (!(mode & 0222)) dee.read_only = 1;
51 /*dee.archive = 0;*/
52 dee.hidden = name[0] == '.';
Mikulas Patocka0b697602011-05-08 20:44:26 +020053 dee.fnode = cpu_to_le32(fno);
Arnd Bergmannf08957d02018-08-17 15:43:54 -070054 dee.creation_date = dee.write_date = dee.read_date = cpu_to_le32(local_get_seconds(dir->i_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 result = new_inode(dir->i_sb);
56 if (!result)
57 goto bail2;
58 hpfs_init_inode(result);
59 result->i_ino = fno;
60 hpfs_i(result)->i_parent_dir = dir->i_ino;
61 hpfs_i(result)->i_dno = dno;
Mikulas Patocka0b697602011-05-08 20:44:26 +020062 result->i_ctime.tv_sec = result->i_mtime.tv_sec = result->i_atime.tv_sec = local_to_gmt(dir->i_sb, le32_to_cpu(dee.creation_date));
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 result->i_ctime.tv_nsec = 0;
64 result->i_mtime.tv_nsec = 0;
65 result->i_atime.tv_nsec = 0;
66 hpfs_i(result)->i_ea_size = 0;
67 result->i_mode |= S_IFDIR;
68 result->i_op = &hpfs_dir_iops;
69 result->i_fop = &hpfs_dir_ops;
70 result->i_blocks = 4;
71 result->i_size = 2048;
Miklos Szeredibfe86842011-10-28 14:13:29 +020072 set_nlink(result, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 if (dee.read_only)
74 result->i_mode &= ~0222;
75
Mikulas Patocka7d23ce32011-05-08 20:43:06 +020076 r = hpfs_add_dirent(dir, name, len, &dee);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 if (r == 1)
78 goto bail3;
79 if (r == -1) {
80 err = -EEXIST;
81 goto bail3;
82 }
83 fnode->len = len;
84 memcpy(fnode->name, name, len > 15 ? 15 : len);
Mikulas Patocka0b697602011-05-08 20:44:26 +020085 fnode->up = cpu_to_le32(dir->i_ino);
Al Viroc4c99542012-04-06 14:30:07 -040086 fnode->flags |= FNODE_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 fnode->btree.n_free_nodes = 7;
88 fnode->btree.n_used_nodes = 1;
Mikulas Patocka0b697602011-05-08 20:44:26 +020089 fnode->btree.first_free = cpu_to_le16(0x14);
90 fnode->u.external[0].disk_secno = cpu_to_le32(dno);
91 fnode->u.external[0].file_secno = cpu_to_le32(-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 dnode->root_dnode = 1;
Mikulas Patocka0b697602011-05-08 20:44:26 +020093 dnode->up = cpu_to_le32(fno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 de = hpfs_add_de(dir->i_sb, dnode, "\001\001", 2, 0);
Arnd Bergmannf08957d02018-08-17 15:43:54 -070095 de->creation_date = de->write_date = de->read_date = cpu_to_le32(local_get_seconds(dir->i_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 if (!(mode & 0222)) de->read_only = 1;
97 de->first = de->directory = 1;
98 /*de->hidden = de->system = 0;*/
Mikulas Patocka0b697602011-05-08 20:44:26 +020099 de->fnode = cpu_to_le32(fno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 mark_buffer_dirty(bh);
101 brelse(bh);
102 hpfs_mark_4buffers_dirty(&qbh0);
103 hpfs_brelse4(&qbh0);
Dave Hansend8c76e62006-09-30 23:29:04 -0700104 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 insert_inode_hash(result);
106
Eric W. Biederman0e1a43c2012-02-07 16:27:53 -0800107 if (!uid_eq(result->i_uid, current_fsuid()) ||
108 !gid_eq(result->i_gid, current_fsgid()) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 result->i_mode != (mode | S_IFDIR)) {
David Howellsde395b82008-11-14 10:38:55 +1100110 result->i_uid = current_fsuid();
111 result->i_gid = current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 result->i_mode = mode | S_IFDIR;
113 hpfs_write_inode_nolock(result);
114 }
Mikulas Patockaf49a26e2015-09-02 22:51:53 +0200115 hpfs_update_directory_times(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 d_instantiate(dentry, result);
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100117 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return 0;
119bail3:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 iput(result);
121bail2:
122 hpfs_brelse4(&qbh0);
123 hpfs_free_dnode(dir->i_sb, dno);
124bail1:
125 brelse(bh);
126 hpfs_free_sectors(dir->i_sb, fno, 1);
127bail:
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100128 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 return err;
130}
131
Christian Brauner549c7292021-01-21 14:19:43 +0100132static int hpfs_create(struct user_namespace *mnt_userns, struct inode *dir,
133 struct dentry *dentry, umode_t mode, bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Al Viro7e7742e2010-01-31 17:09:29 -0500135 const unsigned char *name = dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 unsigned len = dentry->d_name.len;
137 struct inode *result = NULL;
138 struct buffer_head *bh;
139 struct fnode *fnode;
140 fnode_secno fno;
141 int r;
142 struct hpfs_dirent dee;
143 int err;
Al Viro7e7742e2010-01-31 17:09:29 -0500144 if ((err = hpfs_chk_name(name, &len)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return err==-ENOENT ? -EINVAL : err;
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100146 hpfs_lock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 err = -ENOSPC;
148 fnode = hpfs_alloc_fnode(dir->i_sb, hpfs_i(dir)->i_dno, &fno, &bh);
149 if (!fnode)
150 goto bail;
151 memset(&dee, 0, sizeof dee);
152 if (!(mode & 0222)) dee.read_only = 1;
153 dee.archive = 1;
154 dee.hidden = name[0] == '.';
Mikulas Patocka0b697602011-05-08 20:44:26 +0200155 dee.fnode = cpu_to_le32(fno);
Arnd Bergmannf08957d02018-08-17 15:43:54 -0700156 dee.creation_date = dee.write_date = dee.read_date = cpu_to_le32(local_get_seconds(dir->i_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 result = new_inode(dir->i_sb);
159 if (!result)
160 goto bail1;
161
162 hpfs_init_inode(result);
163 result->i_ino = fno;
164 result->i_mode |= S_IFREG;
165 result->i_mode &= ~0111;
166 result->i_op = &hpfs_file_iops;
167 result->i_fop = &hpfs_file_ops;
Miklos Szeredibfe86842011-10-28 14:13:29 +0200168 set_nlink(result, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 hpfs_i(result)->i_parent_dir = dir->i_ino;
Mikulas Patocka0b697602011-05-08 20:44:26 +0200170 result->i_ctime.tv_sec = result->i_mtime.tv_sec = result->i_atime.tv_sec = local_to_gmt(dir->i_sb, le32_to_cpu(dee.creation_date));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 result->i_ctime.tv_nsec = 0;
172 result->i_mtime.tv_nsec = 0;
173 result->i_atime.tv_nsec = 0;
174 hpfs_i(result)->i_ea_size = 0;
175 if (dee.read_only)
176 result->i_mode &= ~0222;
177 result->i_blocks = 1;
178 result->i_size = 0;
179 result->i_data.a_ops = &hpfs_aops;
180 hpfs_i(result)->mmu_private = 0;
181
Mikulas Patocka7d23ce32011-05-08 20:43:06 +0200182 r = hpfs_add_dirent(dir, name, len, &dee);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 if (r == 1)
184 goto bail2;
185 if (r == -1) {
186 err = -EEXIST;
187 goto bail2;
188 }
189 fnode->len = len;
190 memcpy(fnode->name, name, len > 15 ? 15 : len);
Mikulas Patocka0b697602011-05-08 20:44:26 +0200191 fnode->up = cpu_to_le32(dir->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 mark_buffer_dirty(bh);
193 brelse(bh);
194
195 insert_inode_hash(result);
196
Eric W. Biederman0e1a43c2012-02-07 16:27:53 -0800197 if (!uid_eq(result->i_uid, current_fsuid()) ||
198 !gid_eq(result->i_gid, current_fsgid()) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 result->i_mode != (mode | S_IFREG)) {
David Howellsde395b82008-11-14 10:38:55 +1100200 result->i_uid = current_fsuid();
201 result->i_gid = current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 result->i_mode = mode | S_IFREG;
203 hpfs_write_inode_nolock(result);
204 }
Mikulas Patockaf49a26e2015-09-02 22:51:53 +0200205 hpfs_update_directory_times(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 d_instantiate(dentry, result);
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100207 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return 0;
209
210bail2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 iput(result);
212bail1:
213 brelse(bh);
214 hpfs_free_sectors(dir->i_sb, fno, 1);
215bail:
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100216 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return err;
218}
219
Christian Brauner549c7292021-01-21 14:19:43 +0100220static int hpfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
221 struct dentry *dentry, umode_t mode, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
Al Viro7e7742e2010-01-31 17:09:29 -0500223 const unsigned char *name = dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 unsigned len = dentry->d_name.len;
225 struct buffer_head *bh;
226 struct fnode *fnode;
227 fnode_secno fno;
228 int r;
229 struct hpfs_dirent dee;
230 struct inode *result = NULL;
231 int err;
Al Viro7e7742e2010-01-31 17:09:29 -0500232 if ((err = hpfs_chk_name(name, &len))) return err==-ENOENT ? -EINVAL : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (hpfs_sb(dir->i_sb)->sb_eas < 2) return -EPERM;
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100234 hpfs_lock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 err = -ENOSPC;
236 fnode = hpfs_alloc_fnode(dir->i_sb, hpfs_i(dir)->i_dno, &fno, &bh);
237 if (!fnode)
238 goto bail;
239 memset(&dee, 0, sizeof dee);
240 if (!(mode & 0222)) dee.read_only = 1;
241 dee.archive = 1;
242 dee.hidden = name[0] == '.';
Mikulas Patocka0b697602011-05-08 20:44:26 +0200243 dee.fnode = cpu_to_le32(fno);
Arnd Bergmannf08957d02018-08-17 15:43:54 -0700244 dee.creation_date = dee.write_date = dee.read_date = cpu_to_le32(local_get_seconds(dir->i_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 result = new_inode(dir->i_sb);
247 if (!result)
248 goto bail1;
249
250 hpfs_init_inode(result);
251 result->i_ino = fno;
252 hpfs_i(result)->i_parent_dir = dir->i_ino;
Mikulas Patocka0b697602011-05-08 20:44:26 +0200253 result->i_ctime.tv_sec = result->i_mtime.tv_sec = result->i_atime.tv_sec = local_to_gmt(dir->i_sb, le32_to_cpu(dee.creation_date));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 result->i_ctime.tv_nsec = 0;
255 result->i_mtime.tv_nsec = 0;
256 result->i_atime.tv_nsec = 0;
257 hpfs_i(result)->i_ea_size = 0;
David Howellsde395b82008-11-14 10:38:55 +1100258 result->i_uid = current_fsuid();
259 result->i_gid = current_fsgid();
Miklos Szeredibfe86842011-10-28 14:13:29 +0200260 set_nlink(result, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 result->i_size = 0;
262 result->i_blocks = 1;
263 init_special_inode(result, mode, rdev);
264
Mikulas Patocka7d23ce32011-05-08 20:43:06 +0200265 r = hpfs_add_dirent(dir, name, len, &dee);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (r == 1)
267 goto bail2;
268 if (r == -1) {
269 err = -EEXIST;
270 goto bail2;
271 }
272 fnode->len = len;
273 memcpy(fnode->name, name, len > 15 ? 15 : len);
Mikulas Patocka0b697602011-05-08 20:44:26 +0200274 fnode->up = cpu_to_le32(dir->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 mark_buffer_dirty(bh);
276
277 insert_inode_hash(result);
278
279 hpfs_write_inode_nolock(result);
Mikulas Patockaf49a26e2015-09-02 22:51:53 +0200280 hpfs_update_directory_times(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 d_instantiate(dentry, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 brelse(bh);
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100283 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return 0;
285bail2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 iput(result);
287bail1:
288 brelse(bh);
289 hpfs_free_sectors(dir->i_sb, fno, 1);
290bail:
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100291 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 return err;
293}
294
Christian Brauner549c7292021-01-21 14:19:43 +0100295static int hpfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
296 struct dentry *dentry, const char *symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Al Viro7e7742e2010-01-31 17:09:29 -0500298 const unsigned char *name = dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 unsigned len = dentry->d_name.len;
300 struct buffer_head *bh;
301 struct fnode *fnode;
302 fnode_secno fno;
303 int r;
304 struct hpfs_dirent dee;
305 struct inode *result;
306 int err;
Al Viro7e7742e2010-01-31 17:09:29 -0500307 if ((err = hpfs_chk_name(name, &len))) return err==-ENOENT ? -EINVAL : err;
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100308 hpfs_lock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (hpfs_sb(dir->i_sb)->sb_eas < 2) {
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100310 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 return -EPERM;
312 }
313 err = -ENOSPC;
314 fnode = hpfs_alloc_fnode(dir->i_sb, hpfs_i(dir)->i_dno, &fno, &bh);
315 if (!fnode)
316 goto bail;
317 memset(&dee, 0, sizeof dee);
318 dee.archive = 1;
319 dee.hidden = name[0] == '.';
Mikulas Patocka0b697602011-05-08 20:44:26 +0200320 dee.fnode = cpu_to_le32(fno);
Arnd Bergmannf08957d02018-08-17 15:43:54 -0700321 dee.creation_date = dee.write_date = dee.read_date = cpu_to_le32(local_get_seconds(dir->i_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 result = new_inode(dir->i_sb);
324 if (!result)
325 goto bail1;
326 result->i_ino = fno;
327 hpfs_init_inode(result);
328 hpfs_i(result)->i_parent_dir = dir->i_ino;
Mikulas Patocka0b697602011-05-08 20:44:26 +0200329 result->i_ctime.tv_sec = result->i_mtime.tv_sec = result->i_atime.tv_sec = local_to_gmt(dir->i_sb, le32_to_cpu(dee.creation_date));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 result->i_ctime.tv_nsec = 0;
331 result->i_mtime.tv_nsec = 0;
332 result->i_atime.tv_nsec = 0;
333 hpfs_i(result)->i_ea_size = 0;
334 result->i_mode = S_IFLNK | 0777;
David Howellsde395b82008-11-14 10:38:55 +1100335 result->i_uid = current_fsuid();
336 result->i_gid = current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 result->i_blocks = 1;
Miklos Szeredibfe86842011-10-28 14:13:29 +0200338 set_nlink(result, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 result->i_size = strlen(symlink);
Al Viro21fc61c2015-11-17 01:07:57 -0500340 inode_nohighmem(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 result->i_op = &page_symlink_inode_operations;
342 result->i_data.a_ops = &hpfs_symlink_aops;
343
Mikulas Patocka7d23ce32011-05-08 20:43:06 +0200344 r = hpfs_add_dirent(dir, name, len, &dee);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if (r == 1)
346 goto bail2;
347 if (r == -1) {
348 err = -EEXIST;
349 goto bail2;
350 }
351 fnode->len = len;
352 memcpy(fnode->name, name, len > 15 ? 15 : len);
Mikulas Patocka0b697602011-05-08 20:44:26 +0200353 fnode->up = cpu_to_le32(dir->i_ino);
Al Viro7e7742e2010-01-31 17:09:29 -0500354 hpfs_set_ea(result, fnode, "SYMLINK", symlink, strlen(symlink));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 mark_buffer_dirty(bh);
356 brelse(bh);
357
358 insert_inode_hash(result);
359
360 hpfs_write_inode_nolock(result);
Mikulas Patockaf49a26e2015-09-02 22:51:53 +0200361 hpfs_update_directory_times(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 d_instantiate(dentry, result);
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100363 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return 0;
365bail2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 iput(result);
367bail1:
368 brelse(bh);
369 hpfs_free_sectors(dir->i_sb, fno, 1);
370bail:
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100371 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return err;
373}
374
375static int hpfs_unlink(struct inode *dir, struct dentry *dentry)
376{
Al Viro7e7742e2010-01-31 17:09:29 -0500377 const unsigned char *name = dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 unsigned len = dentry->d_name.len;
379 struct quad_buffer_head qbh;
380 struct hpfs_dirent *de;
David Howells2b0143b2015-03-17 22:25:59 +0000381 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 dnode_secno dno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 int err;
385
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100386 hpfs_lock(dir->i_sb);
Al Viro7e7742e2010-01-31 17:09:29 -0500387 hpfs_adjust_length(name, &len);
Mikulas Patockab6853f72016-02-25 18:17:38 +0100388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 err = -ENOENT;
Al Viro7e7742e2010-01-31 17:09:29 -0500390 de = map_dirent(dir, hpfs_i(dir)->i_dno, name, len, &dno, &qbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (!de)
392 goto out;
393
394 err = -EPERM;
395 if (de->first)
396 goto out1;
397
398 err = -EISDIR;
399 if (de->directory)
400 goto out1;
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 r = hpfs_remove_dirent(dir, dno, de, &qbh, 1);
403 switch (r) {
404 case 1:
405 hpfs_error(dir->i_sb, "there was error when removing dirent");
406 err = -EFSERROR;
407 break;
Mikulas Patockab6853f72016-02-25 18:17:38 +0100408 case 2: /* no space for deleting */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 err = -ENOSPC;
Mikulas Patockab6853f72016-02-25 18:17:38 +0100410 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 default:
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700412 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 err = 0;
414 }
415 goto out;
416
417out1:
418 hpfs_brelse4(&qbh);
419out:
Mikulas Patockaf49a26e2015-09-02 22:51:53 +0200420 if (!err)
421 hpfs_update_directory_times(dir);
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100422 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return err;
424}
425
426static int hpfs_rmdir(struct inode *dir, struct dentry *dentry)
427{
Al Viro7e7742e2010-01-31 17:09:29 -0500428 const unsigned char *name = dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 unsigned len = dentry->d_name.len;
430 struct quad_buffer_head qbh;
431 struct hpfs_dirent *de;
David Howells2b0143b2015-03-17 22:25:59 +0000432 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 dnode_secno dno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 int n_items = 0;
435 int err;
436 int r;
437
Al Viro7e7742e2010-01-31 17:09:29 -0500438 hpfs_adjust_length(name, &len);
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100439 hpfs_lock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 err = -ENOENT;
Al Viro7e7742e2010-01-31 17:09:29 -0500441 de = map_dirent(dir, hpfs_i(dir)->i_dno, name, len, &dno, &qbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 if (!de)
443 goto out;
444
445 err = -EPERM;
446 if (de->first)
447 goto out1;
448
449 err = -ENOTDIR;
450 if (!de->directory)
451 goto out1;
452
453 hpfs_count_dnodes(dir->i_sb, hpfs_i(inode)->i_dno, NULL, NULL, &n_items);
454 err = -ENOTEMPTY;
455 if (n_items)
456 goto out1;
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 r = hpfs_remove_dirent(dir, dno, de, &qbh, 1);
459 switch (r) {
460 case 1:
461 hpfs_error(dir->i_sb, "there was error when removing dirent");
462 err = -EFSERROR;
463 break;
464 case 2:
465 err = -ENOSPC;
466 break;
467 default:
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700468 drop_nlink(dir);
Dave Hansence71ec32006-09-30 23:29:06 -0700469 clear_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 err = 0;
471 }
472 goto out;
473out1:
474 hpfs_brelse4(&qbh);
475out:
Mikulas Patockaf49a26e2015-09-02 22:51:53 +0200476 if (!err)
477 hpfs_update_directory_times(dir);
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100478 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 return err;
480}
481
482static int hpfs_symlink_readpage(struct file *file, struct page *page)
483{
Al Viro21fc61c2015-11-17 01:07:57 -0500484 char *link = page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 struct inode *i = page->mapping->host;
486 struct fnode *fnode;
487 struct buffer_head *bh;
488 int err;
489
490 err = -EIO;
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100491 hpfs_lock(i->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (!(fnode = hpfs_map_fnode(i->i_sb, i->i_ino, &bh)))
493 goto fail;
494 err = hpfs_read_ea(i->i_sb, fnode, "SYMLINK", link, PAGE_SIZE);
495 brelse(bh);
496 if (err)
497 goto fail;
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100498 hpfs_unlock(i->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 unlock_page(page);
501 return 0;
502
503fail:
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100504 hpfs_unlock(i->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 SetPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 unlock_page(page);
507 return err;
508}
509
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700510const struct address_space_operations hpfs_symlink_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 .readpage = hpfs_symlink_readpage
512};
Christian Brauner549c7292021-01-21 14:19:43 +0100513
514static int hpfs_rename(struct user_namespace *mnt_userns, struct inode *old_dir,
515 struct dentry *old_dentry, struct inode *new_dir,
516 struct dentry *new_dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Al Viro7e7742e2010-01-31 17:09:29 -0500518 const unsigned char *old_name = old_dentry->d_name.name;
519 unsigned old_len = old_dentry->d_name.len;
520 const unsigned char *new_name = new_dentry->d_name.name;
521 unsigned new_len = new_dentry->d_name.len;
David Howells2b0143b2015-03-17 22:25:59 +0000522 struct inode *i = d_inode(old_dentry);
523 struct inode *new_inode = d_inode(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 struct quad_buffer_head qbh, qbh1;
525 struct hpfs_dirent *dep, *nde;
526 struct hpfs_dirent de;
527 dnode_secno dno;
528 int r;
529 struct buffer_head *bh;
530 struct fnode *fnode;
531 int err;
Sage Weile4eaac02011-05-24 13:06:07 -0700532
Miklos Szeredif03b8ad2016-09-27 11:03:57 +0200533 if (flags & ~RENAME_NOREPLACE)
534 return -EINVAL;
535
Al Viro7e7742e2010-01-31 17:09:29 -0500536 if ((err = hpfs_chk_name(new_name, &new_len))) return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 err = 0;
Al Viro7e7742e2010-01-31 17:09:29 -0500538 hpfs_adjust_length(old_name, &old_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100540 hpfs_lock(i->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 /* order doesn't matter, due to VFS exclusion */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 /* Erm? Moving over the empty non-busy directory is perfectly legal */
544 if (new_inode && S_ISDIR(new_inode->i_mode)) {
545 err = -EINVAL;
546 goto end1;
547 }
548
Al Viro7e7742e2010-01-31 17:09:29 -0500549 if (!(dep = map_dirent(old_dir, hpfs_i(old_dir)->i_dno, old_name, old_len, &dno, &qbh))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 hpfs_error(i->i_sb, "lookup succeeded but map dirent failed");
551 err = -ENOENT;
552 goto end1;
553 }
554 copy_de(&de, dep);
555 de.hidden = new_name[0] == '.';
556
557 if (new_inode) {
558 int r;
559 if ((r = hpfs_remove_dirent(old_dir, dno, dep, &qbh, 1)) != 2) {
Al Viro7e7742e2010-01-31 17:09:29 -0500560 if ((nde = map_dirent(new_dir, hpfs_i(new_dir)->i_dno, new_name, new_len, NULL, &qbh1))) {
Dave Hansence71ec32006-09-30 23:29:06 -0700561 clear_nlink(new_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 copy_de(nde, &de);
563 memcpy(nde->name, new_name, new_len);
564 hpfs_mark_4buffers_dirty(&qbh1);
565 hpfs_brelse4(&qbh1);
566 goto end;
567 }
568 hpfs_error(new_dir->i_sb, "hpfs_rename: could not find dirent");
569 err = -EFSERROR;
570 goto end1;
571 }
Colin Ian Kinge0fcfe12018-08-25 12:24:31 +0200572 err = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 goto end1;
574 }
575
576 if (new_dir == old_dir) hpfs_brelse4(&qbh);
577
Mikulas Patocka7d23ce32011-05-08 20:43:06 +0200578 if ((r = hpfs_add_dirent(new_dir, new_name, new_len, &de))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (r == -1) hpfs_error(new_dir->i_sb, "hpfs_rename: dirent already exists!");
580 err = r == 1 ? -ENOSPC : -EFSERROR;
581 if (new_dir != old_dir) hpfs_brelse4(&qbh);
582 goto end1;
583 }
584
585 if (new_dir == old_dir)
Al Viro7e7742e2010-01-31 17:09:29 -0500586 if (!(dep = map_dirent(old_dir, hpfs_i(old_dir)->i_dno, old_name, old_len, &dno, &qbh))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 hpfs_error(i->i_sb, "lookup succeeded but map dirent failed at #2");
588 err = -ENOENT;
589 goto end1;
590 }
591
592 if ((r = hpfs_remove_dirent(old_dir, dno, dep, &qbh, 0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 hpfs_error(i->i_sb, "hpfs_rename: could not remove dirent");
594 err = r == 2 ? -ENOSPC : -EFSERROR;
595 goto end1;
596 }
Mikulas Patocka7d23ce32011-05-08 20:43:06 +0200597
Mikulas Patockaf49a26e2015-09-02 22:51:53 +0200598end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 hpfs_i(i)->i_parent_dir = new_dir->i_ino;
600 if (S_ISDIR(i->i_mode)) {
Dave Hansend8c76e62006-09-30 23:29:04 -0700601 inc_nlink(new_dir);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700602 drop_nlink(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
604 if ((fnode = hpfs_map_fnode(i->i_sb, i->i_ino, &bh))) {
Mikulas Patocka0b697602011-05-08 20:44:26 +0200605 fnode->up = cpu_to_le32(new_dir->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 fnode->len = new_len;
607 memcpy(fnode->name, new_name, new_len>15?15:new_len);
608 if (new_len < 15) memset(&fnode->name[new_len], 0, 15 - new_len);
609 mark_buffer_dirty(bh);
610 brelse(bh);
611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612end1:
Mikulas Patockaf49a26e2015-09-02 22:51:53 +0200613 if (!err) {
614 hpfs_update_directory_times(old_dir);
615 hpfs_update_directory_times(new_dir);
616 }
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100617 hpfs_unlock(i->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 return err;
619}
620
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800621const struct inode_operations hpfs_dir_iops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
623 .create = hpfs_create,
624 .lookup = hpfs_lookup,
625 .unlink = hpfs_unlink,
626 .symlink = hpfs_symlink,
627 .mkdir = hpfs_mkdir,
628 .rmdir = hpfs_rmdir,
629 .mknod = hpfs_mknod,
Miklos Szeredi2773bf02016-09-27 11:03:58 +0200630 .rename = hpfs_rename,
Christoph Hellwigca30bc92008-08-11 00:27:59 +0200631 .setattr = hpfs_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632};