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
,
29 static inline void dir_put_page(struct page
*page
)
36 * Return the offset into page `page_nr' of the last valid
37 * byte in that page, plus one.
40 minix_last_byte(struct inode
*inode
, unsigned long page_nr
)
42 unsigned last_byte
= PAGE_SIZE
;
44 if (page_nr
== (inode
->i_size
>> PAGE_SHIFT
))
45 last_byte
= inode
->i_size
& (PAGE_SIZE
- 1);
49 static int dir_commit_chunk(struct page
*page
, loff_t pos
, unsigned len
)
51 struct address_space
*mapping
= page
->mapping
;
52 struct inode
*dir
= mapping
->host
;
54 block_write_end(NULL
, mapping
, pos
, len
, len
, page
, NULL
);
56 if (pos
+len
> dir
->i_size
) {
57 i_size_write(dir
, pos
+len
);
58 mark_inode_dirty(dir
);
61 err
= write_one_page(page
);
67 static struct page
* dir_get_page(struct inode
*dir
, unsigned long n
)
69 struct address_space
*mapping
= dir
->i_mapping
;
70 struct page
*page
= read_mapping_page(mapping
, n
, NULL
);
76 static inline void *minix_next_entry(void *de
, struct minix_sb_info
*sbi
)
78 return (void*)((char*)de
+ sbi
->s_dirsize
);
81 static int minix_readdir(struct file
*file
, struct dir_context
*ctx
)
83 struct inode
*inode
= file_inode(file
);
84 struct super_block
*sb
= inode
->i_sb
;
85 struct minix_sb_info
*sbi
= minix_sb(sb
);
86 unsigned chunk_size
= sbi
->s_dirsize
;
87 unsigned long npages
= dir_pages(inode
);
88 unsigned long pos
= ctx
->pos
;
92 ctx
->pos
= pos
= ALIGN(pos
, chunk_size
);
93 if (pos
>= inode
->i_size
)
96 offset
= pos
& ~PAGE_MASK
;
97 n
= pos
>> PAGE_SHIFT
;
99 for ( ; n
< npages
; n
++, offset
= 0) {
100 char *p
, *kaddr
, *limit
;
101 struct page
*page
= dir_get_page(inode
, n
);
105 kaddr
= (char *)page_address(page
);
107 limit
= kaddr
+ minix_last_byte(inode
, n
) - chunk_size
;
108 for ( ; p
<= limit
; p
= minix_next_entry(p
, sbi
)) {
111 if (sbi
->s_version
== MINIX_V3
) {
112 minix3_dirent
*de3
= (minix3_dirent
*)p
;
114 inumber
= de3
->inode
;
116 minix_dirent
*de
= (minix_dirent
*)p
;
121 unsigned l
= strnlen(name
, sbi
->s_namelen
);
122 if (!dir_emit(ctx
, name
, l
,
123 inumber
, DT_UNKNOWN
)) {
128 ctx
->pos
+= chunk_size
;
135 static inline int namecompare(int len
, int maxlen
,
136 const char * name
, const char * buffer
)
138 if (len
< maxlen
&& buffer
[len
])
140 return !memcmp(name
, buffer
, len
);
146 * finds an entry in the specified directory with the wanted name. It
147 * returns the cache buffer in which the entry was found, and the entry
148 * itself (as a parameter - res_dir). It does NOT read the inode of the
149 * entry - you'll have to do that yourself if you want to.
151 minix_dirent
*minix_find_entry(struct dentry
*dentry
, struct page
**res_page
)
153 const char * name
= dentry
->d_name
.name
;
154 int namelen
= dentry
->d_name
.len
;
155 struct inode
* dir
= d_inode(dentry
->d_parent
);
156 struct super_block
* sb
= dir
->i_sb
;
157 struct minix_sb_info
* sbi
= minix_sb(sb
);
159 unsigned long npages
= dir_pages(dir
);
160 struct page
*page
= NULL
;
167 for (n
= 0; n
< npages
; n
++) {
170 page
= dir_get_page(dir
, n
);
174 kaddr
= (char*)page_address(page
);
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
))
197 return (minix_dirent
*)p
;
200 int minix_add_link(struct dentry
*dentry
, struct inode
*inode
)
202 struct inode
*dir
= d_inode(dentry
->d_parent
);
203 const char * name
= dentry
->d_name
.name
;
204 int namelen
= dentry
->d_name
.len
;
205 struct super_block
* sb
= dir
->i_sb
;
206 struct minix_sb_info
* sbi
= minix_sb(sb
);
207 struct page
*page
= NULL
;
208 unsigned long npages
= dir_pages(dir
);
219 * We take care of directory expansion in the same loop
220 * This code plays outside i_size, so it locks the page
221 * to protect that region.
223 for (n
= 0; n
<= npages
; n
++) {
224 char *limit
, *dir_end
;
226 page
= dir_get_page(dir
, n
);
231 kaddr
= (char*)page_address(page
);
232 dir_end
= kaddr
+ minix_last_byte(dir
, n
);
233 limit
= kaddr
+ PAGE_SIZE
- sbi
->s_dirsize
;
234 for (p
= kaddr
; p
<= limit
; p
= minix_next_entry(p
, sbi
)) {
235 de
= (minix_dirent
*)p
;
236 de3
= (minix3_dirent
*)p
;
237 if (sbi
->s_version
== MINIX_V3
) {
239 inumber
= de3
->inode
;
246 if (sbi
->s_version
== MINIX_V3
)
255 if (namecompare(namelen
, sbi
->s_namelen
, name
, namx
))
265 pos
= page_offset(page
) + p
- (char *)page_address(page
);
266 err
= minix_prepare_chunk(page
, pos
, sbi
->s_dirsize
);
269 memcpy (namx
, name
, namelen
);
270 if (sbi
->s_version
== MINIX_V3
) {
271 memset (namx
+ namelen
, 0, sbi
->s_dirsize
- namelen
- 4);
272 de3
->inode
= inode
->i_ino
;
274 memset (namx
+ namelen
, 0, sbi
->s_dirsize
- namelen
- 2);
275 de
->inode
= inode
->i_ino
;
277 err
= dir_commit_chunk(page
, pos
, sbi
->s_dirsize
);
278 dir
->i_mtime
= dir
->i_ctime
= current_time(dir
);
279 mark_inode_dirty(dir
);
289 int minix_delete_entry(struct minix_dir_entry
*de
, struct page
*page
)
291 struct inode
*inode
= page
->mapping
->host
;
292 char *kaddr
= page_address(page
);
293 loff_t pos
= page_offset(page
) + (char*)de
- kaddr
;
294 struct minix_sb_info
*sbi
= minix_sb(inode
->i_sb
);
295 unsigned len
= sbi
->s_dirsize
;
299 err
= minix_prepare_chunk(page
, pos
, len
);
301 if (sbi
->s_version
== MINIX_V3
)
302 ((minix3_dirent
*) de
)->inode
= 0;
305 err
= dir_commit_chunk(page
, pos
, len
);
310 inode
->i_ctime
= inode
->i_mtime
= current_time(inode
);
311 mark_inode_dirty(inode
);
315 int minix_make_empty(struct inode
*inode
, struct inode
*dir
)
317 struct page
*page
= grab_cache_page(inode
->i_mapping
, 0);
318 struct minix_sb_info
*sbi
= minix_sb(inode
->i_sb
);
324 err
= minix_prepare_chunk(page
, 0, 2 * sbi
->s_dirsize
);
330 kaddr
= kmap_atomic(page
);
331 memset(kaddr
, 0, PAGE_SIZE
);
333 if (sbi
->s_version
== MINIX_V3
) {
334 minix3_dirent
*de3
= (minix3_dirent
*)kaddr
;
336 de3
->inode
= inode
->i_ino
;
337 strcpy(de3
->name
, ".");
338 de3
= minix_next_entry(de3
, sbi
);
339 de3
->inode
= dir
->i_ino
;
340 strcpy(de3
->name
, "..");
342 minix_dirent
*de
= (minix_dirent
*)kaddr
;
344 de
->inode
= inode
->i_ino
;
345 strcpy(de
->name
, ".");
346 de
= minix_next_entry(de
, sbi
);
347 de
->inode
= dir
->i_ino
;
348 strcpy(de
->name
, "..");
350 kunmap_atomic(kaddr
);
352 err
= dir_commit_chunk(page
, 0, 2 * sbi
->s_dirsize
);
359 * routine to check that the specified directory is empty (for rmdir)
361 int minix_empty_dir(struct inode
* inode
)
363 struct page
*page
= NULL
;
364 unsigned long i
, npages
= dir_pages(inode
);
365 struct minix_sb_info
*sbi
= minix_sb(inode
->i_sb
);
369 for (i
= 0; i
< npages
; i
++) {
370 char *p
, *kaddr
, *limit
;
372 page
= dir_get_page(inode
, i
);
376 kaddr
= (char *)page_address(page
);
377 limit
= kaddr
+ minix_last_byte(inode
, i
) - sbi
->s_dirsize
;
378 for (p
= kaddr
; p
<= limit
; p
= minix_next_entry(p
, sbi
)) {
379 if (sbi
->s_version
== MINIX_V3
) {
380 minix3_dirent
*de3
= (minix3_dirent
*)p
;
382 inumber
= de3
->inode
;
384 minix_dirent
*de
= (minix_dirent
*)p
;
390 /* check for . and .. */
394 if (inumber
!= inode
->i_ino
)
396 } else if (name
[1] != '.')
411 /* Releases the page */
412 void minix_set_link(struct minix_dir_entry
*de
, struct page
*page
,
415 struct inode
*dir
= page
->mapping
->host
;
416 struct minix_sb_info
*sbi
= minix_sb(dir
->i_sb
);
417 loff_t pos
= page_offset(page
) +
418 (char *)de
-(char*)page_address(page
);
423 err
= minix_prepare_chunk(page
, pos
, sbi
->s_dirsize
);
425 if (sbi
->s_version
== MINIX_V3
)
426 ((minix3_dirent
*) de
)->inode
= inode
->i_ino
;
428 de
->inode
= inode
->i_ino
;
429 err
= dir_commit_chunk(page
, pos
, sbi
->s_dirsize
);
434 dir
->i_mtime
= dir
->i_ctime
= current_time(dir
);
435 mark_inode_dirty(dir
);
438 struct minix_dir_entry
* minix_dotdot (struct inode
*dir
, struct page
**p
)
440 struct page
*page
= dir_get_page(dir
, 0);
441 struct minix_sb_info
*sbi
= minix_sb(dir
->i_sb
);
442 struct minix_dir_entry
*de
= NULL
;
445 de
= minix_next_entry(page_address(page
), sbi
);
451 ino_t
minix_inode_by_name(struct dentry
*dentry
)
454 struct minix_dir_entry
*de
= minix_find_entry(dentry
, &page
);
458 struct address_space
*mapping
= page
->mapping
;
459 struct inode
*inode
= mapping
->host
;
460 struct minix_sb_info
*sbi
= minix_sb(inode
->i_sb
);
462 if (sbi
->s_version
== MINIX_V3
)
463 res
= ((minix3_dirent
*) de
)->inode
;