2 * linux/fs/ufs/ufs_dir.c
5 * Adrian Rodriguez (adrian@franklins-tower.rutgers.edu)
6 * Laboratory for Computer Science Research Computing Facility
7 * Rutgers, The State University of New Jersey
9 * swab support by Francois-Rene Rideau <fare@tunes.org> 19970406
11 * 4.4BSD (FreeBSD) support added on February 1st 1998 by
12 * Niels Kristian Bech Jensen <nkbj@image.dk> partially based
13 * on code by Martin von Loewis <martin@mira.isdn.cs.tu-berlin.de>.
15 * Migration to usage of "page cache" on May 2006 by
16 * Evgeniy Dushistov <dushistov@mail.ru> based on ext2 code base.
19 #include <linux/time.h>
21 #include <linux/ufs_fs.h>
22 #include <linux/swap.h>
29 * NOTE! unlike strncmp, ufs_match returns 1 for success, 0 for failure.
31 * len <= UFS_MAXNAMLEN and de != NULL are guaranteed by caller.
33 static inline int ufs_match(struct super_block
*sb
, int len
,
34 const char * const name
, struct ufs_dir_entry
* de
)
36 if (len
!= ufs_get_de_namlen(sb
, de
))
40 return !memcmp(name
, de
->d_name
, len
);
43 static int ufs_commit_chunk(struct page
*page
, loff_t pos
, unsigned len
)
45 struct address_space
*mapping
= page
->mapping
;
46 struct inode
*dir
= mapping
->host
;
50 block_write_end(NULL
, mapping
, pos
, len
, len
, page
, NULL
);
51 if (pos
+len
> dir
->i_size
) {
52 i_size_write(dir
, pos
+len
);
53 mark_inode_dirty(dir
);
56 err
= write_one_page(page
, 1);
62 static inline void ufs_put_page(struct page
*page
)
65 page_cache_release(page
);
68 static inline unsigned long ufs_dir_pages(struct inode
*inode
)
70 return (inode
->i_size
+PAGE_CACHE_SIZE
-1)>>PAGE_CACHE_SHIFT
;
73 ino_t
ufs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
)
76 struct ufs_dir_entry
*de
;
79 de
= ufs_find_entry(dir
, dentry
, &page
);
81 res
= fs32_to_cpu(dir
->i_sb
, de
->d_ino
);
88 /* Releases the page */
89 void ufs_set_link(struct inode
*dir
, struct ufs_dir_entry
*de
,
90 struct page
*page
, struct inode
*inode
)
92 loff_t pos
= page_offset(page
) +
93 (char *) de
- (char *) page_address(page
);
94 unsigned len
= fs16_to_cpu(dir
->i_sb
, de
->d_reclen
);
98 err
= __ufs_write_begin(NULL
, page
->mapping
, pos
, len
,
99 AOP_FLAG_UNINTERRUPTIBLE
, &page
, NULL
);
102 de
->d_ino
= cpu_to_fs32(dir
->i_sb
, inode
->i_ino
);
103 ufs_set_de_type(dir
->i_sb
, de
, inode
->i_mode
);
105 err
= ufs_commit_chunk(page
, pos
, len
);
107 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME_SEC
;
108 mark_inode_dirty(dir
);
112 static void ufs_check_page(struct page
*page
)
114 struct inode
*dir
= page
->mapping
->host
;
115 struct super_block
*sb
= dir
->i_sb
;
116 char *kaddr
= page_address(page
);
117 unsigned offs
, rec_len
;
118 unsigned limit
= PAGE_CACHE_SIZE
;
119 const unsigned chunk_mask
= UFS_SB(sb
)->s_uspi
->s_dirblksize
- 1;
120 struct ufs_dir_entry
*p
;
123 if ((dir
->i_size
>> PAGE_CACHE_SHIFT
) == page
->index
) {
124 limit
= dir
->i_size
& ~PAGE_CACHE_MASK
;
125 if (limit
& chunk_mask
)
130 for (offs
= 0; offs
<= limit
- UFS_DIR_REC_LEN(1); offs
+= rec_len
) {
131 p
= (struct ufs_dir_entry
*)(kaddr
+ offs
);
132 rec_len
= fs16_to_cpu(sb
, p
->d_reclen
);
134 if (rec_len
< UFS_DIR_REC_LEN(1))
138 if (rec_len
< UFS_DIR_REC_LEN(ufs_get_de_namlen(sb
, p
)))
140 if (((offs
+ rec_len
- 1) ^ offs
) & ~chunk_mask
)
142 if (fs32_to_cpu(sb
, p
->d_ino
) > (UFS_SB(sb
)->s_uspi
->s_ipg
*
143 UFS_SB(sb
)->s_uspi
->s_ncg
))
149 SetPageChecked(page
);
152 /* Too bad, we had an error */
155 ufs_error(sb
, "ufs_check_page",
156 "size of directory #%lu is not a multiple of chunk size",
161 error
= "rec_len is smaller than minimal";
164 error
= "unaligned directory entry";
167 error
= "rec_len is too small for name_len";
170 error
= "directory entry across blocks";
173 error
= "inode out of bounds";
175 ufs_error (sb
, "ufs_check_page", "bad entry in directory #%lu: %s - "
176 "offset=%lu, rec_len=%d, name_len=%d",
177 dir
->i_ino
, error
, (page
->index
<<PAGE_CACHE_SHIFT
)+offs
,
178 rec_len
, ufs_get_de_namlen(sb
, p
));
181 p
= (struct ufs_dir_entry
*)(kaddr
+ offs
);
182 ufs_error(sb
, __FUNCTION__
,
183 "entry in directory #%lu spans the page boundary"
185 dir
->i_ino
, (page
->index
<<PAGE_CACHE_SHIFT
)+offs
);
187 SetPageChecked(page
);
191 static struct page
*ufs_get_page(struct inode
*dir
, unsigned long n
)
193 struct address_space
*mapping
= dir
->i_mapping
;
194 struct page
*page
= read_mapping_page(mapping
, n
, NULL
);
197 if (!PageChecked(page
))
198 ufs_check_page(page
);
206 return ERR_PTR(-EIO
);
210 * Return the offset into page `page_nr' of the last valid
211 * byte in that page, plus one.
214 ufs_last_byte(struct inode
*inode
, unsigned long page_nr
)
216 unsigned last_byte
= inode
->i_size
;
218 last_byte
-= page_nr
<< PAGE_CACHE_SHIFT
;
219 if (last_byte
> PAGE_CACHE_SIZE
)
220 last_byte
= PAGE_CACHE_SIZE
;
224 static inline struct ufs_dir_entry
*
225 ufs_next_entry(struct super_block
*sb
, struct ufs_dir_entry
*p
)
227 return (struct ufs_dir_entry
*)((char *)p
+
228 fs16_to_cpu(sb
, p
->d_reclen
));
231 struct ufs_dir_entry
*ufs_dotdot(struct inode
*dir
, struct page
**p
)
233 struct page
*page
= ufs_get_page(dir
, 0);
234 struct ufs_dir_entry
*de
= NULL
;
237 de
= ufs_next_entry(dir
->i_sb
,
238 (struct ufs_dir_entry
*)page_address(page
));
247 * finds an entry in the specified directory with the wanted name. It
248 * returns the page in which the entry was found, and the entry itself
249 * (as a parameter - res_dir). Page is returned mapped and unlocked.
250 * Entry is guaranteed to be valid.
252 struct ufs_dir_entry
*ufs_find_entry(struct inode
*dir
, struct dentry
*dentry
,
253 struct page
**res_page
)
255 struct super_block
*sb
= dir
->i_sb
;
256 const char *name
= dentry
->d_name
.name
;
257 int namelen
= dentry
->d_name
.len
;
258 unsigned reclen
= UFS_DIR_REC_LEN(namelen
);
259 unsigned long start
, n
;
260 unsigned long npages
= ufs_dir_pages(dir
);
261 struct page
*page
= NULL
;
262 struct ufs_inode_info
*ui
= UFS_I(dir
);
263 struct ufs_dir_entry
*de
;
265 UFSD("ENTER, dir_ino %lu, name %s, namlen %u\n", dir
->i_ino
, name
, namelen
);
267 if (npages
== 0 || namelen
> UFS_MAXNAMLEN
)
273 start
= ui
->i_dir_start_lookup
;
280 page
= ufs_get_page(dir
, n
);
282 kaddr
= page_address(page
);
283 de
= (struct ufs_dir_entry
*) kaddr
;
284 kaddr
+= ufs_last_byte(dir
, n
) - reclen
;
285 while ((char *) de
<= kaddr
) {
286 if (de
->d_reclen
== 0) {
287 ufs_error(dir
->i_sb
, __FUNCTION__
,
288 "zero-length directory entry");
292 if (ufs_match(sb
, namelen
, name
, de
))
294 de
= ufs_next_entry(sb
, de
);
300 } while (n
!= start
);
306 ui
->i_dir_start_lookup
= n
;
313 int ufs_add_link(struct dentry
*dentry
, struct inode
*inode
)
315 struct inode
*dir
= dentry
->d_parent
->d_inode
;
316 const char *name
= dentry
->d_name
.name
;
317 int namelen
= dentry
->d_name
.len
;
318 struct super_block
*sb
= dir
->i_sb
;
319 unsigned reclen
= UFS_DIR_REC_LEN(namelen
);
320 const unsigned int chunk_size
= UFS_SB(sb
)->s_uspi
->s_dirblksize
;
321 unsigned short rec_len
, name_len
;
322 struct page
*page
= NULL
;
323 struct ufs_dir_entry
*de
;
324 unsigned long npages
= ufs_dir_pages(dir
);
330 UFSD("ENTER, name %s, namelen %u\n", name
, namelen
);
333 * We take care of directory expansion in the same loop.
334 * This code plays outside i_size, so it locks the page
335 * to protect that region.
337 for (n
= 0; n
<= npages
; n
++) {
340 page
= ufs_get_page(dir
, n
);
345 kaddr
= page_address(page
);
346 dir_end
= kaddr
+ ufs_last_byte(dir
, n
);
347 de
= (struct ufs_dir_entry
*)kaddr
;
348 kaddr
+= PAGE_CACHE_SIZE
- reclen
;
349 while ((char *)de
<= kaddr
) {
350 if ((char *)de
== dir_end
) {
353 rec_len
= chunk_size
;
354 de
->d_reclen
= cpu_to_fs16(sb
, chunk_size
);
358 if (de
->d_reclen
== 0) {
359 ufs_error(dir
->i_sb
, __FUNCTION__
,
360 "zero-length directory entry");
365 if (ufs_match(sb
, namelen
, name
, de
))
367 name_len
= UFS_DIR_REC_LEN(ufs_get_de_namlen(sb
, de
));
368 rec_len
= fs16_to_cpu(sb
, de
->d_reclen
);
369 if (!de
->d_ino
&& rec_len
>= reclen
)
371 if (rec_len
>= name_len
+ reclen
)
373 de
= (struct ufs_dir_entry
*) ((char *) de
+ rec_len
);
382 pos
= page_offset(page
) +
383 (char*)de
- (char*)page_address(page
);
384 err
= __ufs_write_begin(NULL
, page
->mapping
, pos
, rec_len
,
385 AOP_FLAG_UNINTERRUPTIBLE
, &page
, NULL
);
389 struct ufs_dir_entry
*de1
=
390 (struct ufs_dir_entry
*) ((char *) de
+ name_len
);
391 de1
->d_reclen
= cpu_to_fs16(sb
, rec_len
- name_len
);
392 de
->d_reclen
= cpu_to_fs16(sb
, name_len
);
397 ufs_set_de_namlen(sb
, de
, namelen
);
398 memcpy(de
->d_name
, name
, namelen
+ 1);
399 de
->d_ino
= cpu_to_fs32(sb
, inode
->i_ino
);
400 ufs_set_de_type(sb
, de
, inode
->i_mode
);
402 err
= ufs_commit_chunk(page
, pos
, rec_len
);
403 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME_SEC
;
405 mark_inode_dirty(dir
);
416 static inline unsigned
417 ufs_validate_entry(struct super_block
*sb
, char *base
,
418 unsigned offset
, unsigned mask
)
420 struct ufs_dir_entry
*de
= (struct ufs_dir_entry
*)(base
+ offset
);
421 struct ufs_dir_entry
*p
= (struct ufs_dir_entry
*)(base
+ (offset
&mask
));
422 while ((char*)p
< (char*)de
) {
423 if (p
->d_reclen
== 0)
425 p
= ufs_next_entry(sb
, p
);
427 return (char *)p
- base
;
432 * This is blatantly stolen from ext2fs
435 ufs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
437 loff_t pos
= filp
->f_pos
;
438 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
439 struct super_block
*sb
= inode
->i_sb
;
440 unsigned int offset
= pos
& ~PAGE_CACHE_MASK
;
441 unsigned long n
= pos
>> PAGE_CACHE_SHIFT
;
442 unsigned long npages
= ufs_dir_pages(inode
);
443 unsigned chunk_mask
= ~(UFS_SB(sb
)->s_uspi
->s_dirblksize
- 1);
444 int need_revalidate
= filp
->f_version
!= inode
->i_version
;
445 unsigned flags
= UFS_SB(sb
)->s_flags
;
449 if (pos
> inode
->i_size
- UFS_DIR_REC_LEN(1))
452 for ( ; n
< npages
; n
++, offset
= 0) {
454 struct ufs_dir_entry
*de
;
456 struct page
*page
= ufs_get_page(inode
, n
);
459 ufs_error(sb
, __FUNCTION__
,
462 filp
->f_pos
+= PAGE_CACHE_SIZE
- offset
;
465 kaddr
= page_address(page
);
466 if (unlikely(need_revalidate
)) {
468 offset
= ufs_validate_entry(sb
, kaddr
, offset
, chunk_mask
);
469 filp
->f_pos
= (n
<<PAGE_CACHE_SHIFT
) + offset
;
471 filp
->f_version
= inode
->i_version
;
474 de
= (struct ufs_dir_entry
*)(kaddr
+offset
);
475 limit
= kaddr
+ ufs_last_byte(inode
, n
) - UFS_DIR_REC_LEN(1);
476 for ( ;(char*)de
<= limit
; de
= ufs_next_entry(sb
, de
)) {
477 if (de
->d_reclen
== 0) {
478 ufs_error(sb
, __FUNCTION__
,
479 "zero-length directory entry");
485 unsigned char d_type
= DT_UNKNOWN
;
487 offset
= (char *)de
- kaddr
;
489 UFSD("filldir(%s,%u)\n", de
->d_name
,
490 fs32_to_cpu(sb
, de
->d_ino
));
491 UFSD("namlen %u\n", ufs_get_de_namlen(sb
, de
));
493 if ((flags
& UFS_DE_MASK
) == UFS_DE_44BSD
)
494 d_type
= de
->d_u
.d_44
.d_type
;
496 over
= filldir(dirent
, de
->d_name
,
497 ufs_get_de_namlen(sb
, de
),
498 (n
<<PAGE_CACHE_SHIFT
) | offset
,
499 fs32_to_cpu(sb
, de
->d_ino
), d_type
);
505 filp
->f_pos
+= fs16_to_cpu(sb
, de
->d_reclen
);
514 * ufs_delete_entry deletes a directory entry by merging it with the
517 int ufs_delete_entry(struct inode
*inode
, struct ufs_dir_entry
*dir
,
520 struct super_block
*sb
= inode
->i_sb
;
521 struct address_space
*mapping
= page
->mapping
;
522 char *kaddr
= page_address(page
);
523 unsigned from
= ((char*)dir
- kaddr
) & ~(UFS_SB(sb
)->s_uspi
->s_dirblksize
- 1);
524 unsigned to
= ((char*)dir
- kaddr
) + fs16_to_cpu(sb
, dir
->d_reclen
);
526 struct ufs_dir_entry
*pde
= NULL
;
527 struct ufs_dir_entry
*de
= (struct ufs_dir_entry
*) (kaddr
+ from
);
532 UFSD("ino %u, reclen %u, namlen %u, name %s\n",
533 fs32_to_cpu(sb
, de
->d_ino
),
534 fs16_to_cpu(sb
, de
->d_reclen
),
535 ufs_get_de_namlen(sb
, de
), de
->d_name
);
537 while ((char*)de
< (char*)dir
) {
538 if (de
->d_reclen
== 0) {
539 ufs_error(inode
->i_sb
, __FUNCTION__
,
540 "zero-length directory entry");
545 de
= ufs_next_entry(sb
, de
);
548 from
= (char*)pde
- (char*)page_address(page
);
550 pos
= page_offset(page
) + from
;
552 err
= __ufs_write_begin(NULL
, mapping
, pos
, to
- from
,
553 AOP_FLAG_UNINTERRUPTIBLE
, &page
, NULL
);
556 pde
->d_reclen
= cpu_to_fs16(sb
, to
- from
);
558 err
= ufs_commit_chunk(page
, pos
, to
- from
);
559 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME_SEC
;
560 mark_inode_dirty(inode
);
567 int ufs_make_empty(struct inode
* inode
, struct inode
*dir
)
569 struct super_block
* sb
= dir
->i_sb
;
570 struct address_space
*mapping
= inode
->i_mapping
;
571 struct page
*page
= grab_cache_page(mapping
, 0);
572 const unsigned int chunk_size
= UFS_SB(sb
)->s_uspi
->s_dirblksize
;
573 struct ufs_dir_entry
* de
;
580 err
= __ufs_write_begin(NULL
, mapping
, 0, chunk_size
,
581 AOP_FLAG_UNINTERRUPTIBLE
, &page
, NULL
);
588 base
= (char*)page_address(page
);
589 memset(base
, 0, PAGE_CACHE_SIZE
);
591 de
= (struct ufs_dir_entry
*) base
;
593 de
->d_ino
= cpu_to_fs32(sb
, inode
->i_ino
);
594 ufs_set_de_type(sb
, de
, inode
->i_mode
);
595 ufs_set_de_namlen(sb
, de
, 1);
596 de
->d_reclen
= cpu_to_fs16(sb
, UFS_DIR_REC_LEN(1));
597 strcpy (de
->d_name
, ".");
598 de
= (struct ufs_dir_entry
*)
599 ((char *)de
+ fs16_to_cpu(sb
, de
->d_reclen
));
600 de
->d_ino
= cpu_to_fs32(sb
, dir
->i_ino
);
601 ufs_set_de_type(sb
, de
, dir
->i_mode
);
602 de
->d_reclen
= cpu_to_fs16(sb
, chunk_size
- UFS_DIR_REC_LEN(1));
603 ufs_set_de_namlen(sb
, de
, 2);
604 strcpy (de
->d_name
, "..");
607 err
= ufs_commit_chunk(page
, 0, chunk_size
);
609 page_cache_release(page
);
614 * routine to check that the specified directory is empty (for rmdir)
616 int ufs_empty_dir(struct inode
* inode
)
618 struct super_block
*sb
= inode
->i_sb
;
619 struct page
*page
= NULL
;
620 unsigned long i
, npages
= ufs_dir_pages(inode
);
622 for (i
= 0; i
< npages
; i
++) {
624 struct ufs_dir_entry
*de
;
625 page
= ufs_get_page(inode
, i
);
630 kaddr
= page_address(page
);
631 de
= (struct ufs_dir_entry
*)kaddr
;
632 kaddr
+= ufs_last_byte(inode
, i
) - UFS_DIR_REC_LEN(1);
634 while ((char *)de
<= kaddr
) {
635 if (de
->d_reclen
== 0) {
636 ufs_error(inode
->i_sb
, __FUNCTION__
,
637 "zero-length directory entry: "
638 "kaddr=%p, de=%p\n", kaddr
, de
);
642 u16 namelen
=ufs_get_de_namlen(sb
, de
);
643 /* check for . and .. */
644 if (de
->d_name
[0] != '.')
650 fs32_to_cpu(sb
, de
->d_ino
))
652 } else if (de
->d_name
[1] != '.')
655 de
= ufs_next_entry(sb
, de
);
666 const struct file_operations ufs_dir_operations
= {
667 .read
= generic_read_dir
,
668 .readdir
= ufs_readdir
,