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>
11 static int hpfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
13 const unsigned char *name
= dentry
->d_name
.name
;
14 unsigned len
= dentry
->d_name
.len
;
15 struct quad_buffer_head qbh0
;
16 struct buffer_head
*bh
;
17 struct hpfs_dirent
*de
;
24 struct hpfs_dirent dee
;
26 if ((err
= hpfs_chk_name(name
, &len
))) return err
==-ENOENT
? -EINVAL
: err
;
29 fnode
= hpfs_alloc_fnode(dir
->i_sb
, hpfs_i(dir
)->i_dno
, &fno
, &bh
);
32 dnode
= hpfs_alloc_dnode(dir
->i_sb
, fno
, &dno
, &qbh0
, 1);
35 memset(&dee
, 0, sizeof dee
);
37 if (!(mode
& 0222)) dee
.read_only
= 1;
39 dee
.hidden
= name
[0] == '.';
41 dee
.creation_date
= dee
.write_date
= dee
.read_date
= gmt_to_local(dir
->i_sb
, get_seconds());
42 result
= new_inode(dir
->i_sb
);
45 hpfs_init_inode(result
);
47 hpfs_i(result
)->i_parent_dir
= dir
->i_ino
;
48 hpfs_i(result
)->i_dno
= dno
;
49 result
->i_ctime
.tv_sec
= result
->i_mtime
.tv_sec
= result
->i_atime
.tv_sec
= local_to_gmt(dir
->i_sb
, dee
.creation_date
);
50 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
;
58 result
->i_size
= 2048;
61 result
->i_mode
&= ~0222;
63 mutex_lock(&hpfs_i(dir
)->i_mutex
);
64 r
= hpfs_add_dirent(dir
, name
, len
, &dee
, 0);
72 memcpy(fnode
->name
, name
, len
> 15 ? 15 : len
);
73 fnode
->up
= dir
->i_ino
;
75 fnode
->btree
.n_free_nodes
= 7;
76 fnode
->btree
.n_used_nodes
= 1;
77 fnode
->btree
.first_free
= 0x14;
78 fnode
->u
.external
[0].disk_secno
= dno
;
79 fnode
->u
.external
[0].file_secno
= -1;
80 dnode
->root_dnode
= 1;
82 de
= hpfs_add_de(dir
->i_sb
, dnode
, "\001\001", 2, 0);
83 de
->creation_date
= de
->write_date
= de
->read_date
= gmt_to_local(dir
->i_sb
, get_seconds());
84 if (!(mode
& 0222)) de
->read_only
= 1;
85 de
->first
= de
->directory
= 1;
86 /*de->hidden = de->system = 0;*/
88 mark_buffer_dirty(bh
);
90 hpfs_mark_4buffers_dirty(&qbh0
);
93 insert_inode_hash(result
);
95 if (result
->i_uid
!= current_fsuid() ||
96 result
->i_gid
!= current_fsgid() ||
97 result
->i_mode
!= (mode
| S_IFDIR
)) {
98 result
->i_uid
= current_fsuid();
99 result
->i_gid
= current_fsgid();
100 result
->i_mode
= mode
| S_IFDIR
;
101 hpfs_write_inode_nolock(result
);
103 d_instantiate(dentry
, result
);
104 mutex_unlock(&hpfs_i(dir
)->i_mutex
);
105 hpfs_unlock(dir
->i_sb
);
108 mutex_unlock(&hpfs_i(dir
)->i_mutex
);
112 hpfs_free_dnode(dir
->i_sb
, dno
);
115 hpfs_free_sectors(dir
->i_sb
, fno
, 1);
117 hpfs_unlock(dir
->i_sb
);
121 static int hpfs_create(struct inode
*dir
, struct dentry
*dentry
, int mode
, struct nameidata
*nd
)
123 const unsigned char *name
= dentry
->d_name
.name
;
124 unsigned len
= dentry
->d_name
.len
;
125 struct inode
*result
= NULL
;
126 struct buffer_head
*bh
;
130 struct hpfs_dirent dee
;
132 if ((err
= hpfs_chk_name(name
, &len
)))
133 return err
==-ENOENT
? -EINVAL
: err
;
134 hpfs_lock(dir
->i_sb
);
136 fnode
= hpfs_alloc_fnode(dir
->i_sb
, hpfs_i(dir
)->i_dno
, &fno
, &bh
);
139 memset(&dee
, 0, sizeof dee
);
140 if (!(mode
& 0222)) dee
.read_only
= 1;
142 dee
.hidden
= name
[0] == '.';
144 dee
.creation_date
= dee
.write_date
= dee
.read_date
= gmt_to_local(dir
->i_sb
, get_seconds());
146 result
= new_inode(dir
->i_sb
);
150 hpfs_init_inode(result
);
152 result
->i_mode
|= S_IFREG
;
153 result
->i_mode
&= ~0111;
154 result
->i_op
= &hpfs_file_iops
;
155 result
->i_fop
= &hpfs_file_ops
;
157 hpfs_decide_conv(result
, name
, len
);
158 hpfs_i(result
)->i_parent_dir
= dir
->i_ino
;
159 result
->i_ctime
.tv_sec
= result
->i_mtime
.tv_sec
= result
->i_atime
.tv_sec
= local_to_gmt(dir
->i_sb
, dee
.creation_date
);
160 result
->i_ctime
.tv_nsec
= 0;
161 result
->i_mtime
.tv_nsec
= 0;
162 result
->i_atime
.tv_nsec
= 0;
163 hpfs_i(result
)->i_ea_size
= 0;
165 result
->i_mode
&= ~0222;
166 result
->i_blocks
= 1;
168 result
->i_data
.a_ops
= &hpfs_aops
;
169 hpfs_i(result
)->mmu_private
= 0;
171 mutex_lock(&hpfs_i(dir
)->i_mutex
);
172 r
= hpfs_add_dirent(dir
, name
, len
, &dee
, 0);
180 memcpy(fnode
->name
, name
, len
> 15 ? 15 : len
);
181 fnode
->up
= dir
->i_ino
;
182 mark_buffer_dirty(bh
);
185 insert_inode_hash(result
);
187 if (result
->i_uid
!= current_fsuid() ||
188 result
->i_gid
!= current_fsgid() ||
189 result
->i_mode
!= (mode
| S_IFREG
)) {
190 result
->i_uid
= current_fsuid();
191 result
->i_gid
= current_fsgid();
192 result
->i_mode
= mode
| S_IFREG
;
193 hpfs_write_inode_nolock(result
);
195 d_instantiate(dentry
, result
);
196 mutex_unlock(&hpfs_i(dir
)->i_mutex
);
197 hpfs_unlock(dir
->i_sb
);
201 mutex_unlock(&hpfs_i(dir
)->i_mutex
);
205 hpfs_free_sectors(dir
->i_sb
, fno
, 1);
207 hpfs_unlock(dir
->i_sb
);
211 static int hpfs_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
, dev_t rdev
)
213 const unsigned char *name
= dentry
->d_name
.name
;
214 unsigned len
= dentry
->d_name
.len
;
215 struct buffer_head
*bh
;
219 struct hpfs_dirent dee
;
220 struct inode
*result
= NULL
;
222 if ((err
= hpfs_chk_name(name
, &len
))) return err
==-ENOENT
? -EINVAL
: err
;
223 if (hpfs_sb(dir
->i_sb
)->sb_eas
< 2) return -EPERM
;
224 if (!new_valid_dev(rdev
))
226 hpfs_lock(dir
->i_sb
);
228 fnode
= hpfs_alloc_fnode(dir
->i_sb
, hpfs_i(dir
)->i_dno
, &fno
, &bh
);
231 memset(&dee
, 0, sizeof dee
);
232 if (!(mode
& 0222)) dee
.read_only
= 1;
234 dee
.hidden
= name
[0] == '.';
236 dee
.creation_date
= dee
.write_date
= dee
.read_date
= gmt_to_local(dir
->i_sb
, get_seconds());
238 result
= new_inode(dir
->i_sb
);
242 hpfs_init_inode(result
);
244 hpfs_i(result
)->i_parent_dir
= dir
->i_ino
;
245 result
->i_ctime
.tv_sec
= result
->i_mtime
.tv_sec
= result
->i_atime
.tv_sec
= local_to_gmt(dir
->i_sb
, dee
.creation_date
);
246 result
->i_ctime
.tv_nsec
= 0;
247 result
->i_mtime
.tv_nsec
= 0;
248 result
->i_atime
.tv_nsec
= 0;
249 hpfs_i(result
)->i_ea_size
= 0;
250 result
->i_uid
= current_fsuid();
251 result
->i_gid
= current_fsgid();
254 result
->i_blocks
= 1;
255 init_special_inode(result
, mode
, rdev
);
257 mutex_lock(&hpfs_i(dir
)->i_mutex
);
258 r
= hpfs_add_dirent(dir
, name
, len
, &dee
, 0);
266 memcpy(fnode
->name
, name
, len
> 15 ? 15 : len
);
267 fnode
->up
= dir
->i_ino
;
268 mark_buffer_dirty(bh
);
270 insert_inode_hash(result
);
272 hpfs_write_inode_nolock(result
);
273 d_instantiate(dentry
, result
);
274 mutex_unlock(&hpfs_i(dir
)->i_mutex
);
276 hpfs_unlock(dir
->i_sb
);
279 mutex_unlock(&hpfs_i(dir
)->i_mutex
);
283 hpfs_free_sectors(dir
->i_sb
, fno
, 1);
285 hpfs_unlock(dir
->i_sb
);
289 static int hpfs_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *symlink
)
291 const unsigned char *name
= dentry
->d_name
.name
;
292 unsigned len
= dentry
->d_name
.len
;
293 struct buffer_head
*bh
;
297 struct hpfs_dirent dee
;
298 struct inode
*result
;
300 if ((err
= hpfs_chk_name(name
, &len
))) return err
==-ENOENT
? -EINVAL
: err
;
301 hpfs_lock(dir
->i_sb
);
302 if (hpfs_sb(dir
->i_sb
)->sb_eas
< 2) {
303 hpfs_unlock(dir
->i_sb
);
307 fnode
= hpfs_alloc_fnode(dir
->i_sb
, hpfs_i(dir
)->i_dno
, &fno
, &bh
);
310 memset(&dee
, 0, sizeof dee
);
312 dee
.hidden
= name
[0] == '.';
314 dee
.creation_date
= dee
.write_date
= dee
.read_date
= gmt_to_local(dir
->i_sb
, get_seconds());
316 result
= new_inode(dir
->i_sb
);
320 hpfs_init_inode(result
);
321 hpfs_i(result
)->i_parent_dir
= dir
->i_ino
;
322 result
->i_ctime
.tv_sec
= result
->i_mtime
.tv_sec
= result
->i_atime
.tv_sec
= local_to_gmt(dir
->i_sb
, dee
.creation_date
);
323 result
->i_ctime
.tv_nsec
= 0;
324 result
->i_mtime
.tv_nsec
= 0;
325 result
->i_atime
.tv_nsec
= 0;
326 hpfs_i(result
)->i_ea_size
= 0;
327 result
->i_mode
= S_IFLNK
| 0777;
328 result
->i_uid
= current_fsuid();
329 result
->i_gid
= current_fsgid();
330 result
->i_blocks
= 1;
332 result
->i_size
= strlen(symlink
);
333 result
->i_op
= &page_symlink_inode_operations
;
334 result
->i_data
.a_ops
= &hpfs_symlink_aops
;
336 mutex_lock(&hpfs_i(dir
)->i_mutex
);
337 r
= hpfs_add_dirent(dir
, name
, len
, &dee
, 0);
345 memcpy(fnode
->name
, name
, len
> 15 ? 15 : len
);
346 fnode
->up
= dir
->i_ino
;
347 hpfs_set_ea(result
, fnode
, "SYMLINK", symlink
, strlen(symlink
));
348 mark_buffer_dirty(bh
);
351 insert_inode_hash(result
);
353 hpfs_write_inode_nolock(result
);
354 d_instantiate(dentry
, result
);
355 mutex_unlock(&hpfs_i(dir
)->i_mutex
);
356 hpfs_unlock(dir
->i_sb
);
359 mutex_unlock(&hpfs_i(dir
)->i_mutex
);
363 hpfs_free_sectors(dir
->i_sb
, fno
, 1);
365 hpfs_unlock(dir
->i_sb
);
369 static int hpfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
371 const unsigned char *name
= dentry
->d_name
.name
;
372 unsigned len
= dentry
->d_name
.len
;
373 struct quad_buffer_head qbh
;
374 struct hpfs_dirent
*de
;
375 struct inode
*inode
= dentry
->d_inode
;
382 hpfs_lock(dir
->i_sb
);
383 hpfs_adjust_length(name
, &len
);
385 mutex_lock(&hpfs_i(inode
)->i_parent_mutex
);
386 mutex_lock(&hpfs_i(dir
)->i_mutex
);
388 de
= map_dirent(dir
, hpfs_i(dir
)->i_dno
, name
, len
, &dno
, &qbh
);
401 r
= hpfs_remove_dirent(dir
, dno
, de
, &qbh
, 1);
404 hpfs_error(dir
->i_sb
, "there was error when removing dirent");
407 case 2: /* no space for deleting, try to truncate file */
413 mutex_unlock(&hpfs_i(dir
)->i_mutex
);
414 mutex_unlock(&hpfs_i(inode
)->i_parent_mutex
);
415 dentry_unhash(dentry
);
416 if (!d_unhashed(dentry
)) {
418 hpfs_unlock(dir
->i_sb
);
421 if (generic_permission(inode
, MAY_WRITE
, 0, NULL
) ||
422 !S_ISREG(inode
->i_mode
) ||
423 get_write_access(inode
)) {
427 struct iattr newattrs
;
428 /*printk("HPFS: truncating file before delete.\n");*/
429 newattrs
.ia_size
= 0;
430 newattrs
.ia_valid
= ATTR_SIZE
| ATTR_CTIME
;
431 err
= notify_change(dentry
, &newattrs
);
432 put_write_access(inode
);
437 hpfs_unlock(dir
->i_sb
);
448 mutex_unlock(&hpfs_i(dir
)->i_mutex
);
449 mutex_unlock(&hpfs_i(inode
)->i_parent_mutex
);
450 hpfs_unlock(dir
->i_sb
);
454 static int hpfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
456 const unsigned char *name
= dentry
->d_name
.name
;
457 unsigned len
= dentry
->d_name
.len
;
458 struct quad_buffer_head qbh
;
459 struct hpfs_dirent
*de
;
460 struct inode
*inode
= dentry
->d_inode
;
467 hpfs_adjust_length(name
, &len
);
468 hpfs_lock(dir
->i_sb
);
469 mutex_lock(&hpfs_i(inode
)->i_parent_mutex
);
470 mutex_lock(&hpfs_i(dir
)->i_mutex
);
472 de
= map_dirent(dir
, hpfs_i(dir
)->i_dno
, name
, len
, &dno
, &qbh
);
484 hpfs_count_dnodes(dir
->i_sb
, hpfs_i(inode
)->i_dno
, NULL
, NULL
, &n_items
);
490 r
= hpfs_remove_dirent(dir
, dno
, de
, &qbh
, 1);
493 hpfs_error(dir
->i_sb
, "there was error when removing dirent");
508 mutex_unlock(&hpfs_i(dir
)->i_mutex
);
509 mutex_unlock(&hpfs_i(inode
)->i_parent_mutex
);
510 hpfs_unlock(dir
->i_sb
);
514 static int hpfs_symlink_readpage(struct file
*file
, struct page
*page
)
516 char *link
= kmap(page
);
517 struct inode
*i
= page
->mapping
->host
;
519 struct buffer_head
*bh
;
524 if (!(fnode
= hpfs_map_fnode(i
->i_sb
, i
->i_ino
, &bh
)))
526 err
= hpfs_read_ea(i
->i_sb
, fnode
, "SYMLINK", link
, PAGE_SIZE
);
530 hpfs_unlock(i
->i_sb
);
531 SetPageUptodate(page
);
537 hpfs_unlock(i
->i_sb
);
544 const struct address_space_operations hpfs_symlink_aops
= {
545 .readpage
= hpfs_symlink_readpage
548 static int hpfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
549 struct inode
*new_dir
, struct dentry
*new_dentry
)
551 const unsigned char *old_name
= old_dentry
->d_name
.name
;
552 unsigned old_len
= old_dentry
->d_name
.len
;
553 const unsigned char *new_name
= new_dentry
->d_name
.name
;
554 unsigned new_len
= new_dentry
->d_name
.len
;
555 struct inode
*i
= old_dentry
->d_inode
;
556 struct inode
*new_inode
= new_dentry
->d_inode
;
557 struct quad_buffer_head qbh
, qbh1
;
558 struct hpfs_dirent
*dep
, *nde
;
559 struct hpfs_dirent de
;
562 struct buffer_head
*bh
;
565 if ((err
= hpfs_chk_name(new_name
, &new_len
))) return err
;
567 hpfs_adjust_length(old_name
, &old_len
);
570 /* order doesn't matter, due to VFS exclusion */
571 mutex_lock(&hpfs_i(i
)->i_parent_mutex
);
573 mutex_lock(&hpfs_i(new_inode
)->i_parent_mutex
);
574 mutex_lock(&hpfs_i(old_dir
)->i_mutex
);
575 if (new_dir
!= old_dir
)
576 mutex_lock(&hpfs_i(new_dir
)->i_mutex
);
578 /* Erm? Moving over the empty non-busy directory is perfectly legal */
579 if (new_inode
&& S_ISDIR(new_inode
->i_mode
)) {
584 if (!(dep
= map_dirent(old_dir
, hpfs_i(old_dir
)->i_dno
, old_name
, old_len
, &dno
, &qbh
))) {
585 hpfs_error(i
->i_sb
, "lookup succeeded but map dirent failed");
590 de
.hidden
= new_name
[0] == '.';
594 if ((r
= hpfs_remove_dirent(old_dir
, dno
, dep
, &qbh
, 1)) != 2) {
595 if ((nde
= map_dirent(new_dir
, hpfs_i(new_dir
)->i_dno
, new_name
, new_len
, NULL
, &qbh1
))) {
596 clear_nlink(new_inode
);
598 memcpy(nde
->name
, new_name
, new_len
);
599 hpfs_mark_4buffers_dirty(&qbh1
);
603 hpfs_error(new_dir
->i_sb
, "hpfs_rename: could not find dirent");
607 err
= r
== 2 ? -ENOSPC
: r
== 1 ? -EFSERROR
: 0;
611 if (new_dir
== old_dir
) hpfs_brelse4(&qbh
);
613 hpfs_lock_creation(i
->i_sb
);
614 if ((r
= hpfs_add_dirent(new_dir
, new_name
, new_len
, &de
, 1))) {
615 hpfs_unlock_creation(i
->i_sb
);
616 if (r
== -1) hpfs_error(new_dir
->i_sb
, "hpfs_rename: dirent already exists!");
617 err
= r
== 1 ? -ENOSPC
: -EFSERROR
;
618 if (new_dir
!= old_dir
) hpfs_brelse4(&qbh
);
622 if (new_dir
== old_dir
)
623 if (!(dep
= map_dirent(old_dir
, hpfs_i(old_dir
)->i_dno
, old_name
, old_len
, &dno
, &qbh
))) {
624 hpfs_unlock_creation(i
->i_sb
);
625 hpfs_error(i
->i_sb
, "lookup succeeded but map dirent failed at #2");
630 if ((r
= hpfs_remove_dirent(old_dir
, dno
, dep
, &qbh
, 0))) {
631 hpfs_unlock_creation(i
->i_sb
);
632 hpfs_error(i
->i_sb
, "hpfs_rename: could not remove dirent");
633 err
= r
== 2 ? -ENOSPC
: -EFSERROR
;
636 hpfs_unlock_creation(i
->i_sb
);
639 hpfs_i(i
)->i_parent_dir
= new_dir
->i_ino
;
640 if (S_ISDIR(i
->i_mode
)) {
644 if ((fnode
= hpfs_map_fnode(i
->i_sb
, i
->i_ino
, &bh
))) {
645 fnode
->up
= new_dir
->i_ino
;
646 fnode
->len
= new_len
;
647 memcpy(fnode
->name
, new_name
, new_len
>15?15:new_len
);
648 if (new_len
< 15) memset(&fnode
->name
[new_len
], 0, 15 - new_len
);
649 mark_buffer_dirty(bh
);
652 hpfs_i(i
)->i_conv
= hpfs_sb(i
->i_sb
)->sb_conv
;
653 hpfs_decide_conv(i
, new_name
, new_len
);
655 if (old_dir
!= new_dir
)
656 mutex_unlock(&hpfs_i(new_dir
)->i_mutex
);
657 mutex_unlock(&hpfs_i(old_dir
)->i_mutex
);
658 mutex_unlock(&hpfs_i(i
)->i_parent_mutex
);
660 mutex_unlock(&hpfs_i(new_inode
)->i_parent_mutex
);
661 hpfs_unlock(i
->i_sb
);
665 const struct inode_operations hpfs_dir_iops
=
667 .create
= hpfs_create
,
668 .lookup
= hpfs_lookup
,
669 .unlink
= hpfs_unlink
,
670 .symlink
= hpfs_symlink
,
674 .rename
= hpfs_rename
,
675 .setattr
= hpfs_setattr
,