1 // SPDX-License-Identifier: GPL-2.0
3 * linux/fs/hpfs/namei.c
5 * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
7 * adding & removing files & directories
9 #include <linux/sched.h>
12 static void hpfs_update_directory_times(struct inode
*dir
)
14 time_t t
= get_seconds();
15 if (t
== dir
->i_mtime
.tv_sec
&&
16 t
== dir
->i_ctime
.tv_sec
)
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
);
23 static int hpfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
25 const unsigned char *name
= dentry
->d_name
.name
;
26 unsigned len
= dentry
->d_name
.len
;
27 struct quad_buffer_head qbh0
;
28 struct buffer_head
*bh
;
29 struct hpfs_dirent
*de
;
36 struct hpfs_dirent dee
;
38 if ((err
= hpfs_chk_name(name
, &len
))) return err
==-ENOENT
? -EINVAL
: err
;
41 fnode
= hpfs_alloc_fnode(dir
->i_sb
, hpfs_i(dir
)->i_dno
, &fno
, &bh
);
44 dnode
= hpfs_alloc_dnode(dir
->i_sb
, fno
, &dno
, &qbh0
);
47 memset(&dee
, 0, sizeof dee
);
49 if (!(mode
& 0222)) dee
.read_only
= 1;
51 dee
.hidden
= name
[0] == '.';
52 dee
.fnode
= cpu_to_le32(fno
);
53 dee
.creation_date
= dee
.write_date
= dee
.read_date
= cpu_to_le32(gmt_to_local(dir
->i_sb
, get_seconds()));
54 result
= new_inode(dir
->i_sb
);
57 hpfs_init_inode(result
);
59 hpfs_i(result
)->i_parent_dir
= dir
->i_ino
;
60 hpfs_i(result
)->i_dno
= dno
;
61 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
));
62 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
;
70 result
->i_size
= 2048;
73 result
->i_mode
&= ~0222;
75 r
= hpfs_add_dirent(dir
, name
, len
, &dee
);
83 memcpy(fnode
->name
, name
, len
> 15 ? 15 : len
);
84 fnode
->up
= cpu_to_le32(dir
->i_ino
);
85 fnode
->flags
|= FNODE_dir
;
86 fnode
->btree
.n_free_nodes
= 7;
87 fnode
->btree
.n_used_nodes
= 1;
88 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);
91 dnode
->root_dnode
= 1;
92 dnode
->up
= cpu_to_le32(fno
);
93 de
= hpfs_add_de(dir
->i_sb
, dnode
, "\001\001", 2, 0);
94 de
->creation_date
= de
->write_date
= de
->read_date
= cpu_to_le32(gmt_to_local(dir
->i_sb
, get_seconds()));
95 if (!(mode
& 0222)) de
->read_only
= 1;
96 de
->first
= de
->directory
= 1;
97 /*de->hidden = de->system = 0;*/
98 de
->fnode
= cpu_to_le32(fno
);
99 mark_buffer_dirty(bh
);
101 hpfs_mark_4buffers_dirty(&qbh0
);
104 insert_inode_hash(result
);
106 if (!uid_eq(result
->i_uid
, current_fsuid()) ||
107 !gid_eq(result
->i_gid
, current_fsgid()) ||
108 result
->i_mode
!= (mode
| S_IFDIR
)) {
109 result
->i_uid
= current_fsuid();
110 result
->i_gid
= current_fsgid();
111 result
->i_mode
= mode
| S_IFDIR
;
112 hpfs_write_inode_nolock(result
);
114 hpfs_update_directory_times(dir
);
115 d_instantiate(dentry
, result
);
116 hpfs_unlock(dir
->i_sb
);
122 hpfs_free_dnode(dir
->i_sb
, dno
);
125 hpfs_free_sectors(dir
->i_sb
, fno
, 1);
127 hpfs_unlock(dir
->i_sb
);
131 static int hpfs_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
, bool excl
)
133 const unsigned char *name
= dentry
->d_name
.name
;
134 unsigned len
= dentry
->d_name
.len
;
135 struct inode
*result
= NULL
;
136 struct buffer_head
*bh
;
140 struct hpfs_dirent dee
;
142 if ((err
= hpfs_chk_name(name
, &len
)))
143 return err
==-ENOENT
? -EINVAL
: err
;
144 hpfs_lock(dir
->i_sb
);
146 fnode
= hpfs_alloc_fnode(dir
->i_sb
, hpfs_i(dir
)->i_dno
, &fno
, &bh
);
149 memset(&dee
, 0, sizeof dee
);
150 if (!(mode
& 0222)) dee
.read_only
= 1;
152 dee
.hidden
= name
[0] == '.';
153 dee
.fnode
= cpu_to_le32(fno
);
154 dee
.creation_date
= dee
.write_date
= dee
.read_date
= cpu_to_le32(gmt_to_local(dir
->i_sb
, get_seconds()));
156 result
= new_inode(dir
->i_sb
);
160 hpfs_init_inode(result
);
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
;
166 set_nlink(result
, 1);
167 hpfs_i(result
)->i_parent_dir
= dir
->i_ino
;
168 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
));
169 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;
174 result
->i_mode
&= ~0222;
175 result
->i_blocks
= 1;
177 result
->i_data
.a_ops
= &hpfs_aops
;
178 hpfs_i(result
)->mmu_private
= 0;
180 r
= hpfs_add_dirent(dir
, name
, len
, &dee
);
188 memcpy(fnode
->name
, name
, len
> 15 ? 15 : len
);
189 fnode
->up
= cpu_to_le32(dir
->i_ino
);
190 mark_buffer_dirty(bh
);
193 insert_inode_hash(result
);
195 if (!uid_eq(result
->i_uid
, current_fsuid()) ||
196 !gid_eq(result
->i_gid
, current_fsgid()) ||
197 result
->i_mode
!= (mode
| S_IFREG
)) {
198 result
->i_uid
= current_fsuid();
199 result
->i_gid
= current_fsgid();
200 result
->i_mode
= mode
| S_IFREG
;
201 hpfs_write_inode_nolock(result
);
203 hpfs_update_directory_times(dir
);
204 d_instantiate(dentry
, result
);
205 hpfs_unlock(dir
->i_sb
);
212 hpfs_free_sectors(dir
->i_sb
, fno
, 1);
214 hpfs_unlock(dir
->i_sb
);
218 static int hpfs_mknod(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
, dev_t rdev
)
220 const unsigned char *name
= dentry
->d_name
.name
;
221 unsigned len
= dentry
->d_name
.len
;
222 struct buffer_head
*bh
;
226 struct hpfs_dirent dee
;
227 struct inode
*result
= NULL
;
229 if ((err
= hpfs_chk_name(name
, &len
))) return err
==-ENOENT
? -EINVAL
: err
;
230 if (hpfs_sb(dir
->i_sb
)->sb_eas
< 2) return -EPERM
;
231 hpfs_lock(dir
->i_sb
);
233 fnode
= hpfs_alloc_fnode(dir
->i_sb
, hpfs_i(dir
)->i_dno
, &fno
, &bh
);
236 memset(&dee
, 0, sizeof dee
);
237 if (!(mode
& 0222)) dee
.read_only
= 1;
239 dee
.hidden
= name
[0] == '.';
240 dee
.fnode
= cpu_to_le32(fno
);
241 dee
.creation_date
= dee
.write_date
= dee
.read_date
= cpu_to_le32(gmt_to_local(dir
->i_sb
, get_seconds()));
243 result
= new_inode(dir
->i_sb
);
247 hpfs_init_inode(result
);
249 hpfs_i(result
)->i_parent_dir
= dir
->i_ino
;
250 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
));
251 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;
255 result
->i_uid
= current_fsuid();
256 result
->i_gid
= current_fsgid();
257 set_nlink(result
, 1);
259 result
->i_blocks
= 1;
260 init_special_inode(result
, mode
, rdev
);
262 r
= hpfs_add_dirent(dir
, name
, len
, &dee
);
270 memcpy(fnode
->name
, name
, len
> 15 ? 15 : len
);
271 fnode
->up
= cpu_to_le32(dir
->i_ino
);
272 mark_buffer_dirty(bh
);
274 insert_inode_hash(result
);
276 hpfs_write_inode_nolock(result
);
277 hpfs_update_directory_times(dir
);
278 d_instantiate(dentry
, result
);
280 hpfs_unlock(dir
->i_sb
);
286 hpfs_free_sectors(dir
->i_sb
, fno
, 1);
288 hpfs_unlock(dir
->i_sb
);
292 static int hpfs_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *symlink
)
294 const unsigned char *name
= dentry
->d_name
.name
;
295 unsigned len
= dentry
->d_name
.len
;
296 struct buffer_head
*bh
;
300 struct hpfs_dirent dee
;
301 struct inode
*result
;
303 if ((err
= hpfs_chk_name(name
, &len
))) return err
==-ENOENT
? -EINVAL
: err
;
304 hpfs_lock(dir
->i_sb
);
305 if (hpfs_sb(dir
->i_sb
)->sb_eas
< 2) {
306 hpfs_unlock(dir
->i_sb
);
310 fnode
= hpfs_alloc_fnode(dir
->i_sb
, hpfs_i(dir
)->i_dno
, &fno
, &bh
);
313 memset(&dee
, 0, sizeof dee
);
315 dee
.hidden
= name
[0] == '.';
316 dee
.fnode
= cpu_to_le32(fno
);
317 dee
.creation_date
= dee
.write_date
= dee
.read_date
= cpu_to_le32(gmt_to_local(dir
->i_sb
, get_seconds()));
319 result
= new_inode(dir
->i_sb
);
323 hpfs_init_inode(result
);
324 hpfs_i(result
)->i_parent_dir
= dir
->i_ino
;
325 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
));
326 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;
331 result
->i_uid
= current_fsuid();
332 result
->i_gid
= current_fsgid();
333 result
->i_blocks
= 1;
334 set_nlink(result
, 1);
335 result
->i_size
= strlen(symlink
);
336 inode_nohighmem(result
);
337 result
->i_op
= &page_symlink_inode_operations
;
338 result
->i_data
.a_ops
= &hpfs_symlink_aops
;
340 r
= hpfs_add_dirent(dir
, name
, len
, &dee
);
348 memcpy(fnode
->name
, name
, len
> 15 ? 15 : len
);
349 fnode
->up
= cpu_to_le32(dir
->i_ino
);
350 hpfs_set_ea(result
, fnode
, "SYMLINK", symlink
, strlen(symlink
));
351 mark_buffer_dirty(bh
);
354 insert_inode_hash(result
);
356 hpfs_write_inode_nolock(result
);
357 hpfs_update_directory_times(dir
);
358 d_instantiate(dentry
, result
);
359 hpfs_unlock(dir
->i_sb
);
365 hpfs_free_sectors(dir
->i_sb
, fno
, 1);
367 hpfs_unlock(dir
->i_sb
);
371 static int hpfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
373 const unsigned char *name
= dentry
->d_name
.name
;
374 unsigned len
= dentry
->d_name
.len
;
375 struct quad_buffer_head qbh
;
376 struct hpfs_dirent
*de
;
377 struct inode
*inode
= d_inode(dentry
);
382 hpfs_lock(dir
->i_sb
);
383 hpfs_adjust_length(name
, &len
);
386 de
= map_dirent(dir
, hpfs_i(dir
)->i_dno
, name
, len
, &dno
, &qbh
);
398 r
= hpfs_remove_dirent(dir
, dno
, de
, &qbh
, 1);
401 hpfs_error(dir
->i_sb
, "there was error when removing dirent");
404 case 2: /* no space for deleting */
417 hpfs_update_directory_times(dir
);
418 hpfs_unlock(dir
->i_sb
);
422 static int hpfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
424 const unsigned char *name
= dentry
->d_name
.name
;
425 unsigned len
= dentry
->d_name
.len
;
426 struct quad_buffer_head qbh
;
427 struct hpfs_dirent
*de
;
428 struct inode
*inode
= d_inode(dentry
);
434 hpfs_adjust_length(name
, &len
);
435 hpfs_lock(dir
->i_sb
);
437 de
= map_dirent(dir
, hpfs_i(dir
)->i_dno
, name
, len
, &dno
, &qbh
);
449 hpfs_count_dnodes(dir
->i_sb
, hpfs_i(inode
)->i_dno
, NULL
, NULL
, &n_items
);
454 r
= hpfs_remove_dirent(dir
, dno
, de
, &qbh
, 1);
457 hpfs_error(dir
->i_sb
, "there was error when removing dirent");
473 hpfs_update_directory_times(dir
);
474 hpfs_unlock(dir
->i_sb
);
478 static int hpfs_symlink_readpage(struct file
*file
, struct page
*page
)
480 char *link
= page_address(page
);
481 struct inode
*i
= page
->mapping
->host
;
483 struct buffer_head
*bh
;
488 if (!(fnode
= hpfs_map_fnode(i
->i_sb
, i
->i_ino
, &bh
)))
490 err
= hpfs_read_ea(i
->i_sb
, fnode
, "SYMLINK", link
, PAGE_SIZE
);
494 hpfs_unlock(i
->i_sb
);
495 SetPageUptodate(page
);
500 hpfs_unlock(i
->i_sb
);
506 const struct address_space_operations hpfs_symlink_aops
= {
507 .readpage
= hpfs_symlink_readpage
510 static int hpfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
511 struct inode
*new_dir
, struct dentry
*new_dentry
,
514 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
;
518 struct inode
*i
= d_inode(old_dentry
);
519 struct inode
*new_inode
= d_inode(new_dentry
);
520 struct quad_buffer_head qbh
, qbh1
;
521 struct hpfs_dirent
*dep
, *nde
;
522 struct hpfs_dirent de
;
525 struct buffer_head
*bh
;
529 if (flags
& ~RENAME_NOREPLACE
)
532 if ((err
= hpfs_chk_name(new_name
, &new_len
))) return err
;
534 hpfs_adjust_length(old_name
, &old_len
);
537 /* order doesn't matter, due to VFS exclusion */
539 /* Erm? Moving over the empty non-busy directory is perfectly legal */
540 if (new_inode
&& S_ISDIR(new_inode
->i_mode
)) {
545 if (!(dep
= map_dirent(old_dir
, hpfs_i(old_dir
)->i_dno
, old_name
, old_len
, &dno
, &qbh
))) {
546 hpfs_error(i
->i_sb
, "lookup succeeded but map dirent failed");
551 de
.hidden
= new_name
[0] == '.';
555 if ((r
= hpfs_remove_dirent(old_dir
, dno
, dep
, &qbh
, 1)) != 2) {
556 if ((nde
= map_dirent(new_dir
, hpfs_i(new_dir
)->i_dno
, new_name
, new_len
, NULL
, &qbh1
))) {
557 clear_nlink(new_inode
);
559 memcpy(nde
->name
, new_name
, new_len
);
560 hpfs_mark_4buffers_dirty(&qbh1
);
564 hpfs_error(new_dir
->i_sb
, "hpfs_rename: could not find dirent");
568 err
= r
== 2 ? -ENOSPC
: r
== 1 ? -EFSERROR
: 0;
572 if (new_dir
== old_dir
) hpfs_brelse4(&qbh
);
574 if ((r
= hpfs_add_dirent(new_dir
, new_name
, new_len
, &de
))) {
575 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
);
581 if (new_dir
== old_dir
)
582 if (!(dep
= map_dirent(old_dir
, hpfs_i(old_dir
)->i_dno
, old_name
, old_len
, &dno
, &qbh
))) {
583 hpfs_error(i
->i_sb
, "lookup succeeded but map dirent failed at #2");
588 if ((r
= hpfs_remove_dirent(old_dir
, dno
, dep
, &qbh
, 0))) {
589 hpfs_error(i
->i_sb
, "hpfs_rename: could not remove dirent");
590 err
= r
== 2 ? -ENOSPC
: -EFSERROR
;
595 hpfs_i(i
)->i_parent_dir
= new_dir
->i_ino
;
596 if (S_ISDIR(i
->i_mode
)) {
600 if ((fnode
= hpfs_map_fnode(i
->i_sb
, i
->i_ino
, &bh
))) {
601 fnode
->up
= cpu_to_le32(new_dir
->i_ino
);
602 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
);
610 hpfs_update_directory_times(old_dir
);
611 hpfs_update_directory_times(new_dir
);
613 hpfs_unlock(i
->i_sb
);
617 const struct inode_operations hpfs_dir_iops
=
619 .create
= hpfs_create
,
620 .lookup
= hpfs_lookup
,
621 .unlink
= hpfs_unlink
,
622 .symlink
= hpfs_symlink
,
626 .rename
= hpfs_rename
,
627 .setattr
= hpfs_setattr
,