blob: ae4d5a1fa4c9b7f75ce188d8ec80e69d0a820892 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/hpfs/namei.c
3 *
4 * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
5 *
6 * adding & removing files & directories
7 */
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +04008#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include "hpfs_fn.h"
10
Mikulas Patockaf49a26e2015-09-02 22:51:53 +020011static void hpfs_update_directory_times(struct inode *dir)
12{
13 time_t t = get_seconds();
14 if (t == dir->i_mtime.tv_sec &&
15 t == dir->i_ctime.tv_sec)
16 return;
17 dir->i_mtime.tv_sec = dir->i_ctime.tv_sec = t;
18 dir->i_mtime.tv_nsec = dir->i_ctime.tv_nsec = 0;
19 hpfs_write_inode_nolock(dir);
20}
21
Al Viro18bb1db2011-07-26 01:41:39 -040022static int hpfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070023{
Al Viro7e7742e2010-01-31 17:09:29 -050024 const unsigned char *name = dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 unsigned len = dentry->d_name.len;
26 struct quad_buffer_head qbh0;
27 struct buffer_head *bh;
28 struct hpfs_dirent *de;
29 struct fnode *fnode;
30 struct dnode *dnode;
31 struct inode *result;
32 fnode_secno fno;
33 dnode_secno dno;
34 int r;
35 struct hpfs_dirent dee;
36 int err;
Al Viro7e7742e2010-01-31 17:09:29 -050037 if ((err = hpfs_chk_name(name, &len))) return err==-ENOENT ? -EINVAL : err;
Arnd Bergmann9a311b92011-01-22 20:26:12 +010038 hpfs_lock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 err = -ENOSPC;
40 fnode = hpfs_alloc_fnode(dir->i_sb, hpfs_i(dir)->i_dno, &fno, &bh);
41 if (!fnode)
42 goto bail;
Mikulas Patocka7d23ce32011-05-08 20:43:06 +020043 dnode = hpfs_alloc_dnode(dir->i_sb, fno, &dno, &qbh0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 if (!dnode)
45 goto bail1;
46 memset(&dee, 0, sizeof dee);
47 dee.directory = 1;
48 if (!(mode & 0222)) dee.read_only = 1;
49 /*dee.archive = 0;*/
50 dee.hidden = name[0] == '.';
Mikulas Patocka0b697602011-05-08 20:44:26 +020051 dee.fnode = cpu_to_le32(fno);
52 dee.creation_date = dee.write_date = dee.read_date = cpu_to_le32(gmt_to_local(dir->i_sb, get_seconds()));
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 result = new_inode(dir->i_sb);
54 if (!result)
55 goto bail2;
56 hpfs_init_inode(result);
57 result->i_ino = fno;
58 hpfs_i(result)->i_parent_dir = dir->i_ino;
59 hpfs_i(result)->i_dno = dno;
Mikulas Patocka0b697602011-05-08 20:44:26 +020060 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 -070061 result->i_ctime.tv_nsec = 0;
62 result->i_mtime.tv_nsec = 0;
63 result->i_atime.tv_nsec = 0;
64 hpfs_i(result)->i_ea_size = 0;
65 result->i_mode |= S_IFDIR;
66 result->i_op = &hpfs_dir_iops;
67 result->i_fop = &hpfs_dir_ops;
68 result->i_blocks = 4;
69 result->i_size = 2048;
Miklos Szeredibfe86842011-10-28 14:13:29 +020070 set_nlink(result, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 if (dee.read_only)
72 result->i_mode &= ~0222;
73
Mikulas Patocka7d23ce32011-05-08 20:43:06 +020074 r = hpfs_add_dirent(dir, name, len, &dee);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 if (r == 1)
76 goto bail3;
77 if (r == -1) {
78 err = -EEXIST;
79 goto bail3;
80 }
81 fnode->len = len;
82 memcpy(fnode->name, name, len > 15 ? 15 : len);
Mikulas Patocka0b697602011-05-08 20:44:26 +020083 fnode->up = cpu_to_le32(dir->i_ino);
Al Viroc4c99542012-04-06 14:30:07 -040084 fnode->flags |= FNODE_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 fnode->btree.n_free_nodes = 7;
86 fnode->btree.n_used_nodes = 1;
Mikulas Patocka0b697602011-05-08 20:44:26 +020087 fnode->btree.first_free = cpu_to_le16(0x14);
88 fnode->u.external[0].disk_secno = cpu_to_le32(dno);
89 fnode->u.external[0].file_secno = cpu_to_le32(-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 dnode->root_dnode = 1;
Mikulas Patocka0b697602011-05-08 20:44:26 +020091 dnode->up = cpu_to_le32(fno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 de = hpfs_add_de(dir->i_sb, dnode, "\001\001", 2, 0);
Mikulas Patocka0b697602011-05-08 20:44:26 +020093 de->creation_date = de->write_date = de->read_date = cpu_to_le32(gmt_to_local(dir->i_sb, get_seconds()));
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 if (!(mode & 0222)) de->read_only = 1;
95 de->first = de->directory = 1;
96 /*de->hidden = de->system = 0;*/
Mikulas Patocka0b697602011-05-08 20:44:26 +020097 de->fnode = cpu_to_le32(fno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 mark_buffer_dirty(bh);
99 brelse(bh);
100 hpfs_mark_4buffers_dirty(&qbh0);
101 hpfs_brelse4(&qbh0);
Dave Hansend8c76e62006-09-30 23:29:04 -0700102 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 insert_inode_hash(result);
104
Eric W. Biederman0e1a43c2012-02-07 16:27:53 -0800105 if (!uid_eq(result->i_uid, current_fsuid()) ||
106 !gid_eq(result->i_gid, current_fsgid()) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 result->i_mode != (mode | S_IFDIR)) {
David Howellsde395b82008-11-14 10:38:55 +1100108 result->i_uid = current_fsuid();
109 result->i_gid = current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 result->i_mode = mode | S_IFDIR;
111 hpfs_write_inode_nolock(result);
112 }
Mikulas Patockaf49a26e2015-09-02 22:51:53 +0200113 hpfs_update_directory_times(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 d_instantiate(dentry, result);
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100115 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 return 0;
117bail3:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 iput(result);
119bail2:
120 hpfs_brelse4(&qbh0);
121 hpfs_free_dnode(dir->i_sb, dno);
122bail1:
123 brelse(bh);
124 hpfs_free_sectors(dir->i_sb, fno, 1);
125bail:
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100126 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 return err;
128}
129
Al Viroebfc3b42012-06-10 18:05:36 -0400130static int hpfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Al Viro7e7742e2010-01-31 17:09:29 -0500132 const unsigned char *name = dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 unsigned len = dentry->d_name.len;
134 struct inode *result = NULL;
135 struct buffer_head *bh;
136 struct fnode *fnode;
137 fnode_secno fno;
138 int r;
139 struct hpfs_dirent dee;
140 int err;
Al Viro7e7742e2010-01-31 17:09:29 -0500141 if ((err = hpfs_chk_name(name, &len)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return err==-ENOENT ? -EINVAL : err;
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100143 hpfs_lock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 err = -ENOSPC;
145 fnode = hpfs_alloc_fnode(dir->i_sb, hpfs_i(dir)->i_dno, &fno, &bh);
146 if (!fnode)
147 goto bail;
148 memset(&dee, 0, sizeof dee);
149 if (!(mode & 0222)) dee.read_only = 1;
150 dee.archive = 1;
151 dee.hidden = name[0] == '.';
Mikulas Patocka0b697602011-05-08 20:44:26 +0200152 dee.fnode = cpu_to_le32(fno);
153 dee.creation_date = dee.write_date = dee.read_date = cpu_to_le32(gmt_to_local(dir->i_sb, get_seconds()));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 result = new_inode(dir->i_sb);
156 if (!result)
157 goto bail1;
158
159 hpfs_init_inode(result);
160 result->i_ino = fno;
161 result->i_mode |= S_IFREG;
162 result->i_mode &= ~0111;
163 result->i_op = &hpfs_file_iops;
164 result->i_fop = &hpfs_file_ops;
Miklos Szeredibfe86842011-10-28 14:13:29 +0200165 set_nlink(result, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 hpfs_i(result)->i_parent_dir = dir->i_ino;
Mikulas Patocka0b697602011-05-08 20:44:26 +0200167 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 -0700168 result->i_ctime.tv_nsec = 0;
169 result->i_mtime.tv_nsec = 0;
170 result->i_atime.tv_nsec = 0;
171 hpfs_i(result)->i_ea_size = 0;
172 if (dee.read_only)
173 result->i_mode &= ~0222;
174 result->i_blocks = 1;
175 result->i_size = 0;
176 result->i_data.a_ops = &hpfs_aops;
177 hpfs_i(result)->mmu_private = 0;
178
Mikulas Patocka7d23ce32011-05-08 20:43:06 +0200179 r = hpfs_add_dirent(dir, name, len, &dee);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 if (r == 1)
181 goto bail2;
182 if (r == -1) {
183 err = -EEXIST;
184 goto bail2;
185 }
186 fnode->len = len;
187 memcpy(fnode->name, name, len > 15 ? 15 : len);
Mikulas Patocka0b697602011-05-08 20:44:26 +0200188 fnode->up = cpu_to_le32(dir->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 mark_buffer_dirty(bh);
190 brelse(bh);
191
192 insert_inode_hash(result);
193
Eric W. Biederman0e1a43c2012-02-07 16:27:53 -0800194 if (!uid_eq(result->i_uid, current_fsuid()) ||
195 !gid_eq(result->i_gid, current_fsgid()) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 result->i_mode != (mode | S_IFREG)) {
David Howellsde395b82008-11-14 10:38:55 +1100197 result->i_uid = current_fsuid();
198 result->i_gid = current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 result->i_mode = mode | S_IFREG;
200 hpfs_write_inode_nolock(result);
201 }
Mikulas Patockaf49a26e2015-09-02 22:51:53 +0200202 hpfs_update_directory_times(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 d_instantiate(dentry, result);
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100204 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 return 0;
206
207bail2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 iput(result);
209bail1:
210 brelse(bh);
211 hpfs_free_sectors(dir->i_sb, fno, 1);
212bail:
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100213 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return err;
215}
216
Al Viro1a67aaf2011-07-26 01:52:52 -0400217static int hpfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Al Viro7e7742e2010-01-31 17:09:29 -0500219 const unsigned char *name = dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 unsigned len = dentry->d_name.len;
221 struct buffer_head *bh;
222 struct fnode *fnode;
223 fnode_secno fno;
224 int r;
225 struct hpfs_dirent dee;
226 struct inode *result = NULL;
227 int err;
Al Viro7e7742e2010-01-31 17:09:29 -0500228 if ((err = hpfs_chk_name(name, &len))) return err==-ENOENT ? -EINVAL : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 if (hpfs_sb(dir->i_sb)->sb_eas < 2) return -EPERM;
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100230 hpfs_lock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 err = -ENOSPC;
232 fnode = hpfs_alloc_fnode(dir->i_sb, hpfs_i(dir)->i_dno, &fno, &bh);
233 if (!fnode)
234 goto bail;
235 memset(&dee, 0, sizeof dee);
236 if (!(mode & 0222)) dee.read_only = 1;
237 dee.archive = 1;
238 dee.hidden = name[0] == '.';
Mikulas Patocka0b697602011-05-08 20:44:26 +0200239 dee.fnode = cpu_to_le32(fno);
240 dee.creation_date = dee.write_date = dee.read_date = cpu_to_le32(gmt_to_local(dir->i_sb, get_seconds()));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 result = new_inode(dir->i_sb);
243 if (!result)
244 goto bail1;
245
246 hpfs_init_inode(result);
247 result->i_ino = fno;
248 hpfs_i(result)->i_parent_dir = dir->i_ino;
Mikulas Patocka0b697602011-05-08 20:44:26 +0200249 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 -0700250 result->i_ctime.tv_nsec = 0;
251 result->i_mtime.tv_nsec = 0;
252 result->i_atime.tv_nsec = 0;
253 hpfs_i(result)->i_ea_size = 0;
David Howellsde395b82008-11-14 10:38:55 +1100254 result->i_uid = current_fsuid();
255 result->i_gid = current_fsgid();
Miklos Szeredibfe86842011-10-28 14:13:29 +0200256 set_nlink(result, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 result->i_size = 0;
258 result->i_blocks = 1;
259 init_special_inode(result, mode, rdev);
260
Mikulas Patocka7d23ce32011-05-08 20:43:06 +0200261 r = hpfs_add_dirent(dir, name, len, &dee);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (r == 1)
263 goto bail2;
264 if (r == -1) {
265 err = -EEXIST;
266 goto bail2;
267 }
268 fnode->len = len;
269 memcpy(fnode->name, name, len > 15 ? 15 : len);
Mikulas Patocka0b697602011-05-08 20:44:26 +0200270 fnode->up = cpu_to_le32(dir->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 mark_buffer_dirty(bh);
272
273 insert_inode_hash(result);
274
275 hpfs_write_inode_nolock(result);
Mikulas Patockaf49a26e2015-09-02 22:51:53 +0200276 hpfs_update_directory_times(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 d_instantiate(dentry, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 brelse(bh);
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100279 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return 0;
281bail2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 iput(result);
283bail1:
284 brelse(bh);
285 hpfs_free_sectors(dir->i_sb, fno, 1);
286bail:
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100287 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return err;
289}
290
291static int hpfs_symlink(struct inode *dir, struct dentry *dentry, const char *symlink)
292{
Al Viro7e7742e2010-01-31 17:09:29 -0500293 const unsigned char *name = dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 unsigned len = dentry->d_name.len;
295 struct buffer_head *bh;
296 struct fnode *fnode;
297 fnode_secno fno;
298 int r;
299 struct hpfs_dirent dee;
300 struct inode *result;
301 int err;
Al Viro7e7742e2010-01-31 17:09:29 -0500302 if ((err = hpfs_chk_name(name, &len))) return err==-ENOENT ? -EINVAL : err;
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100303 hpfs_lock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 if (hpfs_sb(dir->i_sb)->sb_eas < 2) {
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100305 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return -EPERM;
307 }
308 err = -ENOSPC;
309 fnode = hpfs_alloc_fnode(dir->i_sb, hpfs_i(dir)->i_dno, &fno, &bh);
310 if (!fnode)
311 goto bail;
312 memset(&dee, 0, sizeof dee);
313 dee.archive = 1;
314 dee.hidden = name[0] == '.';
Mikulas Patocka0b697602011-05-08 20:44:26 +0200315 dee.fnode = cpu_to_le32(fno);
316 dee.creation_date = dee.write_date = dee.read_date = cpu_to_le32(gmt_to_local(dir->i_sb, get_seconds()));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 result = new_inode(dir->i_sb);
319 if (!result)
320 goto bail1;
321 result->i_ino = fno;
322 hpfs_init_inode(result);
323 hpfs_i(result)->i_parent_dir = dir->i_ino;
Mikulas Patocka0b697602011-05-08 20:44:26 +0200324 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 -0700325 result->i_ctime.tv_nsec = 0;
326 result->i_mtime.tv_nsec = 0;
327 result->i_atime.tv_nsec = 0;
328 hpfs_i(result)->i_ea_size = 0;
329 result->i_mode = S_IFLNK | 0777;
David Howellsde395b82008-11-14 10:38:55 +1100330 result->i_uid = current_fsuid();
331 result->i_gid = current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 result->i_blocks = 1;
Miklos Szeredibfe86842011-10-28 14:13:29 +0200333 set_nlink(result, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 result->i_size = strlen(symlink);
335 result->i_op = &page_symlink_inode_operations;
336 result->i_data.a_ops = &hpfs_symlink_aops;
337
Mikulas Patocka7d23ce32011-05-08 20:43:06 +0200338 r = hpfs_add_dirent(dir, name, len, &dee);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (r == 1)
340 goto bail2;
341 if (r == -1) {
342 err = -EEXIST;
343 goto bail2;
344 }
345 fnode->len = len;
346 memcpy(fnode->name, name, len > 15 ? 15 : len);
Mikulas Patocka0b697602011-05-08 20:44:26 +0200347 fnode->up = cpu_to_le32(dir->i_ino);
Al Viro7e7742e2010-01-31 17:09:29 -0500348 hpfs_set_ea(result, fnode, "SYMLINK", symlink, strlen(symlink));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 mark_buffer_dirty(bh);
350 brelse(bh);
351
352 insert_inode_hash(result);
353
354 hpfs_write_inode_nolock(result);
Mikulas Patockaf49a26e2015-09-02 22:51:53 +0200355 hpfs_update_directory_times(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 d_instantiate(dentry, result);
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100357 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return 0;
359bail2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 iput(result);
361bail1:
362 brelse(bh);
363 hpfs_free_sectors(dir->i_sb, fno, 1);
364bail:
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100365 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return err;
367}
368
369static int hpfs_unlink(struct inode *dir, struct dentry *dentry)
370{
Al Viro7e7742e2010-01-31 17:09:29 -0500371 const unsigned char *name = dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 unsigned len = dentry->d_name.len;
373 struct quad_buffer_head qbh;
374 struct hpfs_dirent *de;
David Howells2b0143b2015-03-17 22:25:59 +0000375 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 dnode_secno dno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 int r;
378 int rep = 0;
379 int err;
380
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100381 hpfs_lock(dir->i_sb);
Al Viro7e7742e2010-01-31 17:09:29 -0500382 hpfs_adjust_length(name, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383again:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 err = -ENOENT;
Al Viro7e7742e2010-01-31 17:09:29 -0500385 de = map_dirent(dir, hpfs_i(dir)->i_dno, name, len, &dno, &qbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (!de)
387 goto out;
388
389 err = -EPERM;
390 if (de->first)
391 goto out1;
392
393 err = -EISDIR;
394 if (de->directory)
395 goto out1;
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 r = hpfs_remove_dirent(dir, dno, de, &qbh, 1);
398 switch (r) {
399 case 1:
400 hpfs_error(dir->i_sb, "there was error when removing dirent");
401 err = -EFSERROR;
402 break;
403 case 2: /* no space for deleting, try to truncate file */
404
405 err = -ENOSPC;
406 if (rep++)
407 break;
408
Al Viroe21e7092010-02-01 11:05:16 -0500409 dentry_unhash(dentry);
410 if (!d_unhashed(dentry)) {
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100411 hpfs_unlock(dir->i_sb);
Al Viroe21e7092010-02-01 11:05:16 -0500412 return -ENOSPC;
413 }
Al Viro2830ba72011-06-20 19:16:29 -0400414 if (generic_permission(inode, MAY_WRITE) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 !S_ISREG(inode->i_mode) ||
416 get_write_access(inode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 d_rehash(dentry);
418 } else {
419 struct iattr newattrs;
Fabian Frederick14da17f2014-06-06 14:36:34 -0700420 /*pr_info("truncating file before delete.\n");*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 newattrs.ia_size = 0;
422 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
J. Bruce Fields27ac0ff2011-09-20 17:19:26 -0400423 err = notify_change(dentry, &newattrs, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 put_write_access(inode);
425 if (!err)
426 goto again;
427 }
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100428 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return -ENOSPC;
430 default:
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700431 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 err = 0;
433 }
434 goto out;
435
436out1:
437 hpfs_brelse4(&qbh);
438out:
Mikulas Patockaf49a26e2015-09-02 22:51:53 +0200439 if (!err)
440 hpfs_update_directory_times(dir);
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100441 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return err;
443}
444
445static int hpfs_rmdir(struct inode *dir, struct dentry *dentry)
446{
Al Viro7e7742e2010-01-31 17:09:29 -0500447 const unsigned char *name = dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 unsigned len = dentry->d_name.len;
449 struct quad_buffer_head qbh;
450 struct hpfs_dirent *de;
David Howells2b0143b2015-03-17 22:25:59 +0000451 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 dnode_secno dno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 int n_items = 0;
454 int err;
455 int r;
456
Al Viro7e7742e2010-01-31 17:09:29 -0500457 hpfs_adjust_length(name, &len);
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100458 hpfs_lock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 err = -ENOENT;
Al Viro7e7742e2010-01-31 17:09:29 -0500460 de = map_dirent(dir, hpfs_i(dir)->i_dno, name, len, &dno, &qbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if (!de)
462 goto out;
463
464 err = -EPERM;
465 if (de->first)
466 goto out1;
467
468 err = -ENOTDIR;
469 if (!de->directory)
470 goto out1;
471
472 hpfs_count_dnodes(dir->i_sb, hpfs_i(inode)->i_dno, NULL, NULL, &n_items);
473 err = -ENOTEMPTY;
474 if (n_items)
475 goto out1;
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 r = hpfs_remove_dirent(dir, dno, de, &qbh, 1);
478 switch (r) {
479 case 1:
480 hpfs_error(dir->i_sb, "there was error when removing dirent");
481 err = -EFSERROR;
482 break;
483 case 2:
484 err = -ENOSPC;
485 break;
486 default:
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700487 drop_nlink(dir);
Dave Hansence71ec32006-09-30 23:29:06 -0700488 clear_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 err = 0;
490 }
491 goto out;
492out1:
493 hpfs_brelse4(&qbh);
494out:
Mikulas Patockaf49a26e2015-09-02 22:51:53 +0200495 if (!err)
496 hpfs_update_directory_times(dir);
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100497 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return err;
499}
500
501static int hpfs_symlink_readpage(struct file *file, struct page *page)
502{
503 char *link = kmap(page);
504 struct inode *i = page->mapping->host;
505 struct fnode *fnode;
506 struct buffer_head *bh;
507 int err;
508
509 err = -EIO;
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100510 hpfs_lock(i->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (!(fnode = hpfs_map_fnode(i->i_sb, i->i_ino, &bh)))
512 goto fail;
513 err = hpfs_read_ea(i->i_sb, fnode, "SYMLINK", link, PAGE_SIZE);
514 brelse(bh);
515 if (err)
516 goto fail;
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100517 hpfs_unlock(i->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 SetPageUptodate(page);
519 kunmap(page);
520 unlock_page(page);
521 return 0;
522
523fail:
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100524 hpfs_unlock(i->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 SetPageError(page);
526 kunmap(page);
527 unlock_page(page);
528 return err;
529}
530
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700531const struct address_space_operations hpfs_symlink_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 .readpage = hpfs_symlink_readpage
533};
534
535static int hpfs_rename(struct inode *old_dir, struct dentry *old_dentry,
536 struct inode *new_dir, struct dentry *new_dentry)
537{
Al Viro7e7742e2010-01-31 17:09:29 -0500538 const unsigned char *old_name = old_dentry->d_name.name;
539 unsigned old_len = old_dentry->d_name.len;
540 const unsigned char *new_name = new_dentry->d_name.name;
541 unsigned new_len = new_dentry->d_name.len;
David Howells2b0143b2015-03-17 22:25:59 +0000542 struct inode *i = d_inode(old_dentry);
543 struct inode *new_inode = d_inode(new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 struct quad_buffer_head qbh, qbh1;
545 struct hpfs_dirent *dep, *nde;
546 struct hpfs_dirent de;
547 dnode_secno dno;
548 int r;
549 struct buffer_head *bh;
550 struct fnode *fnode;
551 int err;
Sage Weile4eaac02011-05-24 13:06:07 -0700552
Al Viro7e7742e2010-01-31 17:09:29 -0500553 if ((err = hpfs_chk_name(new_name, &new_len))) return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 err = 0;
Al Viro7e7742e2010-01-31 17:09:29 -0500555 hpfs_adjust_length(old_name, &old_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100557 hpfs_lock(i->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 /* order doesn't matter, due to VFS exclusion */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 /* Erm? Moving over the empty non-busy directory is perfectly legal */
561 if (new_inode && S_ISDIR(new_inode->i_mode)) {
562 err = -EINVAL;
563 goto end1;
564 }
565
Al Viro7e7742e2010-01-31 17:09:29 -0500566 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 -0700567 hpfs_error(i->i_sb, "lookup succeeded but map dirent failed");
568 err = -ENOENT;
569 goto end1;
570 }
571 copy_de(&de, dep);
572 de.hidden = new_name[0] == '.';
573
574 if (new_inode) {
575 int r;
576 if ((r = hpfs_remove_dirent(old_dir, dno, dep, &qbh, 1)) != 2) {
Al Viro7e7742e2010-01-31 17:09:29 -0500577 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 -0700578 clear_nlink(new_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 copy_de(nde, &de);
580 memcpy(nde->name, new_name, new_len);
581 hpfs_mark_4buffers_dirty(&qbh1);
582 hpfs_brelse4(&qbh1);
583 goto end;
584 }
585 hpfs_error(new_dir->i_sb, "hpfs_rename: could not find dirent");
586 err = -EFSERROR;
587 goto end1;
588 }
589 err = r == 2 ? -ENOSPC : r == 1 ? -EFSERROR : 0;
590 goto end1;
591 }
592
593 if (new_dir == old_dir) hpfs_brelse4(&qbh);
594
Mikulas Patocka7d23ce32011-05-08 20:43:06 +0200595 if ((r = hpfs_add_dirent(new_dir, new_name, new_len, &de))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (r == -1) hpfs_error(new_dir->i_sb, "hpfs_rename: dirent already exists!");
597 err = r == 1 ? -ENOSPC : -EFSERROR;
598 if (new_dir != old_dir) hpfs_brelse4(&qbh);
599 goto end1;
600 }
601
602 if (new_dir == old_dir)
Al Viro7e7742e2010-01-31 17:09:29 -0500603 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 -0700604 hpfs_error(i->i_sb, "lookup succeeded but map dirent failed at #2");
605 err = -ENOENT;
606 goto end1;
607 }
608
609 if ((r = hpfs_remove_dirent(old_dir, dno, dep, &qbh, 0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 hpfs_error(i->i_sb, "hpfs_rename: could not remove dirent");
611 err = r == 2 ? -ENOSPC : -EFSERROR;
612 goto end1;
613 }
Mikulas Patocka7d23ce32011-05-08 20:43:06 +0200614
Mikulas Patockaf49a26e2015-09-02 22:51:53 +0200615end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 hpfs_i(i)->i_parent_dir = new_dir->i_ino;
617 if (S_ISDIR(i->i_mode)) {
Dave Hansend8c76e62006-09-30 23:29:04 -0700618 inc_nlink(new_dir);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700619 drop_nlink(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
621 if ((fnode = hpfs_map_fnode(i->i_sb, i->i_ino, &bh))) {
Mikulas Patocka0b697602011-05-08 20:44:26 +0200622 fnode->up = cpu_to_le32(new_dir->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 fnode->len = new_len;
624 memcpy(fnode->name, new_name, new_len>15?15:new_len);
625 if (new_len < 15) memset(&fnode->name[new_len], 0, 15 - new_len);
626 mark_buffer_dirty(bh);
627 brelse(bh);
628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629end1:
Mikulas Patockaf49a26e2015-09-02 22:51:53 +0200630 if (!err) {
631 hpfs_update_directory_times(old_dir);
632 hpfs_update_directory_times(new_dir);
633 }
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100634 hpfs_unlock(i->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return err;
636}
637
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800638const struct inode_operations hpfs_dir_iops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
640 .create = hpfs_create,
641 .lookup = hpfs_lookup,
642 .unlink = hpfs_unlink,
643 .symlink = hpfs_symlink,
644 .mkdir = hpfs_mkdir,
645 .rmdir = hpfs_rmdir,
646 .mknod = hpfs_mknod,
647 .rename = hpfs_rename,
Christoph Hellwigca30bc92008-08-11 00:27:59 +0200648 .setattr = hpfs_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649};