blob: ff0ce21c0867065f2efbcb30fbfee929ee6e4e09 [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
11static int hpfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
12{
Al Viro7e7742e2010-01-31 17:09:29 -050013 const unsigned char *name = dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 unsigned len = dentry->d_name.len;
15 struct quad_buffer_head qbh0;
16 struct buffer_head *bh;
17 struct hpfs_dirent *de;
18 struct fnode *fnode;
19 struct dnode *dnode;
20 struct inode *result;
21 fnode_secno fno;
22 dnode_secno dno;
23 int r;
24 struct hpfs_dirent dee;
25 int err;
Al Viro7e7742e2010-01-31 17:09:29 -050026 if ((err = hpfs_chk_name(name, &len))) return err==-ENOENT ? -EINVAL : err;
Arnd Bergmann9a311b92011-01-22 20:26:12 +010027 hpfs_lock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 err = -ENOSPC;
29 fnode = hpfs_alloc_fnode(dir->i_sb, hpfs_i(dir)->i_dno, &fno, &bh);
30 if (!fnode)
31 goto bail;
Mikulas Patocka7d23ce32011-05-08 20:43:06 +020032 dnode = hpfs_alloc_dnode(dir->i_sb, fno, &dno, &qbh0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 if (!dnode)
34 goto bail1;
35 memset(&dee, 0, sizeof dee);
36 dee.directory = 1;
37 if (!(mode & 0222)) dee.read_only = 1;
38 /*dee.archive = 0;*/
39 dee.hidden = name[0] == '.';
Mikulas Patocka0b697602011-05-08 20:44:26 +020040 dee.fnode = cpu_to_le32(fno);
41 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 -070042 result = new_inode(dir->i_sb);
43 if (!result)
44 goto bail2;
45 hpfs_init_inode(result);
46 result->i_ino = fno;
47 hpfs_i(result)->i_parent_dir = dir->i_ino;
48 hpfs_i(result)->i_dno = dno;
Mikulas Patocka0b697602011-05-08 20:44:26 +020049 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 -070050 result->i_ctime.tv_nsec = 0;
51 result->i_mtime.tv_nsec = 0;
52 result->i_atime.tv_nsec = 0;
53 hpfs_i(result)->i_ea_size = 0;
54 result->i_mode |= S_IFDIR;
55 result->i_op = &hpfs_dir_iops;
56 result->i_fop = &hpfs_dir_ops;
57 result->i_blocks = 4;
58 result->i_size = 2048;
59 result->i_nlink = 2;
60 if (dee.read_only)
61 result->i_mode &= ~0222;
62
Mikulas Patocka7d23ce32011-05-08 20:43:06 +020063 r = hpfs_add_dirent(dir, name, len, &dee);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 if (r == 1)
65 goto bail3;
66 if (r == -1) {
67 err = -EEXIST;
68 goto bail3;
69 }
70 fnode->len = len;
71 memcpy(fnode->name, name, len > 15 ? 15 : len);
Mikulas Patocka0b697602011-05-08 20:44:26 +020072 fnode->up = cpu_to_le32(dir->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 fnode->dirflag = 1;
74 fnode->btree.n_free_nodes = 7;
75 fnode->btree.n_used_nodes = 1;
Mikulas Patocka0b697602011-05-08 20:44:26 +020076 fnode->btree.first_free = cpu_to_le16(0x14);
77 fnode->u.external[0].disk_secno = cpu_to_le32(dno);
78 fnode->u.external[0].file_secno = cpu_to_le32(-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 dnode->root_dnode = 1;
Mikulas Patocka0b697602011-05-08 20:44:26 +020080 dnode->up = cpu_to_le32(fno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 de = hpfs_add_de(dir->i_sb, dnode, "\001\001", 2, 0);
Mikulas Patocka0b697602011-05-08 20:44:26 +020082 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 -070083 if (!(mode & 0222)) de->read_only = 1;
84 de->first = de->directory = 1;
85 /*de->hidden = de->system = 0;*/
Mikulas Patocka0b697602011-05-08 20:44:26 +020086 de->fnode = cpu_to_le32(fno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 mark_buffer_dirty(bh);
88 brelse(bh);
89 hpfs_mark_4buffers_dirty(&qbh0);
90 hpfs_brelse4(&qbh0);
Dave Hansend8c76e62006-09-30 23:29:04 -070091 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 insert_inode_hash(result);
93
David Howellsde395b82008-11-14 10:38:55 +110094 if (result->i_uid != current_fsuid() ||
95 result->i_gid != current_fsgid() ||
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 result->i_mode != (mode | S_IFDIR)) {
David Howellsde395b82008-11-14 10:38:55 +110097 result->i_uid = current_fsuid();
98 result->i_gid = current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 result->i_mode = mode | S_IFDIR;
100 hpfs_write_inode_nolock(result);
101 }
102 d_instantiate(dentry, result);
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100103 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return 0;
105bail3:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 iput(result);
107bail2:
108 hpfs_brelse4(&qbh0);
109 hpfs_free_dnode(dir->i_sb, dno);
110bail1:
111 brelse(bh);
112 hpfs_free_sectors(dir->i_sb, fno, 1);
113bail:
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100114 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return err;
116}
117
118static int hpfs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
119{
Al Viro7e7742e2010-01-31 17:09:29 -0500120 const unsigned char *name = dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 unsigned len = dentry->d_name.len;
122 struct inode *result = NULL;
123 struct buffer_head *bh;
124 struct fnode *fnode;
125 fnode_secno fno;
126 int r;
127 struct hpfs_dirent dee;
128 int err;
Al Viro7e7742e2010-01-31 17:09:29 -0500129 if ((err = hpfs_chk_name(name, &len)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 return err==-ENOENT ? -EINVAL : err;
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100131 hpfs_lock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 err = -ENOSPC;
133 fnode = hpfs_alloc_fnode(dir->i_sb, hpfs_i(dir)->i_dno, &fno, &bh);
134 if (!fnode)
135 goto bail;
136 memset(&dee, 0, sizeof dee);
137 if (!(mode & 0222)) dee.read_only = 1;
138 dee.archive = 1;
139 dee.hidden = name[0] == '.';
Mikulas Patocka0b697602011-05-08 20:44:26 +0200140 dee.fnode = cpu_to_le32(fno);
141 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 -0700142
143 result = new_inode(dir->i_sb);
144 if (!result)
145 goto bail1;
146
147 hpfs_init_inode(result);
148 result->i_ino = fno;
149 result->i_mode |= S_IFREG;
150 result->i_mode &= ~0111;
151 result->i_op = &hpfs_file_iops;
152 result->i_fop = &hpfs_file_ops;
153 result->i_nlink = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 hpfs_i(result)->i_parent_dir = dir->i_ino;
Mikulas Patocka0b697602011-05-08 20:44:26 +0200155 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 -0700156 result->i_ctime.tv_nsec = 0;
157 result->i_mtime.tv_nsec = 0;
158 result->i_atime.tv_nsec = 0;
159 hpfs_i(result)->i_ea_size = 0;
160 if (dee.read_only)
161 result->i_mode &= ~0222;
162 result->i_blocks = 1;
163 result->i_size = 0;
164 result->i_data.a_ops = &hpfs_aops;
165 hpfs_i(result)->mmu_private = 0;
166
Mikulas Patocka7d23ce32011-05-08 20:43:06 +0200167 r = hpfs_add_dirent(dir, name, len, &dee);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 if (r == 1)
169 goto bail2;
170 if (r == -1) {
171 err = -EEXIST;
172 goto bail2;
173 }
174 fnode->len = len;
175 memcpy(fnode->name, name, len > 15 ? 15 : len);
Mikulas Patocka0b697602011-05-08 20:44:26 +0200176 fnode->up = cpu_to_le32(dir->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 mark_buffer_dirty(bh);
178 brelse(bh);
179
180 insert_inode_hash(result);
181
David Howellsde395b82008-11-14 10:38:55 +1100182 if (result->i_uid != current_fsuid() ||
183 result->i_gid != current_fsgid() ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 result->i_mode != (mode | S_IFREG)) {
David Howellsde395b82008-11-14 10:38:55 +1100185 result->i_uid = current_fsuid();
186 result->i_gid = current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 result->i_mode = mode | S_IFREG;
188 hpfs_write_inode_nolock(result);
189 }
190 d_instantiate(dentry, result);
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100191 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return 0;
193
194bail2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 iput(result);
196bail1:
197 brelse(bh);
198 hpfs_free_sectors(dir->i_sb, fno, 1);
199bail:
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100200 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 return err;
202}
203
204static int hpfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
205{
Al Viro7e7742e2010-01-31 17:09:29 -0500206 const unsigned char *name = dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 unsigned len = dentry->d_name.len;
208 struct buffer_head *bh;
209 struct fnode *fnode;
210 fnode_secno fno;
211 int r;
212 struct hpfs_dirent dee;
213 struct inode *result = NULL;
214 int err;
Al Viro7e7742e2010-01-31 17:09:29 -0500215 if ((err = hpfs_chk_name(name, &len))) return err==-ENOENT ? -EINVAL : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 if (hpfs_sb(dir->i_sb)->sb_eas < 2) return -EPERM;
217 if (!new_valid_dev(rdev))
218 return -EINVAL;
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100219 hpfs_lock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 err = -ENOSPC;
221 fnode = hpfs_alloc_fnode(dir->i_sb, hpfs_i(dir)->i_dno, &fno, &bh);
222 if (!fnode)
223 goto bail;
224 memset(&dee, 0, sizeof dee);
225 if (!(mode & 0222)) dee.read_only = 1;
226 dee.archive = 1;
227 dee.hidden = name[0] == '.';
Mikulas Patocka0b697602011-05-08 20:44:26 +0200228 dee.fnode = cpu_to_le32(fno);
229 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 -0700230
231 result = new_inode(dir->i_sb);
232 if (!result)
233 goto bail1;
234
235 hpfs_init_inode(result);
236 result->i_ino = fno;
237 hpfs_i(result)->i_parent_dir = dir->i_ino;
Mikulas Patocka0b697602011-05-08 20:44:26 +0200238 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 -0700239 result->i_ctime.tv_nsec = 0;
240 result->i_mtime.tv_nsec = 0;
241 result->i_atime.tv_nsec = 0;
242 hpfs_i(result)->i_ea_size = 0;
David Howellsde395b82008-11-14 10:38:55 +1100243 result->i_uid = current_fsuid();
244 result->i_gid = current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 result->i_nlink = 1;
246 result->i_size = 0;
247 result->i_blocks = 1;
248 init_special_inode(result, mode, rdev);
249
Mikulas Patocka7d23ce32011-05-08 20:43:06 +0200250 r = hpfs_add_dirent(dir, name, len, &dee);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if (r == 1)
252 goto bail2;
253 if (r == -1) {
254 err = -EEXIST;
255 goto bail2;
256 }
257 fnode->len = len;
258 memcpy(fnode->name, name, len > 15 ? 15 : len);
Mikulas Patocka0b697602011-05-08 20:44:26 +0200259 fnode->up = cpu_to_le32(dir->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 mark_buffer_dirty(bh);
261
262 insert_inode_hash(result);
263
264 hpfs_write_inode_nolock(result);
265 d_instantiate(dentry, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 brelse(bh);
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100267 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return 0;
269bail2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 iput(result);
271bail1:
272 brelse(bh);
273 hpfs_free_sectors(dir->i_sb, fno, 1);
274bail:
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100275 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return err;
277}
278
279static int hpfs_symlink(struct inode *dir, struct dentry *dentry, const char *symlink)
280{
Al Viro7e7742e2010-01-31 17:09:29 -0500281 const unsigned char *name = dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 unsigned len = dentry->d_name.len;
283 struct buffer_head *bh;
284 struct fnode *fnode;
285 fnode_secno fno;
286 int r;
287 struct hpfs_dirent dee;
288 struct inode *result;
289 int err;
Al Viro7e7742e2010-01-31 17:09:29 -0500290 if ((err = hpfs_chk_name(name, &len))) return err==-ENOENT ? -EINVAL : err;
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100291 hpfs_lock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (hpfs_sb(dir->i_sb)->sb_eas < 2) {
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100293 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return -EPERM;
295 }
296 err = -ENOSPC;
297 fnode = hpfs_alloc_fnode(dir->i_sb, hpfs_i(dir)->i_dno, &fno, &bh);
298 if (!fnode)
299 goto bail;
300 memset(&dee, 0, sizeof dee);
301 dee.archive = 1;
302 dee.hidden = name[0] == '.';
Mikulas Patocka0b697602011-05-08 20:44:26 +0200303 dee.fnode = cpu_to_le32(fno);
304 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 -0700305
306 result = new_inode(dir->i_sb);
307 if (!result)
308 goto bail1;
309 result->i_ino = fno;
310 hpfs_init_inode(result);
311 hpfs_i(result)->i_parent_dir = dir->i_ino;
Mikulas Patocka0b697602011-05-08 20:44:26 +0200312 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 -0700313 result->i_ctime.tv_nsec = 0;
314 result->i_mtime.tv_nsec = 0;
315 result->i_atime.tv_nsec = 0;
316 hpfs_i(result)->i_ea_size = 0;
317 result->i_mode = S_IFLNK | 0777;
David Howellsde395b82008-11-14 10:38:55 +1100318 result->i_uid = current_fsuid();
319 result->i_gid = current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 result->i_blocks = 1;
321 result->i_nlink = 1;
322 result->i_size = strlen(symlink);
323 result->i_op = &page_symlink_inode_operations;
324 result->i_data.a_ops = &hpfs_symlink_aops;
325
Mikulas Patocka7d23ce32011-05-08 20:43:06 +0200326 r = hpfs_add_dirent(dir, name, len, &dee);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 if (r == 1)
328 goto bail2;
329 if (r == -1) {
330 err = -EEXIST;
331 goto bail2;
332 }
333 fnode->len = len;
334 memcpy(fnode->name, name, len > 15 ? 15 : len);
Mikulas Patocka0b697602011-05-08 20:44:26 +0200335 fnode->up = cpu_to_le32(dir->i_ino);
Al Viro7e7742e2010-01-31 17:09:29 -0500336 hpfs_set_ea(result, fnode, "SYMLINK", symlink, strlen(symlink));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 mark_buffer_dirty(bh);
338 brelse(bh);
339
340 insert_inode_hash(result);
341
342 hpfs_write_inode_nolock(result);
343 d_instantiate(dentry, result);
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100344 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return 0;
346bail2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 iput(result);
348bail1:
349 brelse(bh);
350 hpfs_free_sectors(dir->i_sb, fno, 1);
351bail:
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100352 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return err;
354}
355
356static int hpfs_unlink(struct inode *dir, struct dentry *dentry)
357{
Al Viro7e7742e2010-01-31 17:09:29 -0500358 const unsigned char *name = dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 unsigned len = dentry->d_name.len;
360 struct quad_buffer_head qbh;
361 struct hpfs_dirent *de;
362 struct inode *inode = dentry->d_inode;
363 dnode_secno dno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 int r;
365 int rep = 0;
366 int err;
367
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100368 hpfs_lock(dir->i_sb);
Al Viro7e7742e2010-01-31 17:09:29 -0500369 hpfs_adjust_length(name, &len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370again:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 err = -ENOENT;
Al Viro7e7742e2010-01-31 17:09:29 -0500372 de = map_dirent(dir, hpfs_i(dir)->i_dno, name, len, &dno, &qbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 if (!de)
374 goto out;
375
376 err = -EPERM;
377 if (de->first)
378 goto out1;
379
380 err = -EISDIR;
381 if (de->directory)
382 goto out1;
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 r = hpfs_remove_dirent(dir, dno, de, &qbh, 1);
385 switch (r) {
386 case 1:
387 hpfs_error(dir->i_sb, "there was error when removing dirent");
388 err = -EFSERROR;
389 break;
390 case 2: /* no space for deleting, try to truncate file */
391
392 err = -ENOSPC;
393 if (rep++)
394 break;
395
Al Viroe21e7092010-02-01 11:05:16 -0500396 dentry_unhash(dentry);
397 if (!d_unhashed(dentry)) {
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100398 hpfs_unlock(dir->i_sb);
Al Viroe21e7092010-02-01 11:05:16 -0500399 return -ENOSPC;
400 }
Nick Pigginb74c79e2011-01-07 17:49:58 +1100401 if (generic_permission(inode, MAY_WRITE, 0, NULL) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 !S_ISREG(inode->i_mode) ||
403 get_write_access(inode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 d_rehash(dentry);
405 } else {
406 struct iattr newattrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 /*printk("HPFS: truncating file before delete.\n");*/
408 newattrs.ia_size = 0;
409 newattrs.ia_valid = ATTR_SIZE | ATTR_CTIME;
410 err = notify_change(dentry, &newattrs);
411 put_write_access(inode);
412 if (!err)
413 goto again;
414 }
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100415 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return -ENOSPC;
417 default:
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700418 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 err = 0;
420 }
421 goto out;
422
423out1:
424 hpfs_brelse4(&qbh);
425out:
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100426 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return err;
428}
429
430static int hpfs_rmdir(struct inode *dir, struct dentry *dentry)
431{
Al Viro7e7742e2010-01-31 17:09:29 -0500432 const unsigned char *name = dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 unsigned len = dentry->d_name.len;
434 struct quad_buffer_head qbh;
435 struct hpfs_dirent *de;
436 struct inode *inode = dentry->d_inode;
437 dnode_secno dno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 int n_items = 0;
439 int err;
440 int r;
441
Sage Weil79bf7c72011-05-24 13:06:06 -0700442 dentry_unhash(dentry);
443
Al Viro7e7742e2010-01-31 17:09:29 -0500444 hpfs_adjust_length(name, &len);
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100445 hpfs_lock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 err = -ENOENT;
Al Viro7e7742e2010-01-31 17:09:29 -0500447 de = map_dirent(dir, hpfs_i(dir)->i_dno, name, len, &dno, &qbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 if (!de)
449 goto out;
450
451 err = -EPERM;
452 if (de->first)
453 goto out1;
454
455 err = -ENOTDIR;
456 if (!de->directory)
457 goto out1;
458
459 hpfs_count_dnodes(dir->i_sb, hpfs_i(inode)->i_dno, NULL, NULL, &n_items);
460 err = -ENOTEMPTY;
461 if (n_items)
462 goto out1;
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 r = hpfs_remove_dirent(dir, dno, de, &qbh, 1);
465 switch (r) {
466 case 1:
467 hpfs_error(dir->i_sb, "there was error when removing dirent");
468 err = -EFSERROR;
469 break;
470 case 2:
471 err = -ENOSPC;
472 break;
473 default:
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700474 drop_nlink(dir);
Dave Hansence71ec32006-09-30 23:29:06 -0700475 clear_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 err = 0;
477 }
478 goto out;
479out1:
480 hpfs_brelse4(&qbh);
481out:
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100482 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 return err;
484}
485
486static int hpfs_symlink_readpage(struct file *file, struct page *page)
487{
488 char *link = kmap(page);
489 struct inode *i = page->mapping->host;
490 struct fnode *fnode;
491 struct buffer_head *bh;
492 int err;
493
494 err = -EIO;
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100495 hpfs_lock(i->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (!(fnode = hpfs_map_fnode(i->i_sb, i->i_ino, &bh)))
497 goto fail;
498 err = hpfs_read_ea(i->i_sb, fnode, "SYMLINK", link, PAGE_SIZE);
499 brelse(bh);
500 if (err)
501 goto fail;
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100502 hpfs_unlock(i->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 SetPageUptodate(page);
504 kunmap(page);
505 unlock_page(page);
506 return 0;
507
508fail:
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100509 hpfs_unlock(i->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 SetPageError(page);
511 kunmap(page);
512 unlock_page(page);
513 return err;
514}
515
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700516const struct address_space_operations hpfs_symlink_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 .readpage = hpfs_symlink_readpage
518};
519
520static int hpfs_rename(struct inode *old_dir, struct dentry *old_dentry,
521 struct inode *new_dir, struct dentry *new_dentry)
522{
Al Viro7e7742e2010-01-31 17:09:29 -0500523 const unsigned char *old_name = old_dentry->d_name.name;
524 unsigned old_len = old_dentry->d_name.len;
525 const unsigned char *new_name = new_dentry->d_name.name;
526 unsigned new_len = new_dentry->d_name.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 struct inode *i = old_dentry->d_inode;
528 struct inode *new_inode = new_dentry->d_inode;
529 struct quad_buffer_head qbh, qbh1;
530 struct hpfs_dirent *dep, *nde;
531 struct hpfs_dirent de;
532 dnode_secno dno;
533 int r;
534 struct buffer_head *bh;
535 struct fnode *fnode;
536 int err;
Sage Weile4eaac02011-05-24 13:06:07 -0700537
538 if (new_inode && S_ISDIR(new_inode->i_mode))
539 dentry_unhash(new_dentry);
540
Al Viro7e7742e2010-01-31 17:09:29 -0500541 if ((err = hpfs_chk_name(new_name, &new_len))) return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 err = 0;
Al Viro7e7742e2010-01-31 17:09:29 -0500543 hpfs_adjust_length(old_name, &old_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100545 hpfs_lock(i->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 /* order doesn't matter, due to VFS exclusion */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 /* Erm? Moving over the empty non-busy directory is perfectly legal */
549 if (new_inode && S_ISDIR(new_inode->i_mode)) {
550 err = -EINVAL;
551 goto end1;
552 }
553
Al Viro7e7742e2010-01-31 17:09:29 -0500554 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 -0700555 hpfs_error(i->i_sb, "lookup succeeded but map dirent failed");
556 err = -ENOENT;
557 goto end1;
558 }
559 copy_de(&de, dep);
560 de.hidden = new_name[0] == '.';
561
562 if (new_inode) {
563 int r;
564 if ((r = hpfs_remove_dirent(old_dir, dno, dep, &qbh, 1)) != 2) {
Al Viro7e7742e2010-01-31 17:09:29 -0500565 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 -0700566 clear_nlink(new_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 copy_de(nde, &de);
568 memcpy(nde->name, new_name, new_len);
569 hpfs_mark_4buffers_dirty(&qbh1);
570 hpfs_brelse4(&qbh1);
571 goto end;
572 }
573 hpfs_error(new_dir->i_sb, "hpfs_rename: could not find dirent");
574 err = -EFSERROR;
575 goto end1;
576 }
577 err = r == 2 ? -ENOSPC : r == 1 ? -EFSERROR : 0;
578 goto end1;
579 }
580
581 if (new_dir == old_dir) hpfs_brelse4(&qbh);
582
Mikulas Patocka7d23ce32011-05-08 20:43:06 +0200583 if ((r = hpfs_add_dirent(new_dir, new_name, new_len, &de))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (r == -1) hpfs_error(new_dir->i_sb, "hpfs_rename: dirent already exists!");
585 err = r == 1 ? -ENOSPC : -EFSERROR;
586 if (new_dir != old_dir) hpfs_brelse4(&qbh);
587 goto end1;
588 }
589
590 if (new_dir == old_dir)
Al Viro7e7742e2010-01-31 17:09:29 -0500591 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 -0700592 hpfs_error(i->i_sb, "lookup succeeded but map dirent failed at #2");
593 err = -ENOENT;
594 goto end1;
595 }
596
597 if ((r = hpfs_remove_dirent(old_dir, dno, dep, &qbh, 0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 hpfs_error(i->i_sb, "hpfs_rename: could not remove dirent");
599 err = r == 2 ? -ENOSPC : -EFSERROR;
600 goto end1;
601 }
Mikulas Patocka7d23ce32011-05-08 20:43:06 +0200602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 end:
604 hpfs_i(i)->i_parent_dir = new_dir->i_ino;
605 if (S_ISDIR(i->i_mode)) {
Dave Hansend8c76e62006-09-30 23:29:04 -0700606 inc_nlink(new_dir);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700607 drop_nlink(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
609 if ((fnode = hpfs_map_fnode(i->i_sb, i->i_ino, &bh))) {
Mikulas Patocka0b697602011-05-08 20:44:26 +0200610 fnode->up = cpu_to_le32(new_dir->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 fnode->len = new_len;
612 memcpy(fnode->name, new_name, new_len>15?15:new_len);
613 if (new_len < 15) memset(&fnode->name[new_len], 0, 15 - new_len);
614 mark_buffer_dirty(bh);
615 brelse(bh);
616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617end1:
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100618 hpfs_unlock(i->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 return err;
620}
621
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800622const struct inode_operations hpfs_dir_iops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
624 .create = hpfs_create,
625 .lookup = hpfs_lookup,
626 .unlink = hpfs_unlink,
627 .symlink = hpfs_symlink,
628 .mkdir = hpfs_mkdir,
629 .rmdir = hpfs_rmdir,
630 .mknod = hpfs_mknod,
631 .rename = hpfs_rename,
Christoph Hellwigca30bc92008-08-11 00:27:59 +0200632 .setattr = hpfs_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633};