blob: 082b7c76dd0c91cc245f842b77becd520e1fb84c [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
Al Viro18bb1db2011-07-26 01:41:39 -040023static int hpfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024{
Al Viro7e7742e2010-01-31 17:09:29 -050025 const unsigned char *name = dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 unsigned len = dentry->d_name.len;
27 struct quad_buffer_head qbh0;
28 struct buffer_head *bh;
29 struct hpfs_dirent *de;
30 struct fnode *fnode;
31 struct dnode *dnode;
32 struct inode *result;
33 fnode_secno fno;
34 dnode_secno dno;
35 int r;
36 struct hpfs_dirent dee;
37 int err;
Al Viro7e7742e2010-01-31 17:09:29 -050038 if ((err = hpfs_chk_name(name, &len))) return err==-ENOENT ? -EINVAL : err;
Arnd Bergmann9a311b92011-01-22 20:26:12 +010039 hpfs_lock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 err = -ENOSPC;
41 fnode = hpfs_alloc_fnode(dir->i_sb, hpfs_i(dir)->i_dno, &fno, &bh);
42 if (!fnode)
43 goto bail;
Mikulas Patocka7d23ce32011-05-08 20:43:06 +020044 dnode = hpfs_alloc_dnode(dir->i_sb, fno, &dno, &qbh0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 if (!dnode)
46 goto bail1;
47 memset(&dee, 0, sizeof dee);
48 dee.directory = 1;
49 if (!(mode & 0222)) dee.read_only = 1;
50 /*dee.archive = 0;*/
51 dee.hidden = name[0] == '.';
Mikulas Patocka0b697602011-05-08 20:44:26 +020052 dee.fnode = cpu_to_le32(fno);
Arnd Bergmannf08957d02018-08-17 15:43:54 -070053 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 -070054 result = new_inode(dir->i_sb);
55 if (!result)
56 goto bail2;
57 hpfs_init_inode(result);
58 result->i_ino = fno;
59 hpfs_i(result)->i_parent_dir = dir->i_ino;
60 hpfs_i(result)->i_dno = dno;
Mikulas Patocka0b697602011-05-08 20:44:26 +020061 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 -070062 result->i_ctime.tv_nsec = 0;
63 result->i_mtime.tv_nsec = 0;
64 result->i_atime.tv_nsec = 0;
65 hpfs_i(result)->i_ea_size = 0;
66 result->i_mode |= S_IFDIR;
67 result->i_op = &hpfs_dir_iops;
68 result->i_fop = &hpfs_dir_ops;
69 result->i_blocks = 4;
70 result->i_size = 2048;
Miklos Szeredibfe86842011-10-28 14:13:29 +020071 set_nlink(result, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 if (dee.read_only)
73 result->i_mode &= ~0222;
74
Mikulas Patocka7d23ce32011-05-08 20:43:06 +020075 r = hpfs_add_dirent(dir, name, len, &dee);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 if (r == 1)
77 goto bail3;
78 if (r == -1) {
79 err = -EEXIST;
80 goto bail3;
81 }
82 fnode->len = len;
83 memcpy(fnode->name, name, len > 15 ? 15 : len);
Mikulas Patocka0b697602011-05-08 20:44:26 +020084 fnode->up = cpu_to_le32(dir->i_ino);
Al Viroc4c99542012-04-06 14:30:07 -040085 fnode->flags |= FNODE_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 fnode->btree.n_free_nodes = 7;
87 fnode->btree.n_used_nodes = 1;
Mikulas Patocka0b697602011-05-08 20:44:26 +020088 fnode->btree.first_free = cpu_to_le16(0x14);
89 fnode->u.external[0].disk_secno = cpu_to_le32(dno);
90 fnode->u.external[0].file_secno = cpu_to_le32(-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 dnode->root_dnode = 1;
Mikulas Patocka0b697602011-05-08 20:44:26 +020092 dnode->up = cpu_to_le32(fno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 de = hpfs_add_de(dir->i_sb, dnode, "\001\001", 2, 0);
Arnd Bergmannf08957d02018-08-17 15:43:54 -070094 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 -070095 if (!(mode & 0222)) de->read_only = 1;
96 de->first = de->directory = 1;
97 /*de->hidden = de->system = 0;*/
Mikulas Patocka0b697602011-05-08 20:44:26 +020098 de->fnode = cpu_to_le32(fno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 mark_buffer_dirty(bh);
100 brelse(bh);
101 hpfs_mark_4buffers_dirty(&qbh0);
102 hpfs_brelse4(&qbh0);
Dave Hansend8c76e62006-09-30 23:29:04 -0700103 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 insert_inode_hash(result);
105
Eric W. Biederman0e1a43c2012-02-07 16:27:53 -0800106 if (!uid_eq(result->i_uid, current_fsuid()) ||
107 !gid_eq(result->i_gid, current_fsgid()) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 result->i_mode != (mode | S_IFDIR)) {
David Howellsde395b82008-11-14 10:38:55 +1100109 result->i_uid = current_fsuid();
110 result->i_gid = current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 result->i_mode = mode | S_IFDIR;
112 hpfs_write_inode_nolock(result);
113 }
Mikulas Patockaf49a26e2015-09-02 22:51:53 +0200114 hpfs_update_directory_times(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 d_instantiate(dentry, result);
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100116 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return 0;
118bail3:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 iput(result);
120bail2:
121 hpfs_brelse4(&qbh0);
122 hpfs_free_dnode(dir->i_sb, dno);
123bail1:
124 brelse(bh);
125 hpfs_free_sectors(dir->i_sb, fno, 1);
126bail:
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100127 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 return err;
129}
130
Al Viroebfc3b42012-06-10 18:05:36 -0400131static int hpfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Al Viro7e7742e2010-01-31 17:09:29 -0500133 const unsigned char *name = dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 unsigned len = dentry->d_name.len;
135 struct inode *result = NULL;
136 struct buffer_head *bh;
137 struct fnode *fnode;
138 fnode_secno fno;
139 int r;
140 struct hpfs_dirent dee;
141 int err;
Al Viro7e7742e2010-01-31 17:09:29 -0500142 if ((err = hpfs_chk_name(name, &len)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 return err==-ENOENT ? -EINVAL : err;
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100144 hpfs_lock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 err = -ENOSPC;
146 fnode = hpfs_alloc_fnode(dir->i_sb, hpfs_i(dir)->i_dno, &fno, &bh);
147 if (!fnode)
148 goto bail;
149 memset(&dee, 0, sizeof dee);
150 if (!(mode & 0222)) dee.read_only = 1;
151 dee.archive = 1;
152 dee.hidden = name[0] == '.';
Mikulas Patocka0b697602011-05-08 20:44:26 +0200153 dee.fnode = cpu_to_le32(fno);
Arnd Bergmannf08957d02018-08-17 15:43:54 -0700154 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 -0700155
156 result = new_inode(dir->i_sb);
157 if (!result)
158 goto bail1;
159
160 hpfs_init_inode(result);
161 result->i_ino = fno;
162 result->i_mode |= S_IFREG;
163 result->i_mode &= ~0111;
164 result->i_op = &hpfs_file_iops;
165 result->i_fop = &hpfs_file_ops;
Miklos Szeredibfe86842011-10-28 14:13:29 +0200166 set_nlink(result, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 hpfs_i(result)->i_parent_dir = dir->i_ino;
Mikulas Patocka0b697602011-05-08 20:44:26 +0200168 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 -0700169 result->i_ctime.tv_nsec = 0;
170 result->i_mtime.tv_nsec = 0;
171 result->i_atime.tv_nsec = 0;
172 hpfs_i(result)->i_ea_size = 0;
173 if (dee.read_only)
174 result->i_mode &= ~0222;
175 result->i_blocks = 1;
176 result->i_size = 0;
177 result->i_data.a_ops = &hpfs_aops;
178 hpfs_i(result)->mmu_private = 0;
179
Mikulas Patocka7d23ce32011-05-08 20:43:06 +0200180 r = hpfs_add_dirent(dir, name, len, &dee);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 if (r == 1)
182 goto bail2;
183 if (r == -1) {
184 err = -EEXIST;
185 goto bail2;
186 }
187 fnode->len = len;
188 memcpy(fnode->name, name, len > 15 ? 15 : len);
Mikulas Patocka0b697602011-05-08 20:44:26 +0200189 fnode->up = cpu_to_le32(dir->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 mark_buffer_dirty(bh);
191 brelse(bh);
192
193 insert_inode_hash(result);
194
Eric W. Biederman0e1a43c2012-02-07 16:27:53 -0800195 if (!uid_eq(result->i_uid, current_fsuid()) ||
196 !gid_eq(result->i_gid, current_fsgid()) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 result->i_mode != (mode | S_IFREG)) {
David Howellsde395b82008-11-14 10:38:55 +1100198 result->i_uid = current_fsuid();
199 result->i_gid = current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 result->i_mode = mode | S_IFREG;
201 hpfs_write_inode_nolock(result);
202 }
Mikulas Patockaf49a26e2015-09-02 22:51:53 +0200203 hpfs_update_directory_times(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 d_instantiate(dentry, result);
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100205 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return 0;
207
208bail2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 iput(result);
210bail1:
211 brelse(bh);
212 hpfs_free_sectors(dir->i_sb, fno, 1);
213bail:
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100214 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return err;
216}
217
Al Viro1a67aaf2011-07-26 01:52:52 -0400218static int hpfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
Al Viro7e7742e2010-01-31 17:09:29 -0500220 const unsigned char *name = dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 unsigned len = dentry->d_name.len;
222 struct buffer_head *bh;
223 struct fnode *fnode;
224 fnode_secno fno;
225 int r;
226 struct hpfs_dirent dee;
227 struct inode *result = NULL;
228 int err;
Al Viro7e7742e2010-01-31 17:09:29 -0500229 if ((err = hpfs_chk_name(name, &len))) return err==-ENOENT ? -EINVAL : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 if (hpfs_sb(dir->i_sb)->sb_eas < 2) return -EPERM;
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100231 hpfs_lock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 err = -ENOSPC;
233 fnode = hpfs_alloc_fnode(dir->i_sb, hpfs_i(dir)->i_dno, &fno, &bh);
234 if (!fnode)
235 goto bail;
236 memset(&dee, 0, sizeof dee);
237 if (!(mode & 0222)) dee.read_only = 1;
238 dee.archive = 1;
239 dee.hidden = name[0] == '.';
Mikulas Patocka0b697602011-05-08 20:44:26 +0200240 dee.fnode = cpu_to_le32(fno);
Arnd Bergmannf08957d02018-08-17 15:43:54 -0700241 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 -0700242
243 result = new_inode(dir->i_sb);
244 if (!result)
245 goto bail1;
246
247 hpfs_init_inode(result);
248 result->i_ino = fno;
249 hpfs_i(result)->i_parent_dir = dir->i_ino;
Mikulas Patocka0b697602011-05-08 20:44:26 +0200250 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 -0700251 result->i_ctime.tv_nsec = 0;
252 result->i_mtime.tv_nsec = 0;
253 result->i_atime.tv_nsec = 0;
254 hpfs_i(result)->i_ea_size = 0;
David Howellsde395b82008-11-14 10:38:55 +1100255 result->i_uid = current_fsuid();
256 result->i_gid = current_fsgid();
Miklos Szeredibfe86842011-10-28 14:13:29 +0200257 set_nlink(result, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 result->i_size = 0;
259 result->i_blocks = 1;
260 init_special_inode(result, mode, rdev);
261
Mikulas Patocka7d23ce32011-05-08 20:43:06 +0200262 r = hpfs_add_dirent(dir, name, len, &dee);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (r == 1)
264 goto bail2;
265 if (r == -1) {
266 err = -EEXIST;
267 goto bail2;
268 }
269 fnode->len = len;
270 memcpy(fnode->name, name, len > 15 ? 15 : len);
Mikulas Patocka0b697602011-05-08 20:44:26 +0200271 fnode->up = cpu_to_le32(dir->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 mark_buffer_dirty(bh);
273
274 insert_inode_hash(result);
275
276 hpfs_write_inode_nolock(result);
Mikulas Patockaf49a26e2015-09-02 22:51:53 +0200277 hpfs_update_directory_times(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 d_instantiate(dentry, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 brelse(bh);
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100280 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return 0;
282bail2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 iput(result);
284bail1:
285 brelse(bh);
286 hpfs_free_sectors(dir->i_sb, fno, 1);
287bail:
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100288 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return err;
290}
291
292static int hpfs_symlink(struct inode *dir, struct dentry *dentry, const char *symlink)
293{
Al Viro7e7742e2010-01-31 17:09:29 -0500294 const unsigned char *name = dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 unsigned len = dentry->d_name.len;
296 struct buffer_head *bh;
297 struct fnode *fnode;
298 fnode_secno fno;
299 int r;
300 struct hpfs_dirent dee;
301 struct inode *result;
302 int err;
Al Viro7e7742e2010-01-31 17:09:29 -0500303 if ((err = hpfs_chk_name(name, &len))) return err==-ENOENT ? -EINVAL : err;
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100304 hpfs_lock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 if (hpfs_sb(dir->i_sb)->sb_eas < 2) {
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100306 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 return -EPERM;
308 }
309 err = -ENOSPC;
310 fnode = hpfs_alloc_fnode(dir->i_sb, hpfs_i(dir)->i_dno, &fno, &bh);
311 if (!fnode)
312 goto bail;
313 memset(&dee, 0, sizeof dee);
314 dee.archive = 1;
315 dee.hidden = name[0] == '.';
Mikulas Patocka0b697602011-05-08 20:44:26 +0200316 dee.fnode = cpu_to_le32(fno);
Arnd Bergmannf08957d02018-08-17 15:43:54 -0700317 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 -0700318
319 result = new_inode(dir->i_sb);
320 if (!result)
321 goto bail1;
322 result->i_ino = fno;
323 hpfs_init_inode(result);
324 hpfs_i(result)->i_parent_dir = dir->i_ino;
Mikulas Patocka0b697602011-05-08 20:44:26 +0200325 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 -0700326 result->i_ctime.tv_nsec = 0;
327 result->i_mtime.tv_nsec = 0;
328 result->i_atime.tv_nsec = 0;
329 hpfs_i(result)->i_ea_size = 0;
330 result->i_mode = S_IFLNK | 0777;
David Howellsde395b82008-11-14 10:38:55 +1100331 result->i_uid = current_fsuid();
332 result->i_gid = current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 result->i_blocks = 1;
Miklos Szeredibfe86842011-10-28 14:13:29 +0200334 set_nlink(result, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 result->i_size = strlen(symlink);
Al Viro21fc61c2015-11-17 01:07:57 -0500336 inode_nohighmem(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 result->i_op = &page_symlink_inode_operations;
338 result->i_data.a_ops = &hpfs_symlink_aops;
339
Mikulas Patocka7d23ce32011-05-08 20:43:06 +0200340 r = hpfs_add_dirent(dir, name, len, &dee);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (r == 1)
342 goto bail2;
343 if (r == -1) {
344 err = -EEXIST;
345 goto bail2;
346 }
347 fnode->len = len;
348 memcpy(fnode->name, name, len > 15 ? 15 : len);
Mikulas Patocka0b697602011-05-08 20:44:26 +0200349 fnode->up = cpu_to_le32(dir->i_ino);
Al Viro7e7742e2010-01-31 17:09:29 -0500350 hpfs_set_ea(result, fnode, "SYMLINK", symlink, strlen(symlink));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 mark_buffer_dirty(bh);
352 brelse(bh);
353
354 insert_inode_hash(result);
355
356 hpfs_write_inode_nolock(result);
Mikulas Patockaf49a26e2015-09-02 22:51:53 +0200357 hpfs_update_directory_times(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 d_instantiate(dentry, result);
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100359 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return 0;
361bail2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 iput(result);
363bail1:
364 brelse(bh);
365 hpfs_free_sectors(dir->i_sb, fno, 1);
366bail:
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100367 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return err;
369}
370
371static int hpfs_unlink(struct inode *dir, struct dentry *dentry)
372{
Al Viro7e7742e2010-01-31 17:09:29 -0500373 const unsigned char *name = dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 unsigned len = dentry->d_name.len;
375 struct quad_buffer_head qbh;
376 struct hpfs_dirent *de;
David Howells2b0143b2015-03-17 22:25:59 +0000377 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 dnode_secno dno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 int err;
381
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100382 hpfs_lock(dir->i_sb);
Al Viro7e7742e2010-01-31 17:09:29 -0500383 hpfs_adjust_length(name, &len);
Mikulas Patockab6853f72016-02-25 18:17:38 +0100384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 err = -ENOENT;
Al Viro7e7742e2010-01-31 17:09:29 -0500386 de = map_dirent(dir, hpfs_i(dir)->i_dno, name, len, &dno, &qbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (!de)
388 goto out;
389
390 err = -EPERM;
391 if (de->first)
392 goto out1;
393
394 err = -EISDIR;
395 if (de->directory)
396 goto out1;
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 r = hpfs_remove_dirent(dir, dno, de, &qbh, 1);
399 switch (r) {
400 case 1:
401 hpfs_error(dir->i_sb, "there was error when removing dirent");
402 err = -EFSERROR;
403 break;
Mikulas Patockab6853f72016-02-25 18:17:38 +0100404 case 2: /* no space for deleting */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 err = -ENOSPC;
Mikulas Patockab6853f72016-02-25 18:17:38 +0100406 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 default:
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700408 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 err = 0;
410 }
411 goto out;
412
413out1:
414 hpfs_brelse4(&qbh);
415out:
Mikulas Patockaf49a26e2015-09-02 22:51:53 +0200416 if (!err)
417 hpfs_update_directory_times(dir);
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100418 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return err;
420}
421
422static int hpfs_rmdir(struct inode *dir, struct dentry *dentry)
423{
Al Viro7e7742e2010-01-31 17:09:29 -0500424 const unsigned char *name = dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 unsigned len = dentry->d_name.len;
426 struct quad_buffer_head qbh;
427 struct hpfs_dirent *de;
David Howells2b0143b2015-03-17 22:25:59 +0000428 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 dnode_secno dno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 int n_items = 0;
431 int err;
432 int r;
433
Al Viro7e7742e2010-01-31 17:09:29 -0500434 hpfs_adjust_length(name, &len);
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100435 hpfs_lock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 err = -ENOENT;
Al Viro7e7742e2010-01-31 17:09:29 -0500437 de = map_dirent(dir, hpfs_i(dir)->i_dno, name, len, &dno, &qbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (!de)
439 goto out;
440
441 err = -EPERM;
442 if (de->first)
443 goto out1;
444
445 err = -ENOTDIR;
446 if (!de->directory)
447 goto out1;
448
449 hpfs_count_dnodes(dir->i_sb, hpfs_i(inode)->i_dno, NULL, NULL, &n_items);
450 err = -ENOTEMPTY;
451 if (n_items)
452 goto out1;
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 r = hpfs_remove_dirent(dir, dno, de, &qbh, 1);
455 switch (r) {
456 case 1:
457 hpfs_error(dir->i_sb, "there was error when removing dirent");
458 err = -EFSERROR;
459 break;
460 case 2:
461 err = -ENOSPC;
462 break;
463 default:
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700464 drop_nlink(dir);
Dave Hansence71ec32006-09-30 23:29:06 -0700465 clear_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 err = 0;
467 }
468 goto out;
469out1:
470 hpfs_brelse4(&qbh);
471out:
Mikulas Patockaf49a26e2015-09-02 22:51:53 +0200472 if (!err)
473 hpfs_update_directory_times(dir);
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100474 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return err;
476}
477
478static int hpfs_symlink_readpage(struct file *file, struct page *page)
479{
Al Viro21fc61c2015-11-17 01:07:57 -0500480 char *link = page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 struct inode *i = page->mapping->host;
482 struct fnode *fnode;
483 struct buffer_head *bh;
484 int err;
485
486 err = -EIO;
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100487 hpfs_lock(i->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (!(fnode = hpfs_map_fnode(i->i_sb, i->i_ino, &bh)))
489 goto fail;
490 err = hpfs_read_ea(i->i_sb, fnode, "SYMLINK", link, PAGE_SIZE);
491 brelse(bh);
492 if (err)
493 goto fail;
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100494 hpfs_unlock(i->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 unlock_page(page);
497 return 0;
498
499fail:
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100500 hpfs_unlock(i->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 SetPageError(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 unlock_page(page);
503 return err;
504}
505
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700506const struct address_space_operations hpfs_symlink_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 .readpage = hpfs_symlink_readpage
508};
509
510static int hpfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Miklos Szeredif03b8ad2016-09-27 11:03:57 +0200511 struct inode *new_dir, struct dentry *new_dentry,
512 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Al Viro7e7742e2010-01-31 17:09:29 -0500514 const unsigned char *old_name = old_dentry->d_name.name;
515 unsigned old_len = old_dentry->d_name.len;
516 const unsigned char *new_name = new_dentry->d_name.name;
517 unsigned new_len = new_dentry->d_name.len;
David Howells2b0143b2015-03-17 22:25:59 +0000518 struct inode *i = d_inode(old_dentry);
519 struct inode *new_inode = d_inode(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 struct quad_buffer_head qbh, qbh1;
521 struct hpfs_dirent *dep, *nde;
522 struct hpfs_dirent de;
523 dnode_secno dno;
524 int r;
525 struct buffer_head *bh;
526 struct fnode *fnode;
527 int err;
Sage Weile4eaac02011-05-24 13:06:07 -0700528
Miklos Szeredif03b8ad2016-09-27 11:03:57 +0200529 if (flags & ~RENAME_NOREPLACE)
530 return -EINVAL;
531
Al Viro7e7742e2010-01-31 17:09:29 -0500532 if ((err = hpfs_chk_name(new_name, &new_len))) return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 err = 0;
Al Viro7e7742e2010-01-31 17:09:29 -0500534 hpfs_adjust_length(old_name, &old_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100536 hpfs_lock(i->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 /* order doesn't matter, due to VFS exclusion */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 /* Erm? Moving over the empty non-busy directory is perfectly legal */
540 if (new_inode && S_ISDIR(new_inode->i_mode)) {
541 err = -EINVAL;
542 goto end1;
543 }
544
Al Viro7e7742e2010-01-31 17:09:29 -0500545 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 -0700546 hpfs_error(i->i_sb, "lookup succeeded but map dirent failed");
547 err = -ENOENT;
548 goto end1;
549 }
550 copy_de(&de, dep);
551 de.hidden = new_name[0] == '.';
552
553 if (new_inode) {
554 int r;
555 if ((r = hpfs_remove_dirent(old_dir, dno, dep, &qbh, 1)) != 2) {
Al Viro7e7742e2010-01-31 17:09:29 -0500556 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 -0700557 clear_nlink(new_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 copy_de(nde, &de);
559 memcpy(nde->name, new_name, new_len);
560 hpfs_mark_4buffers_dirty(&qbh1);
561 hpfs_brelse4(&qbh1);
562 goto end;
563 }
564 hpfs_error(new_dir->i_sb, "hpfs_rename: could not find dirent");
565 err = -EFSERROR;
566 goto end1;
567 }
568 err = r == 2 ? -ENOSPC : r == 1 ? -EFSERROR : 0;
569 goto end1;
570 }
571
572 if (new_dir == old_dir) hpfs_brelse4(&qbh);
573
Mikulas Patocka7d23ce32011-05-08 20:43:06 +0200574 if ((r = hpfs_add_dirent(new_dir, new_name, new_len, &de))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (r == -1) hpfs_error(new_dir->i_sb, "hpfs_rename: dirent already exists!");
576 err = r == 1 ? -ENOSPC : -EFSERROR;
577 if (new_dir != old_dir) hpfs_brelse4(&qbh);
578 goto end1;
579 }
580
581 if (new_dir == old_dir)
Al Viro7e7742e2010-01-31 17:09:29 -0500582 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 -0700583 hpfs_error(i->i_sb, "lookup succeeded but map dirent failed at #2");
584 err = -ENOENT;
585 goto end1;
586 }
587
588 if ((r = hpfs_remove_dirent(old_dir, dno, dep, &qbh, 0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 hpfs_error(i->i_sb, "hpfs_rename: could not remove dirent");
590 err = r == 2 ? -ENOSPC : -EFSERROR;
591 goto end1;
592 }
Mikulas Patocka7d23ce32011-05-08 20:43:06 +0200593
Mikulas Patockaf49a26e2015-09-02 22:51:53 +0200594end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 hpfs_i(i)->i_parent_dir = new_dir->i_ino;
596 if (S_ISDIR(i->i_mode)) {
Dave Hansend8c76e62006-09-30 23:29:04 -0700597 inc_nlink(new_dir);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700598 drop_nlink(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
600 if ((fnode = hpfs_map_fnode(i->i_sb, i->i_ino, &bh))) {
Mikulas Patocka0b697602011-05-08 20:44:26 +0200601 fnode->up = cpu_to_le32(new_dir->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 fnode->len = new_len;
603 memcpy(fnode->name, new_name, new_len>15?15:new_len);
604 if (new_len < 15) memset(&fnode->name[new_len], 0, 15 - new_len);
605 mark_buffer_dirty(bh);
606 brelse(bh);
607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608end1:
Mikulas Patockaf49a26e2015-09-02 22:51:53 +0200609 if (!err) {
610 hpfs_update_directory_times(old_dir);
611 hpfs_update_directory_times(new_dir);
612 }
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100613 hpfs_unlock(i->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 return err;
615}
616
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800617const struct inode_operations hpfs_dir_iops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
619 .create = hpfs_create,
620 .lookup = hpfs_lookup,
621 .unlink = hpfs_unlink,
622 .symlink = hpfs_symlink,
623 .mkdir = hpfs_mkdir,
624 .rmdir = hpfs_rmdir,
625 .mknod = hpfs_mknod,
Miklos Szeredi2773bf02016-09-27 11:03:58 +0200626 .rename = hpfs_rename,
Christoph Hellwigca30bc92008-08-11 00:27:59 +0200627 .setattr = hpfs_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628};