1 // SPDX-License-Identifier: GPL-2.0
3 * linux/fs/ufs/ufs_dir.c
6 * Adrian Rodriguez (adrian@franklins-tower.rutgers.edu)
7 * Laboratory for Computer Science Research Computing Facility
8 * Rutgers, The State University of New Jersey
10 * swab support by Francois-Rene Rideau <fare@tunes.org> 19970406
12 * 4.4BSD (FreeBSD) support added on February 1st 1998 by
13 * Niels Kristian Bech Jensen <nkbj@image.dk> partially based
14 * on code by Martin von Loewis <martin@mira.isdn.cs.tu-berlin.de>.
16 * Migration to usage of "page cache" on May 2006 by
17 * Evgeniy Dushistov <dushistov@mail.ru> based on ext2 code base.
20 #include <linux/time.h>
22 #include <linux/swap.h>
30 * NOTE! unlike strncmp, ufs_match returns 1 for success, 0 for failure.
32 * len <= UFS_MAXNAMLEN and de != NULL are guaranteed by caller.
34 static inline int ufs_match(struct super_block
*sb
, int len
,
35 const unsigned char *name
, struct ufs_dir_entry
*de
)
37 if (len
!= ufs_get_de_namlen(sb
, de
))
41 return !memcmp(name
, de
->d_name
, len
);
44 static int ufs_commit_chunk(struct page
*page
, loff_t pos
, unsigned len
)
46 struct address_space
*mapping
= page
->mapping
;
47 struct inode
*dir
= mapping
->host
;
51 block_write_end(NULL
, mapping
, pos
, len
, len
, page
, NULL
);
52 if (pos
+len
> dir
->i_size
) {
53 i_size_write(dir
, pos
+len
);
54 mark_inode_dirty(dir
);
57 err
= write_one_page(page
);
63 static inline void ufs_put_page(struct page
*page
)
69 ino_t
ufs_inode_by_name(struct inode
*dir
, const struct qstr
*qstr
)
72 struct ufs_dir_entry
*de
;
75 de
= ufs_find_entry(dir
, qstr
, &page
);
77 res
= fs32_to_cpu(dir
->i_sb
, de
->d_ino
);
84 /* Releases the page */
85 void ufs_set_link(struct inode
*dir
, struct ufs_dir_entry
*de
,
86 struct page
*page
, struct inode
*inode
,
89 loff_t pos
= page_offset(page
) +
90 (char *) de
- (char *) page_address(page
);
91 unsigned len
= fs16_to_cpu(dir
->i_sb
, de
->d_reclen
);
95 err
= ufs_prepare_chunk(page
, pos
, len
);
98 de
->d_ino
= cpu_to_fs32(dir
->i_sb
, inode
->i_ino
);
99 ufs_set_de_type(dir
->i_sb
, de
, inode
->i_mode
);
101 err
= ufs_commit_chunk(page
, pos
, len
);
104 dir
->i_mtime
= dir
->i_ctime
= current_time(dir
);
105 mark_inode_dirty(dir
);
109 static bool ufs_check_page(struct page
*page
)
111 struct inode
*dir
= page
->mapping
->host
;
112 struct super_block
*sb
= dir
->i_sb
;
113 char *kaddr
= page_address(page
);
114 unsigned offs
, rec_len
;
115 unsigned limit
= PAGE_SIZE
;
116 const unsigned chunk_mask
= UFS_SB(sb
)->s_uspi
->s_dirblksize
- 1;
117 struct ufs_dir_entry
*p
;
120 if ((dir
->i_size
>> PAGE_SHIFT
) == page
->index
) {
121 limit
= dir
->i_size
& ~PAGE_MASK
;
122 if (limit
& chunk_mask
)
127 for (offs
= 0; offs
<= limit
- UFS_DIR_REC_LEN(1); offs
+= rec_len
) {
128 p
= (struct ufs_dir_entry
*)(kaddr
+ offs
);
129 rec_len
= fs16_to_cpu(sb
, p
->d_reclen
);
131 if (rec_len
< UFS_DIR_REC_LEN(1))
135 if (rec_len
< UFS_DIR_REC_LEN(ufs_get_de_namlen(sb
, p
)))
137 if (((offs
+ rec_len
- 1) ^ offs
) & ~chunk_mask
)
139 if (fs32_to_cpu(sb
, p
->d_ino
) > (UFS_SB(sb
)->s_uspi
->s_ipg
*
140 UFS_SB(sb
)->s_uspi
->s_ncg
))
146 SetPageChecked(page
);
149 /* Too bad, we had an error */
152 ufs_error(sb
, "ufs_check_page",
153 "size of directory #%lu is not a multiple of chunk size",
158 error
= "rec_len is smaller than minimal";
161 error
= "unaligned directory entry";
164 error
= "rec_len is too small for name_len";
167 error
= "directory entry across blocks";
170 error
= "inode out of bounds";
172 ufs_error (sb
, "ufs_check_page", "bad entry in directory #%lu: %s - "
173 "offset=%lu, rec_len=%d, name_len=%d",
174 dir
->i_ino
, error
, (page
->index
<<PAGE_SHIFT
)+offs
,
175 rec_len
, ufs_get_de_namlen(sb
, p
));
178 p
= (struct ufs_dir_entry
*)(kaddr
+ offs
);
179 ufs_error(sb
, __func__
,
180 "entry in directory #%lu spans the page boundary"
182 dir
->i_ino
, (page
->index
<<PAGE_SHIFT
)+offs
);
188 static struct page
*ufs_get_page(struct inode
*dir
, unsigned long n
)
190 struct address_space
*mapping
= dir
->i_mapping
;
191 struct page
*page
= read_mapping_page(mapping
, n
, NULL
);
194 if (unlikely(!PageChecked(page
))) {
195 if (PageError(page
) || !ufs_check_page(page
))
203 return ERR_PTR(-EIO
);
207 * Return the offset into page `page_nr' of the last valid
208 * byte in that page, plus one.
211 ufs_last_byte(struct inode
*inode
, unsigned long page_nr
)
213 unsigned last_byte
= inode
->i_size
;
215 last_byte
-= page_nr
<< PAGE_SHIFT
;
216 if (last_byte
> PAGE_SIZE
)
217 last_byte
= PAGE_SIZE
;
221 static inline struct ufs_dir_entry
*
222 ufs_next_entry(struct super_block
*sb
, struct ufs_dir_entry
*p
)
224 return (struct ufs_dir_entry
*)((char *)p
+
225 fs16_to_cpu(sb
, p
->d_reclen
));
228 struct ufs_dir_entry
*ufs_dotdot(struct inode
*dir
, struct page
**p
)
230 struct page
*page
= ufs_get_page(dir
, 0);
231 struct ufs_dir_entry
*de
= NULL
;
234 de
= ufs_next_entry(dir
->i_sb
,
235 (struct ufs_dir_entry
*)page_address(page
));
244 * finds an entry in the specified directory with the wanted name. It
245 * returns the page in which the entry was found, and the entry itself
246 * (as a parameter - res_dir). Page is returned mapped and unlocked.
247 * Entry is guaranteed to be valid.
249 struct ufs_dir_entry
*ufs_find_entry(struct inode
*dir
, const struct qstr
*qstr
,
250 struct page
**res_page
)
252 struct super_block
*sb
= dir
->i_sb
;
253 const unsigned char *name
= qstr
->name
;
254 int namelen
= qstr
->len
;
255 unsigned reclen
= UFS_DIR_REC_LEN(namelen
);
256 unsigned long start
, n
;
257 unsigned long npages
= dir_pages(dir
);
258 struct page
*page
= NULL
;
259 struct ufs_inode_info
*ui
= UFS_I(dir
);
260 struct ufs_dir_entry
*de
;
262 UFSD("ENTER, dir_ino %lu, name %s, namlen %u\n", dir
->i_ino
, name
, namelen
);
264 if (npages
== 0 || namelen
> UFS_MAXNAMLEN
)
270 start
= ui
->i_dir_start_lookup
;
277 page
= ufs_get_page(dir
, n
);
279 kaddr
= page_address(page
);
280 de
= (struct ufs_dir_entry
*) kaddr
;
281 kaddr
+= ufs_last_byte(dir
, n
) - reclen
;
282 while ((char *) de
<= kaddr
) {
283 if (ufs_match(sb
, namelen
, name
, de
))
285 de
= ufs_next_entry(sb
, de
);
291 } while (n
!= start
);
297 ui
->i_dir_start_lookup
= n
;
304 int ufs_add_link(struct dentry
*dentry
, struct inode
*inode
)
306 struct inode
*dir
= d_inode(dentry
->d_parent
);
307 const unsigned char *name
= dentry
->d_name
.name
;
308 int namelen
= dentry
->d_name
.len
;
309 struct super_block
*sb
= dir
->i_sb
;
310 unsigned reclen
= UFS_DIR_REC_LEN(namelen
);
311 const unsigned int chunk_size
= UFS_SB(sb
)->s_uspi
->s_dirblksize
;
312 unsigned short rec_len
, name_len
;
313 struct page
*page
= NULL
;
314 struct ufs_dir_entry
*de
;
315 unsigned long npages
= dir_pages(dir
);
321 UFSD("ENTER, name %s, namelen %u\n", name
, namelen
);
324 * We take care of directory expansion in the same loop.
325 * This code plays outside i_size, so it locks the page
326 * to protect that region.
328 for (n
= 0; n
<= npages
; n
++) {
331 page
= ufs_get_page(dir
, n
);
336 kaddr
= page_address(page
);
337 dir_end
= kaddr
+ ufs_last_byte(dir
, n
);
338 de
= (struct ufs_dir_entry
*)kaddr
;
339 kaddr
+= PAGE_SIZE
- reclen
;
340 while ((char *)de
<= kaddr
) {
341 if ((char *)de
== dir_end
) {
344 rec_len
= chunk_size
;
345 de
->d_reclen
= cpu_to_fs16(sb
, chunk_size
);
349 if (de
->d_reclen
== 0) {
350 ufs_error(dir
->i_sb
, __func__
,
351 "zero-length directory entry");
356 if (ufs_match(sb
, namelen
, name
, de
))
358 name_len
= UFS_DIR_REC_LEN(ufs_get_de_namlen(sb
, de
));
359 rec_len
= fs16_to_cpu(sb
, de
->d_reclen
);
360 if (!de
->d_ino
&& rec_len
>= reclen
)
362 if (rec_len
>= name_len
+ reclen
)
364 de
= (struct ufs_dir_entry
*) ((char *) de
+ rec_len
);
373 pos
= page_offset(page
) +
374 (char*)de
- (char*)page_address(page
);
375 err
= ufs_prepare_chunk(page
, pos
, rec_len
);
379 struct ufs_dir_entry
*de1
=
380 (struct ufs_dir_entry
*) ((char *) de
+ name_len
);
381 de1
->d_reclen
= cpu_to_fs16(sb
, rec_len
- name_len
);
382 de
->d_reclen
= cpu_to_fs16(sb
, name_len
);
387 ufs_set_de_namlen(sb
, de
, namelen
);
388 memcpy(de
->d_name
, name
, namelen
+ 1);
389 de
->d_ino
= cpu_to_fs32(sb
, inode
->i_ino
);
390 ufs_set_de_type(sb
, de
, inode
->i_mode
);
392 err
= ufs_commit_chunk(page
, pos
, rec_len
);
393 dir
->i_mtime
= dir
->i_ctime
= current_time(dir
);
395 mark_inode_dirty(dir
);
406 static inline unsigned
407 ufs_validate_entry(struct super_block
*sb
, char *base
,
408 unsigned offset
, unsigned mask
)
410 struct ufs_dir_entry
*de
= (struct ufs_dir_entry
*)(base
+ offset
);
411 struct ufs_dir_entry
*p
= (struct ufs_dir_entry
*)(base
+ (offset
&mask
));
412 while ((char*)p
< (char*)de
)
413 p
= ufs_next_entry(sb
, p
);
414 return (char *)p
- base
;
419 * This is blatantly stolen from ext2fs
422 ufs_readdir(struct file
*file
, struct dir_context
*ctx
)
424 loff_t pos
= ctx
->pos
;
425 struct inode
*inode
= file_inode(file
);
426 struct super_block
*sb
= inode
->i_sb
;
427 unsigned int offset
= pos
& ~PAGE_MASK
;
428 unsigned long n
= pos
>> PAGE_SHIFT
;
429 unsigned long npages
= dir_pages(inode
);
430 unsigned chunk_mask
= ~(UFS_SB(sb
)->s_uspi
->s_dirblksize
- 1);
431 int need_revalidate
= file
->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
, __func__
,
449 ctx
->pos
+= PAGE_SIZE
- offset
;
452 kaddr
= page_address(page
);
453 if (unlikely(need_revalidate
)) {
455 offset
= ufs_validate_entry(sb
, kaddr
, offset
, chunk_mask
);
456 ctx
->pos
= (n
<<PAGE_SHIFT
) + offset
;
458 file
->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
)) {
465 unsigned char d_type
= DT_UNKNOWN
;
467 UFSD("filldir(%s,%u)\n", de
->d_name
,
468 fs32_to_cpu(sb
, de
->d_ino
));
469 UFSD("namlen %u\n", ufs_get_de_namlen(sb
, de
));
471 if ((flags
& UFS_DE_MASK
) == UFS_DE_44BSD
)
472 d_type
= de
->d_u
.d_44
.d_type
;
474 if (!dir_emit(ctx
, de
->d_name
,
475 ufs_get_de_namlen(sb
, de
),
476 fs32_to_cpu(sb
, de
->d_ino
),
482 ctx
->pos
+= fs16_to_cpu(sb
, de
->d_reclen
);
491 * ufs_delete_entry deletes a directory entry by merging it with the
494 int ufs_delete_entry(struct inode
*inode
, struct ufs_dir_entry
*dir
,
497 struct super_block
*sb
= inode
->i_sb
;
498 char *kaddr
= page_address(page
);
499 unsigned from
= ((char*)dir
- kaddr
) & ~(UFS_SB(sb
)->s_uspi
->s_dirblksize
- 1);
500 unsigned to
= ((char*)dir
- kaddr
) + fs16_to_cpu(sb
, dir
->d_reclen
);
502 struct ufs_dir_entry
*pde
= NULL
;
503 struct ufs_dir_entry
*de
= (struct ufs_dir_entry
*) (kaddr
+ from
);
508 UFSD("ino %u, reclen %u, namlen %u, name %s\n",
509 fs32_to_cpu(sb
, de
->d_ino
),
510 fs16_to_cpu(sb
, de
->d_reclen
),
511 ufs_get_de_namlen(sb
, de
), de
->d_name
);
513 while ((char*)de
< (char*)dir
) {
514 if (de
->d_reclen
== 0) {
515 ufs_error(inode
->i_sb
, __func__
,
516 "zero-length directory entry");
521 de
= ufs_next_entry(sb
, de
);
524 from
= (char*)pde
- (char*)page_address(page
);
526 pos
= page_offset(page
) + from
;
528 err
= ufs_prepare_chunk(page
, pos
, to
- from
);
531 pde
->d_reclen
= cpu_to_fs16(sb
, to
- from
);
533 err
= ufs_commit_chunk(page
, pos
, to
- from
);
534 inode
->i_ctime
= inode
->i_mtime
= current_time(inode
);
535 mark_inode_dirty(inode
);
542 int ufs_make_empty(struct inode
* inode
, struct inode
*dir
)
544 struct super_block
* sb
= dir
->i_sb
;
545 struct address_space
*mapping
= inode
->i_mapping
;
546 struct page
*page
= grab_cache_page(mapping
, 0);
547 const unsigned int chunk_size
= UFS_SB(sb
)->s_uspi
->s_dirblksize
;
548 struct ufs_dir_entry
* de
;
555 err
= ufs_prepare_chunk(page
, 0, chunk_size
);
562 base
= (char*)page_address(page
);
563 memset(base
, 0, PAGE_SIZE
);
565 de
= (struct ufs_dir_entry
*) base
;
567 de
->d_ino
= cpu_to_fs32(sb
, inode
->i_ino
);
568 ufs_set_de_type(sb
, de
, inode
->i_mode
);
569 ufs_set_de_namlen(sb
, de
, 1);
570 de
->d_reclen
= cpu_to_fs16(sb
, UFS_DIR_REC_LEN(1));
571 strcpy (de
->d_name
, ".");
572 de
= (struct ufs_dir_entry
*)
573 ((char *)de
+ fs16_to_cpu(sb
, de
->d_reclen
));
574 de
->d_ino
= cpu_to_fs32(sb
, dir
->i_ino
);
575 ufs_set_de_type(sb
, de
, dir
->i_mode
);
576 de
->d_reclen
= cpu_to_fs16(sb
, chunk_size
- UFS_DIR_REC_LEN(1));
577 ufs_set_de_namlen(sb
, de
, 2);
578 strcpy (de
->d_name
, "..");
581 err
= ufs_commit_chunk(page
, 0, chunk_size
);
588 * routine to check that the specified directory is empty (for rmdir)
590 int ufs_empty_dir(struct inode
* inode
)
592 struct super_block
*sb
= inode
->i_sb
;
593 struct page
*page
= NULL
;
594 unsigned long i
, npages
= dir_pages(inode
);
596 for (i
= 0; i
< npages
; i
++) {
598 struct ufs_dir_entry
*de
;
599 page
= ufs_get_page(inode
, i
);
604 kaddr
= page_address(page
);
605 de
= (struct ufs_dir_entry
*)kaddr
;
606 kaddr
+= ufs_last_byte(inode
, i
) - UFS_DIR_REC_LEN(1);
608 while ((char *)de
<= kaddr
) {
609 if (de
->d_reclen
== 0) {
610 ufs_error(inode
->i_sb
, __func__
,
611 "zero-length directory entry: "
612 "kaddr=%p, de=%p\n", kaddr
, de
);
616 u16 namelen
=ufs_get_de_namlen(sb
, de
);
617 /* check for . and .. */
618 if (de
->d_name
[0] != '.')
624 fs32_to_cpu(sb
, de
->d_ino
))
626 } else if (de
->d_name
[1] != '.')
629 de
= ufs_next_entry(sb
, de
);
640 const struct file_operations ufs_dir_operations
= {
641 .read
= generic_read_dir
,
642 .iterate_shared
= ufs_readdir
,
643 .fsync
= generic_file_fsync
,
644 .llseek
= generic_file_llseek
,