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>
23 #include <linux/iversion.h>
31 * NOTE! unlike strncmp, ufs_match returns 1 for success, 0 for failure.
33 * len <= UFS_MAXNAMLEN and de != NULL are guaranteed by caller.
35 static inline int ufs_match(struct super_block
*sb
, int len
,
36 const unsigned char *name
, struct ufs_dir_entry
*de
)
38 if (len
!= ufs_get_de_namlen(sb
, de
))
42 return !memcmp(name
, de
->d_name
, len
);
45 static void ufs_commit_chunk(struct folio
*folio
, loff_t pos
, unsigned len
)
47 struct address_space
*mapping
= folio
->mapping
;
48 struct inode
*dir
= mapping
->host
;
50 inode_inc_iversion(dir
);
51 block_write_end(NULL
, mapping
, pos
, len
, len
, folio
, NULL
);
52 if (pos
+len
> dir
->i_size
) {
53 i_size_write(dir
, pos
+len
);
54 mark_inode_dirty(dir
);
59 static int ufs_handle_dirsync(struct inode
*dir
)
63 err
= filemap_write_and_wait(dir
->i_mapping
);
65 err
= sync_inode_metadata(dir
, 1);
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
, &folio
);
77 res
= fs32_to_cpu(dir
->i_sb
, de
->d_ino
);
78 folio_release_kmap(folio
, de
);
84 int ufs_set_link(struct inode
*dir
, struct ufs_dir_entry
*de
,
85 struct folio
*folio
, struct inode
*inode
,
88 loff_t pos
= folio_pos(folio
) + offset_in_folio(folio
, de
);
89 unsigned len
= fs16_to_cpu(dir
->i_sb
, de
->d_reclen
);
93 err
= ufs_prepare_chunk(folio
, pos
, len
);
99 de
->d_ino
= cpu_to_fs32(dir
->i_sb
, inode
->i_ino
);
100 ufs_set_de_type(dir
->i_sb
, de
, inode
->i_mode
);
102 ufs_commit_chunk(folio
, pos
, len
);
104 inode_set_mtime_to_ts(dir
, inode_set_ctime_current(dir
));
105 mark_inode_dirty(dir
);
106 return ufs_handle_dirsync(dir
);
109 static bool ufs_check_folio(struct folio
*folio
, char *kaddr
)
111 struct inode
*dir
= folio
->mapping
->host
;
112 struct super_block
*sb
= dir
->i_sb
;
113 unsigned offs
, rec_len
;
114 unsigned limit
= folio_size(folio
);
115 const unsigned chunk_mask
= UFS_SB(sb
)->s_uspi
->s_dirblksize
- 1;
116 struct ufs_dir_entry
*p
;
119 if (dir
->i_size
< folio_pos(folio
) + limit
) {
120 limit
= offset_in_folio(folio
, dir
->i_size
);
121 if (limit
& chunk_mask
)
126 for (offs
= 0; offs
<= limit
- UFS_DIR_REC_LEN(1); offs
+= rec_len
) {
127 p
= (struct ufs_dir_entry
*)(kaddr
+ offs
);
128 rec_len
= fs16_to_cpu(sb
, p
->d_reclen
);
130 if (rec_len
< UFS_DIR_REC_LEN(1))
134 if (rec_len
< UFS_DIR_REC_LEN(ufs_get_de_namlen(sb
, p
)))
136 if (((offs
+ rec_len
- 1) ^ offs
) & ~chunk_mask
)
138 if (fs32_to_cpu(sb
, p
->d_ino
) > (UFS_SB(sb
)->s_uspi
->s_ipg
*
139 UFS_SB(sb
)->s_uspi
->s_ncg
))
145 folio_set_checked(folio
);
148 /* Too bad, we had an error */
151 ufs_error(sb
, __func__
,
152 "size of directory #%lu is not a multiple of chunk size",
157 error
= "rec_len is smaller than minimal";
160 error
= "unaligned directory entry";
163 error
= "rec_len is too small for name_len";
166 error
= "directory entry across blocks";
169 error
= "inode out of bounds";
171 ufs_error(sb
, __func__
, "bad entry in directory #%lu: %s - "
172 "offset=%llu, rec_len=%d, name_len=%d",
173 dir
->i_ino
, error
, folio_pos(folio
) + offs
,
174 rec_len
, ufs_get_de_namlen(sb
, p
));
177 p
= (struct ufs_dir_entry
*)(kaddr
+ offs
);
178 ufs_error(sb
, __func__
,
179 "entry in directory #%lu spans the page boundary"
181 dir
->i_ino
, folio_pos(folio
) + offs
);
186 static void *ufs_get_folio(struct inode
*dir
, unsigned long n
,
187 struct folio
**foliop
)
189 struct address_space
*mapping
= dir
->i_mapping
;
190 struct folio
*folio
= read_mapping_folio(mapping
, n
, NULL
);
194 return ERR_CAST(folio
);
195 kaddr
= kmap_local_folio(folio
, 0);
196 if (unlikely(!folio_test_checked(folio
))) {
197 if (!ufs_check_folio(folio
, kaddr
))
204 folio_release_kmap(folio
, kaddr
);
205 return ERR_PTR(-EIO
);
209 * Return the offset into page `page_nr' of the last valid
210 * byte in that page, plus one.
213 ufs_last_byte(struct inode
*inode
, unsigned long page_nr
)
215 unsigned last_byte
= inode
->i_size
;
217 last_byte
-= page_nr
<< PAGE_SHIFT
;
218 if (last_byte
> PAGE_SIZE
)
219 last_byte
= PAGE_SIZE
;
223 static inline struct ufs_dir_entry
*
224 ufs_next_entry(struct super_block
*sb
, struct ufs_dir_entry
*p
)
226 return (struct ufs_dir_entry
*)((char *)p
+
227 fs16_to_cpu(sb
, p
->d_reclen
));
230 struct ufs_dir_entry
*ufs_dotdot(struct inode
*dir
, struct folio
**foliop
)
232 struct ufs_dir_entry
*de
= ufs_get_folio(dir
, 0, foliop
);
235 return ufs_next_entry(dir
->i_sb
, de
);
243 * finds an entry in the specified directory with the wanted name. It
244 * returns the page in which the entry was found, and the entry itself
245 * (as a parameter - res_dir). Page is returned mapped and unlocked.
246 * Entry is guaranteed to be valid.
248 struct ufs_dir_entry
*ufs_find_entry(struct inode
*dir
, const struct qstr
*qstr
,
249 struct folio
**foliop
)
251 struct super_block
*sb
= dir
->i_sb
;
252 const unsigned char *name
= qstr
->name
;
253 int namelen
= qstr
->len
;
254 unsigned reclen
= UFS_DIR_REC_LEN(namelen
);
255 unsigned long start
, n
;
256 unsigned long npages
= dir_pages(dir
);
257 struct ufs_inode_info
*ui
= UFS_I(dir
);
258 struct ufs_dir_entry
*de
;
260 UFSD("ENTER, dir_ino %lu, name %s, namlen %u\n", dir
->i_ino
, name
, namelen
);
262 if (npages
== 0 || namelen
> UFS_MAXNAMLEN
)
265 start
= ui
->i_dir_start_lookup
;
271 char *kaddr
= ufs_get_folio(dir
, n
, foliop
);
273 if (!IS_ERR(kaddr
)) {
274 de
= (struct ufs_dir_entry
*)kaddr
;
275 kaddr
+= ufs_last_byte(dir
, n
) - reclen
;
276 while ((char *) de
<= kaddr
) {
277 if (ufs_match(sb
, namelen
, name
, de
))
279 de
= ufs_next_entry(sb
, de
);
281 folio_release_kmap(*foliop
, kaddr
);
285 } while (n
!= start
);
290 ui
->i_dir_start_lookup
= n
;
297 int ufs_add_link(struct dentry
*dentry
, struct inode
*inode
)
299 struct inode
*dir
= d_inode(dentry
->d_parent
);
300 const unsigned char *name
= dentry
->d_name
.name
;
301 int namelen
= dentry
->d_name
.len
;
302 struct super_block
*sb
= dir
->i_sb
;
303 unsigned reclen
= UFS_DIR_REC_LEN(namelen
);
304 const unsigned int chunk_size
= UFS_SB(sb
)->s_uspi
->s_dirblksize
;
305 unsigned short rec_len
, name_len
;
306 struct folio
*folio
= NULL
;
307 struct ufs_dir_entry
*de
;
308 unsigned long npages
= dir_pages(dir
);
313 UFSD("ENTER, name %s, namelen %u\n", name
, namelen
);
316 * We take care of directory expansion in the same loop.
317 * This code plays outside i_size, so it locks the folio
318 * to protect that region.
320 for (n
= 0; n
<= npages
; n
++) {
321 char *kaddr
= ufs_get_folio(dir
, n
, &folio
);
325 return PTR_ERR(kaddr
);
327 dir_end
= kaddr
+ ufs_last_byte(dir
, n
);
328 de
= (struct ufs_dir_entry
*)kaddr
;
329 kaddr
+= folio_size(folio
) - reclen
;
330 while ((char *)de
<= kaddr
) {
331 if ((char *)de
== dir_end
) {
334 rec_len
= chunk_size
;
335 de
->d_reclen
= cpu_to_fs16(sb
, chunk_size
);
339 if (de
->d_reclen
== 0) {
340 ufs_error(dir
->i_sb
, __func__
,
341 "zero-length directory entry");
346 if (ufs_match(sb
, namelen
, name
, de
))
348 name_len
= UFS_DIR_REC_LEN(ufs_get_de_namlen(sb
, de
));
349 rec_len
= fs16_to_cpu(sb
, de
->d_reclen
);
350 if (!de
->d_ino
&& rec_len
>= reclen
)
352 if (rec_len
>= name_len
+ reclen
)
354 de
= (struct ufs_dir_entry
*) ((char *) de
+ rec_len
);
357 folio_release_kmap(folio
, kaddr
);
363 pos
= folio_pos(folio
) + offset_in_folio(folio
, de
);
364 err
= ufs_prepare_chunk(folio
, pos
, rec_len
);
368 struct ufs_dir_entry
*de1
=
369 (struct ufs_dir_entry
*) ((char *) de
+ name_len
);
370 de1
->d_reclen
= cpu_to_fs16(sb
, rec_len
- name_len
);
371 de
->d_reclen
= cpu_to_fs16(sb
, name_len
);
376 ufs_set_de_namlen(sb
, de
, namelen
);
377 memcpy(de
->d_name
, name
, namelen
+ 1);
378 de
->d_ino
= cpu_to_fs32(sb
, inode
->i_ino
);
379 ufs_set_de_type(sb
, de
, inode
->i_mode
);
381 ufs_commit_chunk(folio
, pos
, rec_len
);
382 inode_set_mtime_to_ts(dir
, inode_set_ctime_current(dir
));
384 mark_inode_dirty(dir
);
385 err
= ufs_handle_dirsync(dir
);
388 folio_release_kmap(folio
, de
);
395 static inline unsigned
396 ufs_validate_entry(struct super_block
*sb
, char *base
,
397 unsigned offset
, unsigned mask
)
399 struct ufs_dir_entry
*de
= (struct ufs_dir_entry
*)(base
+ offset
);
400 struct ufs_dir_entry
*p
= (struct ufs_dir_entry
*)(base
+ (offset
&mask
));
401 while ((char*)p
< (char*)de
)
402 p
= ufs_next_entry(sb
, p
);
403 return (char *)p
- base
;
408 * This is blatantly stolen from ext2fs
411 ufs_readdir(struct file
*file
, struct dir_context
*ctx
)
413 loff_t pos
= ctx
->pos
;
414 struct inode
*inode
= file_inode(file
);
415 struct super_block
*sb
= inode
->i_sb
;
416 unsigned int offset
= pos
& ~PAGE_MASK
;
417 unsigned long n
= pos
>> PAGE_SHIFT
;
418 unsigned long npages
= dir_pages(inode
);
419 unsigned chunk_mask
= ~(UFS_SB(sb
)->s_uspi
->s_dirblksize
- 1);
420 bool need_revalidate
= !inode_eq_iversion(inode
, *(u64
*)file
->private_data
);
421 unsigned flags
= UFS_SB(sb
)->s_flags
;
425 if (pos
> inode
->i_size
- UFS_DIR_REC_LEN(1))
428 for ( ; n
< npages
; n
++, offset
= 0) {
429 struct ufs_dir_entry
*de
;
431 char *kaddr
= ufs_get_folio(inode
, n
, &folio
);
435 ufs_error(sb
, __func__
,
438 ctx
->pos
+= PAGE_SIZE
- offset
;
439 return PTR_ERR(kaddr
);
441 if (unlikely(need_revalidate
)) {
443 offset
= ufs_validate_entry(sb
, kaddr
, offset
, chunk_mask
);
444 ctx
->pos
= (n
<<PAGE_SHIFT
) + offset
;
446 *(u64
*)file
->private_data
= inode_query_iversion(inode
);
447 need_revalidate
= false;
449 de
= (struct ufs_dir_entry
*)(kaddr
+offset
);
450 limit
= kaddr
+ ufs_last_byte(inode
, n
) - UFS_DIR_REC_LEN(1);
451 for ( ;(char*)de
<= limit
; de
= ufs_next_entry(sb
, de
)) {
453 unsigned char d_type
= DT_UNKNOWN
;
455 UFSD("filldir(%s,%u)\n", de
->d_name
,
456 fs32_to_cpu(sb
, de
->d_ino
));
457 UFSD("namlen %u\n", ufs_get_de_namlen(sb
, de
));
459 if ((flags
& UFS_DE_MASK
) == UFS_DE_44BSD
)
460 d_type
= de
->d_u
.d_44
.d_type
;
462 if (!dir_emit(ctx
, de
->d_name
,
463 ufs_get_de_namlen(sb
, de
),
464 fs32_to_cpu(sb
, de
->d_ino
),
466 folio_release_kmap(folio
, de
);
470 ctx
->pos
+= fs16_to_cpu(sb
, de
->d_reclen
);
472 folio_release_kmap(folio
, kaddr
);
479 * ufs_delete_entry deletes a directory entry by merging it with the
482 int ufs_delete_entry(struct inode
*inode
, struct ufs_dir_entry
*dir
,
485 struct super_block
*sb
= inode
->i_sb
;
489 struct ufs_dir_entry
*de
, *pde
= NULL
;
494 from
= offset_in_folio(folio
, dir
);
495 to
= from
+ fs16_to_cpu(sb
, dir
->d_reclen
);
496 kaddr
= (char *)dir
- from
;
497 from
&= ~(UFS_SB(sb
)->s_uspi
->s_dirblksize
- 1);
498 de
= (struct ufs_dir_entry
*) (kaddr
+ from
);
500 UFSD("ino %u, reclen %u, namlen %u, name %s\n",
501 fs32_to_cpu(sb
, de
->d_ino
),
502 fs16_to_cpu(sb
, de
->d_reclen
),
503 ufs_get_de_namlen(sb
, de
), de
->d_name
);
505 while ((char*)de
< (char*)dir
) {
506 if (de
->d_reclen
== 0) {
507 ufs_error(inode
->i_sb
, __func__
,
508 "zero-length directory entry");
512 de
= ufs_next_entry(sb
, de
);
515 from
= offset_in_folio(folio
, pde
);
516 pos
= folio_pos(folio
) + from
;
518 err
= ufs_prepare_chunk(folio
, pos
, to
- from
);
524 pde
->d_reclen
= cpu_to_fs16(sb
, to
- from
);
526 ufs_commit_chunk(folio
, pos
, to
- from
);
527 inode_set_mtime_to_ts(inode
, inode_set_ctime_current(inode
));
528 mark_inode_dirty(inode
);
529 return ufs_handle_dirsync(inode
);
532 int ufs_make_empty(struct inode
* inode
, struct inode
*dir
)
534 struct super_block
* sb
= dir
->i_sb
;
535 struct address_space
*mapping
= inode
->i_mapping
;
536 struct folio
*folio
= filemap_grab_folio(mapping
, 0);
537 const unsigned int chunk_size
= UFS_SB(sb
)->s_uspi
->s_dirblksize
;
538 struct ufs_dir_entry
* de
;
543 return PTR_ERR(folio
);
545 err
= ufs_prepare_chunk(folio
, 0, chunk_size
);
551 kaddr
= kmap_local_folio(folio
, 0);
552 memset(kaddr
, 0, folio_size(folio
));
554 de
= (struct ufs_dir_entry
*)kaddr
;
556 de
->d_ino
= cpu_to_fs32(sb
, inode
->i_ino
);
557 ufs_set_de_type(sb
, de
, inode
->i_mode
);
558 ufs_set_de_namlen(sb
, de
, 1);
559 de
->d_reclen
= cpu_to_fs16(sb
, UFS_DIR_REC_LEN(1));
560 strcpy (de
->d_name
, ".");
561 de
= (struct ufs_dir_entry
*)
562 ((char *)de
+ fs16_to_cpu(sb
, de
->d_reclen
));
563 de
->d_ino
= cpu_to_fs32(sb
, dir
->i_ino
);
564 ufs_set_de_type(sb
, de
, dir
->i_mode
);
565 de
->d_reclen
= cpu_to_fs16(sb
, chunk_size
- UFS_DIR_REC_LEN(1));
566 ufs_set_de_namlen(sb
, de
, 2);
567 strcpy (de
->d_name
, "..");
570 ufs_commit_chunk(folio
, 0, chunk_size
);
571 err
= ufs_handle_dirsync(inode
);
578 * routine to check that the specified directory is empty (for rmdir)
580 int ufs_empty_dir(struct inode
* inode
)
582 struct super_block
*sb
= inode
->i_sb
;
585 unsigned long i
, npages
= dir_pages(inode
);
587 for (i
= 0; i
< npages
; i
++) {
588 struct ufs_dir_entry
*de
;
590 kaddr
= ufs_get_folio(inode
, i
, &folio
);
594 de
= (struct ufs_dir_entry
*)kaddr
;
595 kaddr
+= ufs_last_byte(inode
, i
) - UFS_DIR_REC_LEN(1);
597 while ((char *)de
<= kaddr
) {
598 if (de
->d_reclen
== 0) {
599 ufs_error(inode
->i_sb
, __func__
,
600 "zero-length directory entry: "
601 "kaddr=%p, de=%p\n", kaddr
, de
);
605 u16 namelen
=ufs_get_de_namlen(sb
, de
);
606 /* check for . and .. */
607 if (de
->d_name
[0] != '.')
613 fs32_to_cpu(sb
, de
->d_ino
))
615 } else if (de
->d_name
[1] != '.')
618 de
= ufs_next_entry(sb
, de
);
620 folio_release_kmap(folio
, kaddr
);
625 folio_release_kmap(folio
, kaddr
);
629 static int ufs_dir_open(struct inode
*inode
, struct file
*file
)
631 file
->private_data
= kzalloc(sizeof(u64
), GFP_KERNEL
);
632 if (!file
->private_data
)
637 static int ufs_dir_release(struct inode
*inode
, struct file
*file
)
639 kfree(file
->private_data
);
643 static loff_t
ufs_dir_llseek(struct file
*file
, loff_t offset
, int whence
)
645 return generic_llseek_cookie(file
, offset
, whence
,
646 (u64
*)file
->private_data
);
649 const struct file_operations ufs_dir_operations
= {
650 .open
= ufs_dir_open
,
651 .release
= ufs_dir_release
,
652 .read
= generic_read_dir
,
653 .iterate_shared
= ufs_readdir
,
654 .fsync
= generic_file_fsync
,
655 .llseek
= ufs_dir_llseek
,