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 void hpfs_update_directory_times(struct inode
*dir
)
13 time_t t
= get_seconds();
14 if (t
== dir
->i_mtime
.tv_sec
&&
15 t
== dir
->i_ctime
.tv_sec
)
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
);
22 static int hpfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
24 const unsigned char *name
= dentry
->d_name
.name
;
25 unsigned len
= dentry
->d_name
.len
;
26 struct quad_buffer_head qbh0
;
27 struct buffer_head
*bh
;
28 struct hpfs_dirent
*de
;
35 struct hpfs_dirent dee
;
37 if ((err
= hpfs_chk_name(name
, &len
))) return err
==-ENOENT
? -EINVAL
: err
;
40 fnode
= hpfs_alloc_fnode(dir
->i_sb
, hpfs_i(dir
)->i_dno
, &fno
, &bh
);
43 dnode
= hpfs_alloc_dnode(dir
->i_sb
, fno
, &dno
, &qbh0
);
46 memset(&dee
, 0, sizeof dee
);
48 if (!(mode
& 0222)) dee
.read_only
= 1;
50 dee
.hidden
= name
[0] == '.';
51 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()));
53 result
= new_inode(dir
->i_sb
);
56 hpfs_init_inode(result
);
58 hpfs_i(result
)->i_parent_dir
= dir
->i_ino
;
59 hpfs_i(result
)->i_dno
= dno
;
60 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
));
61 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
;
69 result
->i_size
= 2048;
72 result
->i_mode
&= ~0222;
74 r
= hpfs_add_dirent(dir
, name
, len
, &dee
);
82 memcpy(fnode
->name
, name
, len
> 15 ? 15 : len
);
83 fnode
->up
= cpu_to_le32(dir
->i_ino
);
84 fnode
->flags
|= FNODE_dir
;
85 fnode
->btree
.n_free_nodes
= 7;
86 fnode
->btree
.n_used_nodes
= 1;
87 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);
90 dnode
->root_dnode
= 1;
91 dnode
->up
= cpu_to_le32(fno
);
92 de
= hpfs_add_de(dir
->i_sb
, dnode
, "\001\001", 2, 0);
93 de
->creation_date
= de
->write_date
= de
->read_date
= cpu_to_le32(gmt_to_local(dir
->i_sb
, get_seconds()));
94 if (!(mode
& 0222)) de
->read_only
= 1;
95 de
->first
= de
->directory
= 1;
96 /*de->hidden = de->system = 0;*/
97 de
->fnode
= cpu_to_le32(fno
);
98 mark_buffer_dirty(bh
);
100 hpfs_mark_4buffers_dirty(&qbh0
);
103 insert_inode_hash(result
);
105 if (!uid_eq(result
->i_uid
, current_fsuid()) ||
106 !gid_eq(result
->i_gid
, current_fsgid()) ||
107 result
->i_mode
!= (mode
| S_IFDIR
)) {
108 result
->i_uid
= current_fsuid();
109 result
->i_gid
= current_fsgid();
110 result
->i_mode
= mode
| S_IFDIR
;
111 hpfs_write_inode_nolock(result
);
113 hpfs_update_directory_times(dir
);
114 d_instantiate(dentry
, result
);
115 hpfs_unlock(dir
->i_sb
);
121 hpfs_free_dnode(dir
->i_sb
, dno
);
124 hpfs_free_sectors(dir
->i_sb
, fno
, 1);
126 hpfs_unlock(dir
->i_sb
);
130 static int hpfs_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
, bool excl
)
132 const unsigned char *name
= dentry
->d_name
.name
;
133 unsigned len
= dentry
->d_name
.len
;
134 struct inode
*result
= NULL
;
135 struct buffer_head
*bh
;
139 struct hpfs_dirent dee
;
141 if ((err
= hpfs_chk_name(name
, &len
)))
142 return err
==-ENOENT
? -EINVAL
: err
;
143 hpfs_lock(dir
->i_sb
);
145 fnode
= hpfs_alloc_fnode(dir
->i_sb
, hpfs_i(dir
)->i_dno
, &fno
, &bh
);
148 memset(&dee
, 0, sizeof dee
);
149 if (!(mode
& 0222)) dee
.read_only
= 1;
151 dee
.hidden
= name
[0] == '.';
152 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()));
155 result
= new_inode(dir
->i_sb
);
159 hpfs_init_inode(result
);
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
;
165 set_nlink(result
, 1);
166 hpfs_i(result
)->i_parent_dir
= dir
->i_ino
;
167 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
));
168 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;
173 result
->i_mode
&= ~0222;
174 result
->i_blocks
= 1;
176 result
->i_data
.a_ops
= &hpfs_aops
;
177 hpfs_i(result
)->mmu_private
= 0;
179 r
= hpfs_add_dirent(dir
, name
, len
, &dee
);
187 memcpy(fnode
->name
, name
, len
> 15 ? 15 : len
);
188 fnode
->up
= cpu_to_le32(dir
->i_ino
);
189 mark_buffer_dirty(bh
);
192 insert_inode_hash(result
);
194 if (!uid_eq(result
->i_uid
, current_fsuid()) ||
195 !gid_eq(result
->i_gid
, current_fsgid()) ||
196 result
->i_mode
!= (mode
| S_IFREG
)) {
197 result
->i_uid
= current_fsuid();
198 result
->i_gid
= current_fsgid();
199 result
->i_mode
= mode
| S_IFREG
;
200 hpfs_write_inode_nolock(result
);
202 hpfs_update_directory_times(dir
);
203 d_instantiate(dentry
, result
);
204 hpfs_unlock(dir
->i_sb
);
211 hpfs_free_sectors(dir
->i_sb
, fno
, 1);
213 hpfs_unlock(dir
->i_sb
);
217 static int hpfs_mknod(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
, dev_t rdev
)
219 const unsigned char *name
= dentry
->d_name
.name
;
220 unsigned len
= dentry
->d_name
.len
;
221 struct buffer_head
*bh
;
225 struct hpfs_dirent dee
;
226 struct inode
*result
= NULL
;
228 if ((err
= hpfs_chk_name(name
, &len
))) return err
==-ENOENT
? -EINVAL
: err
;
229 if (hpfs_sb(dir
->i_sb
)->sb_eas
< 2) return -EPERM
;
230 hpfs_lock(dir
->i_sb
);
232 fnode
= hpfs_alloc_fnode(dir
->i_sb
, hpfs_i(dir
)->i_dno
, &fno
, &bh
);
235 memset(&dee
, 0, sizeof dee
);
236 if (!(mode
& 0222)) dee
.read_only
= 1;
238 dee
.hidden
= name
[0] == '.';
239 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()));
242 result
= new_inode(dir
->i_sb
);
246 hpfs_init_inode(result
);
248 hpfs_i(result
)->i_parent_dir
= dir
->i_ino
;
249 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
));
250 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;
254 result
->i_uid
= current_fsuid();
255 result
->i_gid
= current_fsgid();
256 set_nlink(result
, 1);
258 result
->i_blocks
= 1;
259 init_special_inode(result
, mode
, rdev
);
261 r
= hpfs_add_dirent(dir
, name
, len
, &dee
);
269 memcpy(fnode
->name
, name
, len
> 15 ? 15 : len
);
270 fnode
->up
= cpu_to_le32(dir
->i_ino
);
271 mark_buffer_dirty(bh
);
273 insert_inode_hash(result
);
275 hpfs_write_inode_nolock(result
);
276 hpfs_update_directory_times(dir
);
277 d_instantiate(dentry
, result
);
279 hpfs_unlock(dir
->i_sb
);
285 hpfs_free_sectors(dir
->i_sb
, fno
, 1);
287 hpfs_unlock(dir
->i_sb
);
291 static int hpfs_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *symlink
)
293 const unsigned char *name
= dentry
->d_name
.name
;
294 unsigned len
= dentry
->d_name
.len
;
295 struct buffer_head
*bh
;
299 struct hpfs_dirent dee
;
300 struct inode
*result
;
302 if ((err
= hpfs_chk_name(name
, &len
))) return err
==-ENOENT
? -EINVAL
: err
;
303 hpfs_lock(dir
->i_sb
);
304 if (hpfs_sb(dir
->i_sb
)->sb_eas
< 2) {
305 hpfs_unlock(dir
->i_sb
);
309 fnode
= hpfs_alloc_fnode(dir
->i_sb
, hpfs_i(dir
)->i_dno
, &fno
, &bh
);
312 memset(&dee
, 0, sizeof dee
);
314 dee
.hidden
= name
[0] == '.';
315 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()));
318 result
= new_inode(dir
->i_sb
);
322 hpfs_init_inode(result
);
323 hpfs_i(result
)->i_parent_dir
= dir
->i_ino
;
324 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
));
325 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;
330 result
->i_uid
= current_fsuid();
331 result
->i_gid
= current_fsgid();
332 result
->i_blocks
= 1;
333 set_nlink(result
, 1);
334 result
->i_size
= strlen(symlink
);
335 inode_nohighmem(result
);
336 result
->i_op
= &page_symlink_inode_operations
;
337 result
->i_data
.a_ops
= &hpfs_symlink_aops
;
339 r
= hpfs_add_dirent(dir
, name
, len
, &dee
);
347 memcpy(fnode
->name
, name
, len
> 15 ? 15 : len
);
348 fnode
->up
= cpu_to_le32(dir
->i_ino
);
349 hpfs_set_ea(result
, fnode
, "SYMLINK", symlink
, strlen(symlink
));
350 mark_buffer_dirty(bh
);
353 insert_inode_hash(result
);
355 hpfs_write_inode_nolock(result
);
356 hpfs_update_directory_times(dir
);
357 d_instantiate(dentry
, result
);
358 hpfs_unlock(dir
->i_sb
);
364 hpfs_free_sectors(dir
->i_sb
, fno
, 1);
366 hpfs_unlock(dir
->i_sb
);
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
= d_inode(dentry
);
381 hpfs_lock(dir
->i_sb
);
382 hpfs_adjust_length(name
, &len
);
385 de
= map_dirent(dir
, hpfs_i(dir
)->i_dno
, name
, len
, &dno
, &qbh
);
397 r
= hpfs_remove_dirent(dir
, dno
, de
, &qbh
, 1);
400 hpfs_error(dir
->i_sb
, "there was error when removing dirent");
403 case 2: /* no space for deleting */
416 hpfs_update_directory_times(dir
);
417 hpfs_unlock(dir
->i_sb
);
421 static int hpfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
423 const unsigned char *name
= dentry
->d_name
.name
;
424 unsigned len
= dentry
->d_name
.len
;
425 struct quad_buffer_head qbh
;
426 struct hpfs_dirent
*de
;
427 struct inode
*inode
= d_inode(dentry
);
433 hpfs_adjust_length(name
, &len
);
434 hpfs_lock(dir
->i_sb
);
436 de
= map_dirent(dir
, hpfs_i(dir
)->i_dno
, name
, len
, &dno
, &qbh
);
448 hpfs_count_dnodes(dir
->i_sb
, hpfs_i(inode
)->i_dno
, NULL
, NULL
, &n_items
);
453 r
= hpfs_remove_dirent(dir
, dno
, de
, &qbh
, 1);
456 hpfs_error(dir
->i_sb
, "there was error when removing dirent");
472 hpfs_update_directory_times(dir
);
473 hpfs_unlock(dir
->i_sb
);
477 static int hpfs_symlink_readpage(struct file
*file
, struct page
*page
)
479 char *link
= page_address(page
);
480 struct inode
*i
= page
->mapping
->host
;
482 struct buffer_head
*bh
;
487 if (!(fnode
= hpfs_map_fnode(i
->i_sb
, i
->i_ino
, &bh
)))
489 err
= hpfs_read_ea(i
->i_sb
, fnode
, "SYMLINK", link
, PAGE_SIZE
);
493 hpfs_unlock(i
->i_sb
);
494 SetPageUptodate(page
);
499 hpfs_unlock(i
->i_sb
);
505 const struct address_space_operations hpfs_symlink_aops
= {
506 .readpage
= hpfs_symlink_readpage
509 static int hpfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
510 struct inode
*new_dir
, struct dentry
*new_dentry
)
512 const unsigned char *old_name
= old_dentry
->d_name
.name
;
513 unsigned old_len
= old_dentry
->d_name
.len
;
514 const unsigned char *new_name
= new_dentry
->d_name
.name
;
515 unsigned new_len
= new_dentry
->d_name
.len
;
516 struct inode
*i
= d_inode(old_dentry
);
517 struct inode
*new_inode
= d_inode(new_dentry
);
518 struct quad_buffer_head qbh
, qbh1
;
519 struct hpfs_dirent
*dep
, *nde
;
520 struct hpfs_dirent de
;
523 struct buffer_head
*bh
;
527 if ((err
= hpfs_chk_name(new_name
, &new_len
))) return err
;
529 hpfs_adjust_length(old_name
, &old_len
);
532 /* order doesn't matter, due to VFS exclusion */
534 /* Erm? Moving over the empty non-busy directory is perfectly legal */
535 if (new_inode
&& S_ISDIR(new_inode
->i_mode
)) {
540 if (!(dep
= map_dirent(old_dir
, hpfs_i(old_dir
)->i_dno
, old_name
, old_len
, &dno
, &qbh
))) {
541 hpfs_error(i
->i_sb
, "lookup succeeded but map dirent failed");
546 de
.hidden
= new_name
[0] == '.';
550 if ((r
= hpfs_remove_dirent(old_dir
, dno
, dep
, &qbh
, 1)) != 2) {
551 if ((nde
= map_dirent(new_dir
, hpfs_i(new_dir
)->i_dno
, new_name
, new_len
, NULL
, &qbh1
))) {
552 clear_nlink(new_inode
);
554 memcpy(nde
->name
, new_name
, new_len
);
555 hpfs_mark_4buffers_dirty(&qbh1
);
559 hpfs_error(new_dir
->i_sb
, "hpfs_rename: could not find dirent");
563 err
= r
== 2 ? -ENOSPC
: r
== 1 ? -EFSERROR
: 0;
567 if (new_dir
== old_dir
) hpfs_brelse4(&qbh
);
569 if ((r
= hpfs_add_dirent(new_dir
, new_name
, new_len
, &de
))) {
570 if (r
== -1) hpfs_error(new_dir
->i_sb
, "hpfs_rename: dirent already exists!");
571 err
= r
== 1 ? -ENOSPC
: -EFSERROR
;
572 if (new_dir
!= old_dir
) hpfs_brelse4(&qbh
);
576 if (new_dir
== old_dir
)
577 if (!(dep
= map_dirent(old_dir
, hpfs_i(old_dir
)->i_dno
, old_name
, old_len
, &dno
, &qbh
))) {
578 hpfs_error(i
->i_sb
, "lookup succeeded but map dirent failed at #2");
583 if ((r
= hpfs_remove_dirent(old_dir
, dno
, dep
, &qbh
, 0))) {
584 hpfs_error(i
->i_sb
, "hpfs_rename: could not remove dirent");
585 err
= r
== 2 ? -ENOSPC
: -EFSERROR
;
590 hpfs_i(i
)->i_parent_dir
= new_dir
->i_ino
;
591 if (S_ISDIR(i
->i_mode
)) {
595 if ((fnode
= hpfs_map_fnode(i
->i_sb
, i
->i_ino
, &bh
))) {
596 fnode
->up
= cpu_to_le32(new_dir
->i_ino
);
597 fnode
->len
= new_len
;
598 memcpy(fnode
->name
, new_name
, new_len
>15?15:new_len
);
599 if (new_len
< 15) memset(&fnode
->name
[new_len
], 0, 15 - new_len
);
600 mark_buffer_dirty(bh
);
605 hpfs_update_directory_times(old_dir
);
606 hpfs_update_directory_times(new_dir
);
608 hpfs_unlock(i
->i_sb
);
612 const struct inode_operations hpfs_dir_iops
=
614 .create
= hpfs_create
,
615 .lookup
= hpfs_lookup
,
616 .unlink
= hpfs_unlink
,
617 .symlink
= hpfs_symlink
,
621 .rename
= hpfs_rename
,
622 .setattr
= hpfs_setattr
,