1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 1991, 1992 Linus Torvalds
7 * minix directory handling functions
9 * Updated to filesystem version 3 by Daniel Aragones
13 #include <linux/buffer_head.h>
14 #include <linux/highmem.h>
15 #include <linux/swap.h>
17 typedef struct minix_dir_entry minix_dirent
;
18 typedef struct minix3_dir_entry minix3_dirent
;
20 static int minix_readdir(struct file
*, struct dir_context
*);
22 const struct file_operations minix_dir_operations
= {
23 .llseek
= generic_file_llseek
,
24 .read
= generic_read_dir
,
25 .iterate_shared
= minix_readdir
,
26 .fsync
= generic_file_fsync
,
30 * Return the offset into page `page_nr' of the last valid
31 * byte in that page, plus one.
34 minix_last_byte(struct inode
*inode
, unsigned long page_nr
)
36 unsigned last_byte
= PAGE_SIZE
;
38 if (page_nr
== (inode
->i_size
>> PAGE_SHIFT
))
39 last_byte
= inode
->i_size
& (PAGE_SIZE
- 1);
43 static void dir_commit_chunk(struct folio
*folio
, loff_t pos
, unsigned len
)
45 struct address_space
*mapping
= folio
->mapping
;
46 struct inode
*dir
= mapping
->host
;
48 block_write_end(NULL
, mapping
, pos
, len
, len
, folio
, NULL
);
50 if (pos
+len
> dir
->i_size
) {
51 i_size_write(dir
, pos
+len
);
52 mark_inode_dirty(dir
);
57 static int minix_handle_dirsync(struct inode
*dir
)
61 err
= filemap_write_and_wait(dir
->i_mapping
);
63 err
= sync_inode_metadata(dir
, 1);
67 static void *dir_get_folio(struct inode
*dir
, unsigned long n
,
68 struct folio
**foliop
)
70 struct folio
*folio
= read_mapping_folio(dir
->i_mapping
, n
, NULL
);
73 return ERR_CAST(folio
);
75 return kmap_local_folio(folio
, 0);
78 static inline void *minix_next_entry(void *de
, struct minix_sb_info
*sbi
)
80 return (void*)((char*)de
+ sbi
->s_dirsize
);
83 static int minix_readdir(struct file
*file
, struct dir_context
*ctx
)
85 struct inode
*inode
= file_inode(file
);
86 struct super_block
*sb
= inode
->i_sb
;
87 struct minix_sb_info
*sbi
= minix_sb(sb
);
88 unsigned chunk_size
= sbi
->s_dirsize
;
89 unsigned long npages
= dir_pages(inode
);
90 unsigned long pos
= ctx
->pos
;
94 ctx
->pos
= pos
= ALIGN(pos
, chunk_size
);
95 if (pos
>= inode
->i_size
)
98 offset
= pos
& ~PAGE_MASK
;
99 n
= pos
>> PAGE_SHIFT
;
101 for ( ; n
< npages
; n
++, offset
= 0) {
102 char *p
, *kaddr
, *limit
;
105 kaddr
= dir_get_folio(inode
, n
, &folio
);
109 limit
= kaddr
+ minix_last_byte(inode
, n
) - chunk_size
;
110 for ( ; p
<= limit
; p
= minix_next_entry(p
, sbi
)) {
113 if (sbi
->s_version
== MINIX_V3
) {
114 minix3_dirent
*de3
= (minix3_dirent
*)p
;
116 inumber
= de3
->inode
;
118 minix_dirent
*de
= (minix_dirent
*)p
;
123 unsigned l
= strnlen(name
, sbi
->s_namelen
);
124 if (!dir_emit(ctx
, name
, l
,
125 inumber
, DT_UNKNOWN
)) {
126 folio_release_kmap(folio
, p
);
130 ctx
->pos
+= chunk_size
;
132 folio_release_kmap(folio
, kaddr
);
137 static inline int namecompare(int len
, int maxlen
,
138 const char * name
, const char * buffer
)
140 if (len
< maxlen
&& buffer
[len
])
142 return !memcmp(name
, buffer
, len
);
148 * finds an entry in the specified directory with the wanted name.
149 * It does NOT read the inode of the
150 * entry - you'll have to do that yourself if you want to.
152 * On Success folio_release_kmap() should be called on *foliop.
154 minix_dirent
*minix_find_entry(struct dentry
*dentry
, struct folio
**foliop
)
156 const char * name
= dentry
->d_name
.name
;
157 int namelen
= dentry
->d_name
.len
;
158 struct inode
* dir
= d_inode(dentry
->d_parent
);
159 struct super_block
* sb
= dir
->i_sb
;
160 struct minix_sb_info
* sbi
= minix_sb(sb
);
162 unsigned long npages
= dir_pages(dir
);
168 for (n
= 0; n
< npages
; n
++) {
171 kaddr
= dir_get_folio(dir
, n
, foliop
);
175 limit
= kaddr
+ minix_last_byte(dir
, n
) - sbi
->s_dirsize
;
176 for (p
= kaddr
; p
<= limit
; p
= minix_next_entry(p
, sbi
)) {
177 if (sbi
->s_version
== MINIX_V3
) {
178 minix3_dirent
*de3
= (minix3_dirent
*)p
;
180 inumber
= de3
->inode
;
182 minix_dirent
*de
= (minix_dirent
*)p
;
188 if (namecompare(namelen
, sbi
->s_namelen
, name
, namx
))
191 folio_release_kmap(*foliop
, kaddr
);
196 return (minix_dirent
*)p
;
199 int minix_add_link(struct dentry
*dentry
, struct inode
*inode
)
201 struct inode
*dir
= d_inode(dentry
->d_parent
);
202 const char * name
= dentry
->d_name
.name
;
203 int namelen
= dentry
->d_name
.len
;
204 struct super_block
* sb
= dir
->i_sb
;
205 struct minix_sb_info
* sbi
= minix_sb(sb
);
206 struct folio
*folio
= NULL
;
207 unsigned long npages
= dir_pages(dir
);
218 * We take care of directory expansion in the same loop
219 * This code plays outside i_size, so it locks the page
220 * to protect that region.
222 for (n
= 0; n
<= npages
; n
++) {
223 char *limit
, *dir_end
;
225 kaddr
= dir_get_folio(dir
, n
, &folio
);
227 return PTR_ERR(kaddr
);
229 dir_end
= kaddr
+ minix_last_byte(dir
, n
);
230 limit
= kaddr
+ PAGE_SIZE
- sbi
->s_dirsize
;
231 for (p
= kaddr
; p
<= limit
; p
= minix_next_entry(p
, sbi
)) {
232 de
= (minix_dirent
*)p
;
233 de3
= (minix3_dirent
*)p
;
234 if (sbi
->s_version
== MINIX_V3
) {
236 inumber
= de3
->inode
;
243 if (sbi
->s_version
== MINIX_V3
)
252 if (namecompare(namelen
, sbi
->s_namelen
, name
, namx
))
256 folio_release_kmap(folio
, kaddr
);
262 pos
= folio_pos(folio
) + offset_in_folio(folio
, p
);
263 err
= minix_prepare_chunk(folio
, pos
, sbi
->s_dirsize
);
266 memcpy (namx
, name
, namelen
);
267 if (sbi
->s_version
== MINIX_V3
) {
268 memset (namx
+ namelen
, 0, sbi
->s_dirsize
- namelen
- 4);
269 de3
->inode
= inode
->i_ino
;
271 memset (namx
+ namelen
, 0, sbi
->s_dirsize
- namelen
- 2);
272 de
->inode
= inode
->i_ino
;
274 dir_commit_chunk(folio
, pos
, sbi
->s_dirsize
);
275 inode_set_mtime_to_ts(dir
, inode_set_ctime_current(dir
));
276 mark_inode_dirty(dir
);
277 err
= minix_handle_dirsync(dir
);
279 folio_release_kmap(folio
, kaddr
);
286 int minix_delete_entry(struct minix_dir_entry
*de
, struct folio
*folio
)
288 struct inode
*inode
= folio
->mapping
->host
;
289 loff_t pos
= folio_pos(folio
) + offset_in_folio(folio
, de
);
290 struct minix_sb_info
*sbi
= minix_sb(inode
->i_sb
);
291 unsigned len
= sbi
->s_dirsize
;
295 err
= minix_prepare_chunk(folio
, pos
, len
);
300 if (sbi
->s_version
== MINIX_V3
)
301 ((minix3_dirent
*)de
)->inode
= 0;
304 dir_commit_chunk(folio
, pos
, len
);
305 inode_set_mtime_to_ts(inode
, inode_set_ctime_current(inode
));
306 mark_inode_dirty(inode
);
307 return minix_handle_dirsync(inode
);
310 int minix_make_empty(struct inode
*inode
, struct inode
*dir
)
312 struct folio
*folio
= filemap_grab_folio(inode
->i_mapping
, 0);
313 struct minix_sb_info
*sbi
= minix_sb(inode
->i_sb
);
318 return PTR_ERR(folio
);
319 err
= minix_prepare_chunk(folio
, 0, 2 * sbi
->s_dirsize
);
325 kaddr
= kmap_local_folio(folio
, 0);
326 memset(kaddr
, 0, folio_size(folio
));
328 if (sbi
->s_version
== MINIX_V3
) {
329 minix3_dirent
*de3
= (minix3_dirent
*)kaddr
;
331 de3
->inode
= inode
->i_ino
;
332 strcpy(de3
->name
, ".");
333 de3
= minix_next_entry(de3
, sbi
);
334 de3
->inode
= dir
->i_ino
;
335 strcpy(de3
->name
, "..");
337 minix_dirent
*de
= (minix_dirent
*)kaddr
;
339 de
->inode
= inode
->i_ino
;
340 strcpy(de
->name
, ".");
341 de
= minix_next_entry(de
, sbi
);
342 de
->inode
= dir
->i_ino
;
343 strcpy(de
->name
, "..");
347 dir_commit_chunk(folio
, 0, 2 * sbi
->s_dirsize
);
348 err
= minix_handle_dirsync(inode
);
355 * routine to check that the specified directory is empty (for rmdir)
357 int minix_empty_dir(struct inode
* inode
)
359 struct folio
*folio
= NULL
;
360 unsigned long i
, npages
= dir_pages(inode
);
361 struct minix_sb_info
*sbi
= minix_sb(inode
->i_sb
);
365 for (i
= 0; i
< npages
; i
++) {
368 kaddr
= dir_get_folio(inode
, i
, &folio
);
372 limit
= kaddr
+ minix_last_byte(inode
, i
) - sbi
->s_dirsize
;
373 for (p
= kaddr
; p
<= limit
; p
= minix_next_entry(p
, sbi
)) {
374 if (sbi
->s_version
== MINIX_V3
) {
375 minix3_dirent
*de3
= (minix3_dirent
*)p
;
377 inumber
= de3
->inode
;
379 minix_dirent
*de
= (minix_dirent
*)p
;
385 /* check for . and .. */
389 if (inumber
!= inode
->i_ino
)
391 } else if (name
[1] != '.')
397 folio_release_kmap(folio
, kaddr
);
402 folio_release_kmap(folio
, kaddr
);
406 /* Releases the page */
407 int minix_set_link(struct minix_dir_entry
*de
, struct folio
*folio
,
410 struct inode
*dir
= folio
->mapping
->host
;
411 struct minix_sb_info
*sbi
= minix_sb(dir
->i_sb
);
412 loff_t pos
= folio_pos(folio
) + offset_in_folio(folio
, de
);
416 err
= minix_prepare_chunk(folio
, pos
, sbi
->s_dirsize
);
421 if (sbi
->s_version
== MINIX_V3
)
422 ((minix3_dirent
*)de
)->inode
= inode
->i_ino
;
424 de
->inode
= inode
->i_ino
;
425 dir_commit_chunk(folio
, pos
, sbi
->s_dirsize
);
426 inode_set_mtime_to_ts(dir
, inode_set_ctime_current(dir
));
427 mark_inode_dirty(dir
);
428 return minix_handle_dirsync(dir
);
431 struct minix_dir_entry
*minix_dotdot(struct inode
*dir
, struct folio
**foliop
)
433 struct minix_sb_info
*sbi
= minix_sb(dir
->i_sb
);
434 struct minix_dir_entry
*de
= dir_get_folio(dir
, 0, foliop
);
437 return minix_next_entry(de
, sbi
);
441 ino_t
minix_inode_by_name(struct dentry
*dentry
)
444 struct minix_dir_entry
*de
= minix_find_entry(dentry
, &folio
);
448 struct inode
*inode
= folio
->mapping
->host
;
449 struct minix_sb_info
*sbi
= minix_sb(inode
->i_sb
);
451 if (sbi
->s_version
== MINIX_V3
)
452 res
= ((minix3_dirent
*) de
)->inode
;
455 folio_release_kmap(folio
, de
);