2 * linux/fs/hpfs/namei.c
4 * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
6 * adding & removing files & directories
8 #include <linux/sched.h>
9 #include <linux/smp_lock.h>
12 static int hpfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
14 const unsigned char *name
= dentry
->d_name
.name
;
15 unsigned len
= dentry
->d_name
.len
;
16 struct quad_buffer_head qbh0
;
17 struct buffer_head
*bh
;
18 struct hpfs_dirent
*de
;
25 struct hpfs_dirent dee
;
27 if ((err
= hpfs_chk_name(name
, &len
))) return err
==-ENOENT
? -EINVAL
: err
;
30 fnode
= hpfs_alloc_fnode(dir
->i_sb
, hpfs_i(dir
)->i_dno
, &fno
, &bh
);
33 dnode
= hpfs_alloc_dnode(dir
->i_sb
, fno
, &dno
, &qbh0
, 1);
36 memset(&dee
, 0, sizeof dee
);
38 if (!(mode
& 0222)) dee
.read_only
= 1;
40 dee
.hidden
= name
[0] == '.';
42 dee
.creation_date
= dee
.write_date
= dee
.read_date
= gmt_to_local(dir
->i_sb
, get_seconds());
43 result
= new_inode(dir
->i_sb
);
46 hpfs_init_inode(result
);
48 hpfs_i(result
)->i_parent_dir
= dir
->i_ino
;
49 hpfs_i(result
)->i_dno
= dno
;
50 result
->i_ctime
.tv_sec
= result
->i_mtime
.tv_sec
= result
->i_atime
.tv_sec
= local_to_gmt(dir
->i_sb
, dee
.creation_date
);
51 result
->i_ctime
.tv_nsec
= 0;
52 result
->i_mtime
.tv_nsec
= 0;
53 result
->i_atime
.tv_nsec
= 0;
54 hpfs_i(result
)->i_ea_size
= 0;
55 result
->i_mode
|= S_IFDIR
;
56 result
->i_op
= &hpfs_dir_iops
;
57 result
->i_fop
= &hpfs_dir_ops
;
59 result
->i_size
= 2048;
62 result
->i_mode
&= ~0222;
64 mutex_lock(&hpfs_i(dir
)->i_mutex
);
65 r
= hpfs_add_dirent(dir
, name
, len
, &dee
, 0);
73 memcpy(fnode
->name
, name
, len
> 15 ? 15 : len
);
74 fnode
->up
= dir
->i_ino
;
76 fnode
->btree
.n_free_nodes
= 7;
77 fnode
->btree
.n_used_nodes
= 1;
78 fnode
->btree
.first_free
= 0x14;
79 fnode
->u
.external
[0].disk_secno
= dno
;
80 fnode
->u
.external
[0].file_secno
= -1;
81 dnode
->root_dnode
= 1;
83 de
= hpfs_add_de(dir
->i_sb
, dnode
, "\001\001", 2, 0);
84 de
->creation_date
= de
->write_date
= de
->read_date
= gmt_to_local(dir
->i_sb
, get_seconds());
85 if (!(mode
& 0222)) de
->read_only
= 1;
86 de
->first
= de
->directory
= 1;
87 /*de->hidden = de->system = 0;*/
89 mark_buffer_dirty(bh
);
91 hpfs_mark_4buffers_dirty(&qbh0
);
94 insert_inode_hash(result
);
96 if (result
->i_uid
!= current_fsuid() ||
97 result
->i_gid
!= current_fsgid() ||
98 result
->i_mode
!= (mode
| S_IFDIR
)) {
99 result
->i_uid
= current_fsuid();
100 result
->i_gid
= current_fsgid();
101 result
->i_mode
= mode
| S_IFDIR
;
102 hpfs_write_inode_nolock(result
);
104 d_instantiate(dentry
, result
);
105 mutex_unlock(&hpfs_i(dir
)->i_mutex
);
109 mutex_unlock(&hpfs_i(dir
)->i_mutex
);
113 hpfs_free_dnode(dir
->i_sb
, dno
);
116 hpfs_free_sectors(dir
->i_sb
, fno
, 1);
122 static int hpfs_create(struct inode
*dir
, struct dentry
*dentry
, int mode
, struct nameidata
*nd
)
124 const unsigned char *name
= dentry
->d_name
.name
;
125 unsigned len
= dentry
->d_name
.len
;
126 struct inode
*result
= NULL
;
127 struct buffer_head
*bh
;
131 struct hpfs_dirent dee
;
133 if ((err
= hpfs_chk_name(name
, &len
)))
134 return err
==-ENOENT
? -EINVAL
: err
;
137 fnode
= hpfs_alloc_fnode(dir
->i_sb
, hpfs_i(dir
)->i_dno
, &fno
, &bh
);
140 memset(&dee
, 0, sizeof dee
);
141 if (!(mode
& 0222)) dee
.read_only
= 1;
143 dee
.hidden
= name
[0] == '.';
145 dee
.creation_date
= dee
.write_date
= dee
.read_date
= gmt_to_local(dir
->i_sb
, get_seconds());
147 result
= new_inode(dir
->i_sb
);
151 hpfs_init_inode(result
);
153 result
->i_mode
|= S_IFREG
;
154 result
->i_mode
&= ~0111;
155 result
->i_op
= &hpfs_file_iops
;
156 result
->i_fop
= &hpfs_file_ops
;
158 hpfs_decide_conv(result
, name
, len
);
159 hpfs_i(result
)->i_parent_dir
= dir
->i_ino
;
160 result
->i_ctime
.tv_sec
= result
->i_mtime
.tv_sec
= result
->i_atime
.tv_sec
= local_to_gmt(dir
->i_sb
, dee
.creation_date
);
161 result
->i_ctime
.tv_nsec
= 0;
162 result
->i_mtime
.tv_nsec
= 0;
163 result
->i_atime
.tv_nsec
= 0;
164 hpfs_i(result
)->i_ea_size
= 0;
166 result
->i_mode
&= ~0222;
167 result
->i_blocks
= 1;
169 result
->i_data
.a_ops
= &hpfs_aops
;
170 hpfs_i(result
)->mmu_private
= 0;
172 mutex_lock(&hpfs_i(dir
)->i_mutex
);
173 r
= hpfs_add_dirent(dir
, name
, len
, &dee
, 0);
181 memcpy(fnode
->name
, name
, len
> 15 ? 15 : len
);
182 fnode
->up
= dir
->i_ino
;
183 mark_buffer_dirty(bh
);
186 insert_inode_hash(result
);
188 if (result
->i_uid
!= current_fsuid() ||
189 result
->i_gid
!= current_fsgid() ||
190 result
->i_mode
!= (mode
| S_IFREG
)) {
191 result
->i_uid
= current_fsuid();
192 result
->i_gid
= current_fsgid();
193 result
->i_mode
= mode
| S_IFREG
;
194 hpfs_write_inode_nolock(result
);
196 d_instantiate(dentry
, result
);
197 mutex_unlock(&hpfs_i(dir
)->i_mutex
);
202 mutex_unlock(&hpfs_i(dir
)->i_mutex
);
206 hpfs_free_sectors(dir
->i_sb
, fno
, 1);
212 static int hpfs_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
, dev_t rdev
)
214 const unsigned char *name
= dentry
->d_name
.name
;
215 unsigned len
= dentry
->d_name
.len
;
216 struct buffer_head
*bh
;
220 struct hpfs_dirent dee
;
221 struct inode
*result
= NULL
;
223 if ((err
= hpfs_chk_name(name
, &len
))) return err
==-ENOENT
? -EINVAL
: err
;
224 if (hpfs_sb(dir
->i_sb
)->sb_eas
< 2) return -EPERM
;
225 if (!new_valid_dev(rdev
))
229 fnode
= hpfs_alloc_fnode(dir
->i_sb
, hpfs_i(dir
)->i_dno
, &fno
, &bh
);
232 memset(&dee
, 0, sizeof dee
);
233 if (!(mode
& 0222)) dee
.read_only
= 1;
235 dee
.hidden
= name
[0] == '.';
237 dee
.creation_date
= dee
.write_date
= dee
.read_date
= gmt_to_local(dir
->i_sb
, get_seconds());
239 result
= new_inode(dir
->i_sb
);
243 hpfs_init_inode(result
);
245 hpfs_i(result
)->i_parent_dir
= dir
->i_ino
;
246 result
->i_ctime
.tv_sec
= result
->i_mtime
.tv_sec
= result
->i_atime
.tv_sec
= local_to_gmt(dir
->i_sb
, dee
.creation_date
);
247 result
->i_ctime
.tv_nsec
= 0;
248 result
->i_mtime
.tv_nsec
= 0;
249 result
->i_atime
.tv_nsec
= 0;
250 hpfs_i(result
)->i_ea_size
= 0;
251 result
->i_uid
= current_fsuid();
252 result
->i_gid
= current_fsgid();
255 result
->i_blocks
= 1;
256 init_special_inode(result
, mode
, rdev
);
258 mutex_lock(&hpfs_i(dir
)->i_mutex
);
259 r
= hpfs_add_dirent(dir
, name
, len
, &dee
, 0);
267 memcpy(fnode
->name
, name
, len
> 15 ? 15 : len
);
268 fnode
->up
= dir
->i_ino
;
269 mark_buffer_dirty(bh
);
271 insert_inode_hash(result
);
273 hpfs_write_inode_nolock(result
);
274 d_instantiate(dentry
, result
);
275 mutex_unlock(&hpfs_i(dir
)->i_mutex
);
280 mutex_unlock(&hpfs_i(dir
)->i_mutex
);
284 hpfs_free_sectors(dir
->i_sb
, fno
, 1);
290 static int hpfs_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *symlink
)
292 const unsigned char *name
= dentry
->d_name
.name
;
293 unsigned len
= dentry
->d_name
.len
;
294 struct buffer_head
*bh
;
298 struct hpfs_dirent dee
;
299 struct inode
*result
;
301 if ((err
= hpfs_chk_name(name
, &len
))) return err
==-ENOENT
? -EINVAL
: err
;
303 if (hpfs_sb(dir
->i_sb
)->sb_eas
< 2) {
308 fnode
= hpfs_alloc_fnode(dir
->i_sb
, hpfs_i(dir
)->i_dno
, &fno
, &bh
);
311 memset(&dee
, 0, sizeof dee
);
313 dee
.hidden
= name
[0] == '.';
315 dee
.creation_date
= dee
.write_date
= dee
.read_date
= gmt_to_local(dir
->i_sb
, get_seconds());
317 result
= new_inode(dir
->i_sb
);
321 hpfs_init_inode(result
);
322 hpfs_i(result
)->i_parent_dir
= dir
->i_ino
;
323 result
->i_ctime
.tv_sec
= result
->i_mtime
.tv_sec
= result
->i_atime
.tv_sec
= local_to_gmt(dir
->i_sb
, dee
.creation_date
);
324 result
->i_ctime
.tv_nsec
= 0;
325 result
->i_mtime
.tv_nsec
= 0;
326 result
->i_atime
.tv_nsec
= 0;
327 hpfs_i(result
)->i_ea_size
= 0;
328 result
->i_mode
= S_IFLNK
| 0777;
329 result
->i_uid
= current_fsuid();
330 result
->i_gid
= current_fsgid();
331 result
->i_blocks
= 1;
333 result
->i_size
= strlen(symlink
);
334 result
->i_op
= &page_symlink_inode_operations
;
335 result
->i_data
.a_ops
= &hpfs_symlink_aops
;
337 mutex_lock(&hpfs_i(dir
)->i_mutex
);
338 r
= hpfs_add_dirent(dir
, name
, len
, &dee
, 0);
346 memcpy(fnode
->name
, name
, len
> 15 ? 15 : len
);
347 fnode
->up
= dir
->i_ino
;
348 hpfs_set_ea(result
, fnode
, "SYMLINK", symlink
, strlen(symlink
));
349 mark_buffer_dirty(bh
);
352 insert_inode_hash(result
);
354 hpfs_write_inode_nolock(result
);
355 d_instantiate(dentry
, result
);
356 mutex_unlock(&hpfs_i(dir
)->i_mutex
);
360 mutex_unlock(&hpfs_i(dir
)->i_mutex
);
364 hpfs_free_sectors(dir
->i_sb
, fno
, 1);
370 static int hpfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
372 const unsigned char *name
= dentry
->d_name
.name
;
373 unsigned len
= dentry
->d_name
.len
;
374 struct quad_buffer_head qbh
;
375 struct hpfs_dirent
*de
;
376 struct inode
*inode
= dentry
->d_inode
;
384 hpfs_adjust_length(name
, &len
);
386 mutex_lock(&hpfs_i(inode
)->i_parent_mutex
);
387 mutex_lock(&hpfs_i(dir
)->i_mutex
);
389 de
= map_dirent(dir
, hpfs_i(dir
)->i_dno
, name
, len
, &dno
, &qbh
);
402 r
= hpfs_remove_dirent(dir
, dno
, de
, &qbh
, 1);
405 hpfs_error(dir
->i_sb
, "there was error when removing dirent");
408 case 2: /* no space for deleting, try to truncate file */
414 mutex_unlock(&hpfs_i(dir
)->i_mutex
);
415 mutex_unlock(&hpfs_i(inode
)->i_parent_mutex
);
416 dentry_unhash(dentry
);
417 if (!d_unhashed(dentry
)) {
422 if (generic_permission(inode
, MAY_WRITE
, 0, NULL
) ||
423 !S_ISREG(inode
->i_mode
) ||
424 get_write_access(inode
)) {
428 struct iattr newattrs
;
429 /*printk("HPFS: truncating file before delete.\n");*/
430 newattrs
.ia_size
= 0;
431 newattrs
.ia_valid
= ATTR_SIZE
| ATTR_CTIME
;
432 err
= notify_change(dentry
, &newattrs
);
433 put_write_access(inode
);
449 mutex_unlock(&hpfs_i(dir
)->i_mutex
);
450 mutex_unlock(&hpfs_i(inode
)->i_parent_mutex
);
455 static int hpfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
457 const unsigned char *name
= dentry
->d_name
.name
;
458 unsigned len
= dentry
->d_name
.len
;
459 struct quad_buffer_head qbh
;
460 struct hpfs_dirent
*de
;
461 struct inode
*inode
= dentry
->d_inode
;
468 hpfs_adjust_length(name
, &len
);
470 mutex_lock(&hpfs_i(inode
)->i_parent_mutex
);
471 mutex_lock(&hpfs_i(dir
)->i_mutex
);
473 de
= map_dirent(dir
, hpfs_i(dir
)->i_dno
, name
, len
, &dno
, &qbh
);
485 hpfs_count_dnodes(dir
->i_sb
, hpfs_i(inode
)->i_dno
, NULL
, NULL
, &n_items
);
491 r
= hpfs_remove_dirent(dir
, dno
, de
, &qbh
, 1);
494 hpfs_error(dir
->i_sb
, "there was error when removing dirent");
509 mutex_unlock(&hpfs_i(dir
)->i_mutex
);
510 mutex_unlock(&hpfs_i(inode
)->i_parent_mutex
);
515 static int hpfs_symlink_readpage(struct file
*file
, struct page
*page
)
517 char *link
= kmap(page
);
518 struct inode
*i
= page
->mapping
->host
;
520 struct buffer_head
*bh
;
525 if (!(fnode
= hpfs_map_fnode(i
->i_sb
, i
->i_ino
, &bh
)))
527 err
= hpfs_read_ea(i
->i_sb
, fnode
, "SYMLINK", link
, PAGE_SIZE
);
532 SetPageUptodate(page
);
545 const struct address_space_operations hpfs_symlink_aops
= {
546 .readpage
= hpfs_symlink_readpage
549 static int hpfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
550 struct inode
*new_dir
, struct dentry
*new_dentry
)
552 const unsigned char *old_name
= old_dentry
->d_name
.name
;
553 unsigned old_len
= old_dentry
->d_name
.len
;
554 const unsigned char *new_name
= new_dentry
->d_name
.name
;
555 unsigned new_len
= new_dentry
->d_name
.len
;
556 struct inode
*i
= old_dentry
->d_inode
;
557 struct inode
*new_inode
= new_dentry
->d_inode
;
558 struct quad_buffer_head qbh
, qbh1
;
559 struct hpfs_dirent
*dep
, *nde
;
560 struct hpfs_dirent de
;
563 struct buffer_head
*bh
;
566 if ((err
= hpfs_chk_name(new_name
, &new_len
))) return err
;
568 hpfs_adjust_length(old_name
, &old_len
);
571 /* order doesn't matter, due to VFS exclusion */
572 mutex_lock(&hpfs_i(i
)->i_parent_mutex
);
574 mutex_lock(&hpfs_i(new_inode
)->i_parent_mutex
);
575 mutex_lock(&hpfs_i(old_dir
)->i_mutex
);
576 if (new_dir
!= old_dir
)
577 mutex_lock(&hpfs_i(new_dir
)->i_mutex
);
579 /* Erm? Moving over the empty non-busy directory is perfectly legal */
580 if (new_inode
&& S_ISDIR(new_inode
->i_mode
)) {
585 if (!(dep
= map_dirent(old_dir
, hpfs_i(old_dir
)->i_dno
, old_name
, old_len
, &dno
, &qbh
))) {
586 hpfs_error(i
->i_sb
, "lookup succeeded but map dirent failed");
591 de
.hidden
= new_name
[0] == '.';
595 if ((r
= hpfs_remove_dirent(old_dir
, dno
, dep
, &qbh
, 1)) != 2) {
596 if ((nde
= map_dirent(new_dir
, hpfs_i(new_dir
)->i_dno
, new_name
, new_len
, NULL
, &qbh1
))) {
597 clear_nlink(new_inode
);
599 memcpy(nde
->name
, new_name
, new_len
);
600 hpfs_mark_4buffers_dirty(&qbh1
);
604 hpfs_error(new_dir
->i_sb
, "hpfs_rename: could not find dirent");
608 err
= r
== 2 ? -ENOSPC
: r
== 1 ? -EFSERROR
: 0;
612 if (new_dir
== old_dir
) hpfs_brelse4(&qbh
);
614 hpfs_lock_creation(i
->i_sb
);
615 if ((r
= hpfs_add_dirent(new_dir
, new_name
, new_len
, &de
, 1))) {
616 hpfs_unlock_creation(i
->i_sb
);
617 if (r
== -1) hpfs_error(new_dir
->i_sb
, "hpfs_rename: dirent already exists!");
618 err
= r
== 1 ? -ENOSPC
: -EFSERROR
;
619 if (new_dir
!= old_dir
) hpfs_brelse4(&qbh
);
623 if (new_dir
== old_dir
)
624 if (!(dep
= map_dirent(old_dir
, hpfs_i(old_dir
)->i_dno
, old_name
, old_len
, &dno
, &qbh
))) {
625 hpfs_unlock_creation(i
->i_sb
);
626 hpfs_error(i
->i_sb
, "lookup succeeded but map dirent failed at #2");
631 if ((r
= hpfs_remove_dirent(old_dir
, dno
, dep
, &qbh
, 0))) {
632 hpfs_unlock_creation(i
->i_sb
);
633 hpfs_error(i
->i_sb
, "hpfs_rename: could not remove dirent");
634 err
= r
== 2 ? -ENOSPC
: -EFSERROR
;
637 hpfs_unlock_creation(i
->i_sb
);
640 hpfs_i(i
)->i_parent_dir
= new_dir
->i_ino
;
641 if (S_ISDIR(i
->i_mode
)) {
645 if ((fnode
= hpfs_map_fnode(i
->i_sb
, i
->i_ino
, &bh
))) {
646 fnode
->up
= new_dir
->i_ino
;
647 fnode
->len
= new_len
;
648 memcpy(fnode
->name
, new_name
, new_len
>15?15:new_len
);
649 if (new_len
< 15) memset(&fnode
->name
[new_len
], 0, 15 - new_len
);
650 mark_buffer_dirty(bh
);
653 hpfs_i(i
)->i_conv
= hpfs_sb(i
->i_sb
)->sb_conv
;
654 hpfs_decide_conv(i
, new_name
, new_len
);
656 if (old_dir
!= new_dir
)
657 mutex_unlock(&hpfs_i(new_dir
)->i_mutex
);
658 mutex_unlock(&hpfs_i(old_dir
)->i_mutex
);
659 mutex_unlock(&hpfs_i(i
)->i_parent_mutex
);
661 mutex_unlock(&hpfs_i(new_inode
)->i_parent_mutex
);
666 const struct inode_operations hpfs_dir_iops
=
668 .create
= hpfs_create
,
669 .lookup
= hpfs_lookup
,
670 .unlink
= hpfs_unlink
,
671 .symlink
= hpfs_symlink
,
675 .rename
= hpfs_rename
,
676 .setattr
= hpfs_setattr
,