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>
27 * NOTE! unlike strncmp, ufs_match returns 1 for success, 0 for failure.
29 * len <= UFS_MAXNAMLEN and de != NULL are guaranteed by caller.
31 static inline int ufs_match(struct super_block
*sb
, int len
,
32 const char * const name
, struct ufs_dir_entry
* de
)
34 if (len
!= ufs_get_de_namlen(sb
, de
))
38 return !memcmp(name
, de
->d_name
, len
);
41 static int ufs_commit_chunk(struct page
*page
, unsigned from
, unsigned to
)
43 struct inode
*dir
= page
->mapping
->host
;
46 page
->mapping
->a_ops
->commit_write(NULL
, page
, from
, to
);
48 err
= write_one_page(page
, 1);
54 static inline void ufs_put_page(struct page
*page
)
57 page_cache_release(page
);
60 static inline unsigned long ufs_dir_pages(struct inode
*inode
)
62 return (inode
->i_size
+PAGE_CACHE_SIZE
-1)>>PAGE_CACHE_SHIFT
;
65 ino_t
ufs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
)
68 struct ufs_dir_entry
*de
;
71 de
= ufs_find_entry(dir
, dentry
, &page
);
73 res
= fs32_to_cpu(dir
->i_sb
, de
->d_ino
);
80 /* Releases the page */
81 void ufs_set_link(struct inode
*dir
, struct ufs_dir_entry
*de
,
82 struct page
*page
, struct inode
*inode
)
84 unsigned from
= (char *) de
- (char *) page_address(page
);
85 unsigned to
= from
+ fs16_to_cpu(dir
->i_sb
, de
->d_reclen
);
89 err
= page
->mapping
->a_ops
->prepare_write(NULL
, page
, from
, to
);
91 de
->d_ino
= cpu_to_fs32(dir
->i_sb
, inode
->i_ino
);
92 ufs_set_de_type(dir
->i_sb
, de
, inode
->i_mode
);
93 err
= ufs_commit_chunk(page
, from
, to
);
95 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME_SEC
;
96 mark_inode_dirty(dir
);
100 static void ufs_check_page(struct page
*page
)
102 struct inode
*dir
= page
->mapping
->host
;
103 struct super_block
*sb
= dir
->i_sb
;
104 char *kaddr
= page_address(page
);
105 unsigned offs
, rec_len
;
106 unsigned limit
= PAGE_CACHE_SIZE
;
107 const unsigned chunk_mask
= UFS_SB(sb
)->s_uspi
->s_dirblksize
- 1;
108 struct ufs_dir_entry
*p
;
111 if ((dir
->i_size
>> PAGE_CACHE_SHIFT
) == page
->index
) {
112 limit
= dir
->i_size
& ~PAGE_CACHE_MASK
;
113 if (limit
& chunk_mask
)
118 for (offs
= 0; offs
<= limit
- UFS_DIR_REC_LEN(1); offs
+= rec_len
) {
119 p
= (struct ufs_dir_entry
*)(kaddr
+ offs
);
120 rec_len
= fs16_to_cpu(sb
, p
->d_reclen
);
122 if (rec_len
< UFS_DIR_REC_LEN(1))
126 if (rec_len
< UFS_DIR_REC_LEN(ufs_get_de_namlen(sb
, p
)))
128 if (((offs
+ rec_len
- 1) ^ offs
) & ~chunk_mask
)
130 if (fs32_to_cpu(sb
, p
->d_ino
) > (UFS_SB(sb
)->s_uspi
->s_ipg
*
131 UFS_SB(sb
)->s_uspi
->s_ncg
))
137 SetPageChecked(page
);
140 /* Too bad, we had an error */
143 ufs_error(sb
, "ufs_check_page",
144 "size of directory #%lu is not a multiple of chunk size",
149 error
= "rec_len is smaller than minimal";
152 error
= "unaligned directory entry";
155 error
= "rec_len is too small for name_len";
158 error
= "directory entry across blocks";
161 error
= "inode out of bounds";
163 ufs_error (sb
, "ufs_check_page", "bad entry in directory #%lu: %s - "
164 "offset=%lu, rec_len=%d, name_len=%d",
165 dir
->i_ino
, error
, (page
->index
<<PAGE_CACHE_SHIFT
)+offs
,
166 rec_len
, ufs_get_de_namlen(sb
, p
));
169 p
= (struct ufs_dir_entry
*)(kaddr
+ offs
);
170 ufs_error (sb
, "ext2_check_page",
171 "entry in directory #%lu spans the page boundary"
173 dir
->i_ino
, (page
->index
<<PAGE_CACHE_SHIFT
)+offs
);
175 SetPageChecked(page
);
179 static struct page
*ufs_get_page(struct inode
*dir
, unsigned long n
)
181 struct address_space
*mapping
= dir
->i_mapping
;
182 struct page
*page
= read_mapping_page(mapping
, n
, NULL
);
185 if (!PageChecked(page
))
186 ufs_check_page(page
);
194 return ERR_PTR(-EIO
);
198 * Return the offset into page `page_nr' of the last valid
199 * byte in that page, plus one.
202 ufs_last_byte(struct inode
*inode
, unsigned long page_nr
)
204 unsigned last_byte
= inode
->i_size
;
206 last_byte
-= page_nr
<< PAGE_CACHE_SHIFT
;
207 if (last_byte
> PAGE_CACHE_SIZE
)
208 last_byte
= PAGE_CACHE_SIZE
;
212 static inline struct ufs_dir_entry
*
213 ufs_next_entry(struct super_block
*sb
, struct ufs_dir_entry
*p
)
215 return (struct ufs_dir_entry
*)((char *)p
+
216 fs16_to_cpu(sb
, p
->d_reclen
));
219 struct ufs_dir_entry
*ufs_dotdot(struct inode
*dir
, struct page
**p
)
221 struct page
*page
= ufs_get_page(dir
, 0);
222 struct ufs_dir_entry
*de
= NULL
;
225 de
= ufs_next_entry(dir
->i_sb
,
226 (struct ufs_dir_entry
*)page_address(page
));
235 * finds an entry in the specified directory with the wanted name. It
236 * returns the page in which the entry was found, and the entry itself
237 * (as a parameter - res_dir). Page is returned mapped and unlocked.
238 * Entry is guaranteed to be valid.
240 struct ufs_dir_entry
*ufs_find_entry(struct inode
*dir
, struct dentry
*dentry
,
241 struct page
**res_page
)
243 struct super_block
*sb
= dir
->i_sb
;
244 const char *name
= dentry
->d_name
.name
;
245 int namelen
= dentry
->d_name
.len
;
246 unsigned reclen
= UFS_DIR_REC_LEN(namelen
);
247 unsigned long start
, n
;
248 unsigned long npages
= ufs_dir_pages(dir
);
249 struct page
*page
= NULL
;
250 struct ufs_inode_info
*ui
= UFS_I(dir
);
251 struct ufs_dir_entry
*de
;
253 UFSD("ENTER, dir_ino %lu, name %s, namlen %u\n", dir
->i_ino
, name
, namelen
);
255 if (npages
== 0 || namelen
> UFS_MAXNAMLEN
)
261 start
= ui
->i_dir_start_lookup
;
268 page
= ufs_get_page(dir
, n
);
270 kaddr
= page_address(page
);
271 de
= (struct ufs_dir_entry
*) kaddr
;
272 kaddr
+= ufs_last_byte(dir
, n
) - reclen
;
273 while ((char *) de
<= kaddr
) {
274 if (de
->d_reclen
== 0) {
275 ufs_error(dir
->i_sb
, __FUNCTION__
,
276 "zero-length directory entry");
280 if (ufs_match(sb
, namelen
, name
, de
))
282 de
= ufs_next_entry(sb
, de
);
288 } while (n
!= start
);
294 ui
->i_dir_start_lookup
= n
;
301 int ufs_add_link(struct dentry
*dentry
, struct inode
*inode
)
303 struct inode
*dir
= dentry
->d_parent
->d_inode
;
304 const char *name
= dentry
->d_name
.name
;
305 int namelen
= dentry
->d_name
.len
;
306 struct super_block
*sb
= dir
->i_sb
;
307 unsigned reclen
= UFS_DIR_REC_LEN(namelen
);
308 const unsigned int chunk_size
= UFS_SB(sb
)->s_uspi
->s_dirblksize
;
309 unsigned short rec_len
, name_len
;
310 struct page
*page
= NULL
;
311 struct ufs_dir_entry
*de
;
312 unsigned long npages
= ufs_dir_pages(dir
);
318 UFSD("ENTER, name %s, namelen %u\n", name
, namelen
);
321 * We take care of directory expansion in the same loop.
322 * This code plays outside i_size, so it locks the page
323 * to protect that region.
325 for (n
= 0; n
<= npages
; n
++) {
328 page
= ufs_get_page(dir
, n
);
333 kaddr
= page_address(page
);
334 dir_end
= kaddr
+ ufs_last_byte(dir
, n
);
335 de
= (struct ufs_dir_entry
*)kaddr
;
336 kaddr
+= PAGE_CACHE_SIZE
- reclen
;
337 while ((char *)de
<= kaddr
) {
338 if ((char *)de
== dir_end
) {
341 rec_len
= chunk_size
;
342 de
->d_reclen
= cpu_to_fs16(sb
, chunk_size
);
346 if (de
->d_reclen
== 0) {
347 ufs_error(dir
->i_sb
, __FUNCTION__
,
348 "zero-length directory entry");
353 if (ufs_match(sb
, namelen
, name
, de
))
355 name_len
= UFS_DIR_REC_LEN(ufs_get_de_namlen(sb
, de
));
356 rec_len
= fs16_to_cpu(sb
, de
->d_reclen
);
357 if (!de
->d_ino
&& rec_len
>= reclen
)
359 if (rec_len
>= name_len
+ reclen
)
361 de
= (struct ufs_dir_entry
*) ((char *) de
+ rec_len
);
370 from
= (char*)de
- (char*)page_address(page
);
372 err
= page
->mapping
->a_ops
->prepare_write(NULL
, page
, from
, to
);
376 struct ufs_dir_entry
*de1
=
377 (struct ufs_dir_entry
*) ((char *) de
+ name_len
);
378 de1
->d_reclen
= cpu_to_fs16(sb
, rec_len
- name_len
);
379 de
->d_reclen
= cpu_to_fs16(sb
, name_len
);
384 ufs_set_de_namlen(sb
, de
, namelen
);
385 memcpy(de
->d_name
, name
, namelen
+ 1);
386 de
->d_ino
= cpu_to_fs32(sb
, inode
->i_ino
);
387 ufs_set_de_type(sb
, de
, inode
->i_mode
);
389 err
= ufs_commit_chunk(page
, from
, to
);
390 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME_SEC
;
392 mark_inode_dirty(dir
);
403 static inline unsigned
404 ufs_validate_entry(struct super_block
*sb
, char *base
,
405 unsigned offset
, unsigned mask
)
407 struct ufs_dir_entry
*de
= (struct ufs_dir_entry
*)(base
+ offset
);
408 struct ufs_dir_entry
*p
= (struct ufs_dir_entry
*)(base
+ (offset
&mask
));
409 while ((char*)p
< (char*)de
) {
410 if (p
->d_reclen
== 0)
412 p
= ufs_next_entry(sb
, p
);
414 return (char *)p
- base
;
419 * This is blatantly stolen from ext2fs
422 ufs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
424 loff_t pos
= filp
->f_pos
;
425 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
426 struct super_block
*sb
= inode
->i_sb
;
427 unsigned int offset
= pos
& ~PAGE_CACHE_MASK
;
428 unsigned long n
= pos
>> PAGE_CACHE_SHIFT
;
429 unsigned long npages
= ufs_dir_pages(inode
);
430 unsigned chunk_mask
= ~(UFS_SB(sb
)->s_uspi
->s_dirblksize
- 1);
431 int need_revalidate
= filp
->f_version
!= inode
->i_version
;
432 unsigned flags
= UFS_SB(sb
)->s_flags
;
436 if (pos
> inode
->i_size
- UFS_DIR_REC_LEN(1))
439 for ( ; n
< npages
; n
++, offset
= 0) {
441 struct ufs_dir_entry
*de
;
443 struct page
*page
= ufs_get_page(inode
, n
);
446 ufs_error(sb
, __FUNCTION__
,
449 filp
->f_pos
+= PAGE_CACHE_SIZE
- offset
;
452 kaddr
= page_address(page
);
453 if (unlikely(need_revalidate
)) {
455 offset
= ufs_validate_entry(sb
, kaddr
, offset
, chunk_mask
);
456 filp
->f_pos
= (n
<<PAGE_CACHE_SHIFT
) + offset
;
458 filp
->f_version
= inode
->i_version
;
461 de
= (struct ufs_dir_entry
*)(kaddr
+offset
);
462 limit
= kaddr
+ ufs_last_byte(inode
, n
) - UFS_DIR_REC_LEN(1);
463 for ( ;(char*)de
<= limit
; de
= ufs_next_entry(sb
, de
)) {
464 if (de
->d_reclen
== 0) {
465 ufs_error(sb
, __FUNCTION__
,
466 "zero-length directory entry");
472 unsigned char d_type
= DT_UNKNOWN
;
474 offset
= (char *)de
- kaddr
;
476 UFSD("filldir(%s,%u)\n", de
->d_name
,
477 fs32_to_cpu(sb
, de
->d_ino
));
478 UFSD("namlen %u\n", ufs_get_de_namlen(sb
, de
));
480 if ((flags
& UFS_DE_MASK
) == UFS_DE_44BSD
)
481 d_type
= de
->d_u
.d_44
.d_type
;
483 over
= filldir(dirent
, de
->d_name
,
484 ufs_get_de_namlen(sb
, de
),
485 (n
<<PAGE_CACHE_SHIFT
) | offset
,
486 fs32_to_cpu(sb
, de
->d_ino
), d_type
);
492 filp
->f_pos
+= fs16_to_cpu(sb
, de
->d_reclen
);
501 * ufs_delete_entry deletes a directory entry by merging it with the
504 int ufs_delete_entry(struct inode
*inode
, struct ufs_dir_entry
*dir
,
507 struct super_block
*sb
= inode
->i_sb
;
508 struct address_space
*mapping
= page
->mapping
;
509 char *kaddr
= page_address(page
);
510 unsigned from
= ((char*)dir
- kaddr
) & ~(UFS_SB(sb
)->s_uspi
->s_dirblksize
- 1);
511 unsigned to
= ((char*)dir
- kaddr
) + fs16_to_cpu(sb
, dir
->d_reclen
);
512 struct ufs_dir_entry
*pde
= NULL
;
513 struct ufs_dir_entry
*de
= (struct ufs_dir_entry
*) (kaddr
+ from
);
518 UFSD("ino %u, reclen %u, namlen %u, name %s\n",
519 fs32_to_cpu(sb
, de
->d_ino
),
520 fs16_to_cpu(sb
, de
->d_reclen
),
521 ufs_get_de_namlen(sb
, de
), de
->d_name
);
523 while ((char*)de
< (char*)dir
) {
524 if (de
->d_reclen
== 0) {
525 ufs_error(inode
->i_sb
, __FUNCTION__
,
526 "zero-length directory entry");
531 de
= ufs_next_entry(sb
, de
);
534 from
= (char*)pde
- (char*)page_address(page
);
536 err
= mapping
->a_ops
->prepare_write(NULL
, page
, from
, to
);
539 pde
->d_reclen
= cpu_to_fs16(sb
, to
-from
);
541 err
= ufs_commit_chunk(page
, from
, to
);
542 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME_SEC
;
543 mark_inode_dirty(inode
);
550 int ufs_make_empty(struct inode
* inode
, struct inode
*dir
)
552 struct super_block
* sb
= dir
->i_sb
;
553 struct address_space
*mapping
= inode
->i_mapping
;
554 struct page
*page
= grab_cache_page(mapping
, 0);
555 const unsigned int chunk_size
= UFS_SB(sb
)->s_uspi
->s_dirblksize
;
556 struct ufs_dir_entry
* de
;
563 err
= mapping
->a_ops
->prepare_write(NULL
, page
, 0, chunk_size
);
570 base
= (char*)page_address(page
);
571 memset(base
, 0, PAGE_CACHE_SIZE
);
573 de
= (struct ufs_dir_entry
*) base
;
575 de
->d_ino
= cpu_to_fs32(sb
, inode
->i_ino
);
576 ufs_set_de_type(sb
, de
, inode
->i_mode
);
577 ufs_set_de_namlen(sb
, de
, 1);
578 de
->d_reclen
= cpu_to_fs16(sb
, UFS_DIR_REC_LEN(1));
579 strcpy (de
->d_name
, ".");
580 de
= (struct ufs_dir_entry
*)
581 ((char *)de
+ fs16_to_cpu(sb
, de
->d_reclen
));
582 de
->d_ino
= cpu_to_fs32(sb
, dir
->i_ino
);
583 ufs_set_de_type(sb
, de
, dir
->i_mode
);
584 de
->d_reclen
= cpu_to_fs16(sb
, chunk_size
- UFS_DIR_REC_LEN(1));
585 ufs_set_de_namlen(sb
, de
, 2);
586 strcpy (de
->d_name
, "..");
588 err
= ufs_commit_chunk(page
, 0, chunk_size
);
591 page_cache_release(page
);
596 * routine to check that the specified directory is empty (for rmdir)
598 int ufs_empty_dir(struct inode
* inode
)
600 struct super_block
*sb
= inode
->i_sb
;
601 struct page
*page
= NULL
;
602 unsigned long i
, npages
= ufs_dir_pages(inode
);
604 for (i
= 0; i
< npages
; i
++) {
606 struct ufs_dir_entry
*de
;
607 page
= ufs_get_page(inode
, i
);
612 kaddr
= page_address(page
);
613 de
= (struct ufs_dir_entry
*)kaddr
;
614 kaddr
+= ufs_last_byte(inode
, i
) - UFS_DIR_REC_LEN(1);
616 while ((char *)de
<= kaddr
) {
617 if (de
->d_reclen
== 0) {
618 ufs_error(inode
->i_sb
, __FUNCTION__
,
619 "zero-length directory entry: "
620 "kaddr=%p, de=%p\n", kaddr
, de
);
624 u16 namelen
=ufs_get_de_namlen(sb
, de
);
625 /* check for . and .. */
626 if (de
->d_name
[0] != '.')
632 fs32_to_cpu(sb
, de
->d_ino
))
634 } else if (de
->d_name
[1] != '.')
637 de
= ufs_next_entry(sb
, de
);
648 const struct file_operations ufs_dir_operations
= {
649 .read
= generic_read_dir
,
650 .readdir
= ufs_readdir
,