4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * minix directory handling functions
8 * Updated to filesystem version 3 by Daniel Aragones
12 #include <linux/buffer_head.h>
13 #include <linux/highmem.h>
14 #include <linux/swap.h>
16 typedef struct minix_dir_entry minix_dirent
;
17 typedef struct minix3_dir_entry minix3_dirent
;
19 static int minix_readdir(struct file
*, struct dir_context
*);
21 const struct file_operations minix_dir_operations
= {
22 .llseek
= generic_file_llseek
,
23 .read
= generic_read_dir
,
24 .iterate
= minix_readdir
,
25 .fsync
= generic_file_fsync
,
28 static inline void dir_put_page(struct page
*page
)
31 page_cache_release(page
);
35 * Return the offset into page `page_nr' of the last valid
36 * byte in that page, plus one.
39 minix_last_byte(struct inode
*inode
, unsigned long page_nr
)
41 unsigned last_byte
= PAGE_CACHE_SIZE
;
43 if (page_nr
== (inode
->i_size
>> PAGE_CACHE_SHIFT
))
44 last_byte
= inode
->i_size
& (PAGE_CACHE_SIZE
- 1);
48 static inline unsigned long dir_pages(struct inode
*inode
)
50 return (inode
->i_size
+PAGE_CACHE_SIZE
-1)>>PAGE_CACHE_SHIFT
;
53 static int dir_commit_chunk(struct page
*page
, loff_t pos
, unsigned len
)
55 struct address_space
*mapping
= page
->mapping
;
56 struct inode
*dir
= mapping
->host
;
58 block_write_end(NULL
, mapping
, pos
, len
, len
, page
, NULL
);
60 if (pos
+len
> dir
->i_size
) {
61 i_size_write(dir
, pos
+len
);
62 mark_inode_dirty(dir
);
65 err
= write_one_page(page
, 1);
71 static struct page
* dir_get_page(struct inode
*dir
, unsigned long n
)
73 struct address_space
*mapping
= dir
->i_mapping
;
74 struct page
*page
= read_mapping_page(mapping
, n
, NULL
);
80 static inline void *minix_next_entry(void *de
, struct minix_sb_info
*sbi
)
82 return (void*)((char*)de
+ sbi
->s_dirsize
);
85 static int minix_readdir(struct file
*file
, struct dir_context
*ctx
)
87 struct inode
*inode
= file_inode(file
);
88 struct super_block
*sb
= inode
->i_sb
;
89 struct minix_sb_info
*sbi
= minix_sb(sb
);
90 unsigned chunk_size
= sbi
->s_dirsize
;
91 unsigned long npages
= dir_pages(inode
);
92 unsigned long pos
= ctx
->pos
;
96 ctx
->pos
= pos
= ALIGN(pos
, chunk_size
);
97 if (pos
>= inode
->i_size
)
100 offset
= pos
& ~PAGE_CACHE_MASK
;
101 n
= pos
>> PAGE_CACHE_SHIFT
;
103 for ( ; n
< npages
; n
++, offset
= 0) {
104 char *p
, *kaddr
, *limit
;
105 struct page
*page
= dir_get_page(inode
, n
);
109 kaddr
= (char *)page_address(page
);
111 limit
= kaddr
+ minix_last_byte(inode
, n
) - chunk_size
;
112 for ( ; p
<= limit
; p
= minix_next_entry(p
, sbi
)) {
115 if (sbi
->s_version
== MINIX_V3
) {
116 minix3_dirent
*de3
= (minix3_dirent
*)p
;
118 inumber
= de3
->inode
;
120 minix_dirent
*de
= (minix_dirent
*)p
;
125 unsigned l
= strnlen(name
, sbi
->s_namelen
);
126 if (!dir_emit(ctx
, name
, l
,
127 inumber
, DT_UNKNOWN
)) {
132 ctx
->pos
+= chunk_size
;
139 static inline int namecompare(int len
, int maxlen
,
140 const char * name
, const char * buffer
)
142 if (len
< maxlen
&& buffer
[len
])
144 return !memcmp(name
, buffer
, len
);
150 * finds an entry in the specified directory with the wanted name. It
151 * returns the cache buffer in which the entry was found, and the entry
152 * itself (as a parameter - res_dir). It does NOT read the inode of the
153 * entry - you'll have to do that yourself if you want to.
155 minix_dirent
*minix_find_entry(struct dentry
*dentry
, struct page
**res_page
)
157 const char * name
= dentry
->d_name
.name
;
158 int namelen
= dentry
->d_name
.len
;
159 struct inode
* dir
= dentry
->d_parent
->d_inode
;
160 struct super_block
* sb
= dir
->i_sb
;
161 struct minix_sb_info
* sbi
= minix_sb(sb
);
163 unsigned long npages
= dir_pages(dir
);
164 struct page
*page
= NULL
;
171 for (n
= 0; n
< npages
; n
++) {
174 page
= dir_get_page(dir
, n
);
178 kaddr
= (char*)page_address(page
);
179 limit
= kaddr
+ minix_last_byte(dir
, n
) - sbi
->s_dirsize
;
180 for (p
= kaddr
; p
<= limit
; p
= minix_next_entry(p
, sbi
)) {
181 if (sbi
->s_version
== MINIX_V3
) {
182 minix3_dirent
*de3
= (minix3_dirent
*)p
;
184 inumber
= de3
->inode
;
186 minix_dirent
*de
= (minix_dirent
*)p
;
192 if (namecompare(namelen
, sbi
->s_namelen
, name
, namx
))
201 return (minix_dirent
*)p
;
204 int minix_add_link(struct dentry
*dentry
, struct inode
*inode
)
206 struct inode
*dir
= dentry
->d_parent
->d_inode
;
207 const char * name
= dentry
->d_name
.name
;
208 int namelen
= dentry
->d_name
.len
;
209 struct super_block
* sb
= dir
->i_sb
;
210 struct minix_sb_info
* sbi
= minix_sb(sb
);
211 struct page
*page
= NULL
;
212 unsigned long npages
= dir_pages(dir
);
223 * We take care of directory expansion in the same loop
224 * This code plays outside i_size, so it locks the page
225 * to protect that region.
227 for (n
= 0; n
<= npages
; n
++) {
228 char *limit
, *dir_end
;
230 page
= dir_get_page(dir
, n
);
235 kaddr
= (char*)page_address(page
);
236 dir_end
= kaddr
+ minix_last_byte(dir
, n
);
237 limit
= kaddr
+ PAGE_CACHE_SIZE
- sbi
->s_dirsize
;
238 for (p
= kaddr
; p
<= limit
; p
= minix_next_entry(p
, sbi
)) {
239 de
= (minix_dirent
*)p
;
240 de3
= (minix3_dirent
*)p
;
241 if (sbi
->s_version
== MINIX_V3
) {
243 inumber
= de3
->inode
;
250 if (sbi
->s_version
== MINIX_V3
)
259 if (namecompare(namelen
, sbi
->s_namelen
, name
, namx
))
269 pos
= page_offset(page
) + p
- (char *)page_address(page
);
270 err
= minix_prepare_chunk(page
, pos
, sbi
->s_dirsize
);
273 memcpy (namx
, name
, namelen
);
274 if (sbi
->s_version
== MINIX_V3
) {
275 memset (namx
+ namelen
, 0, sbi
->s_dirsize
- namelen
- 4);
276 de3
->inode
= inode
->i_ino
;
278 memset (namx
+ namelen
, 0, sbi
->s_dirsize
- namelen
- 2);
279 de
->inode
= inode
->i_ino
;
281 err
= dir_commit_chunk(page
, pos
, sbi
->s_dirsize
);
282 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME_SEC
;
283 mark_inode_dirty(dir
);
293 int minix_delete_entry(struct minix_dir_entry
*de
, struct page
*page
)
295 struct inode
*inode
= page
->mapping
->host
;
296 char *kaddr
= page_address(page
);
297 loff_t pos
= page_offset(page
) + (char*)de
- kaddr
;
298 struct minix_sb_info
*sbi
= minix_sb(inode
->i_sb
);
299 unsigned len
= sbi
->s_dirsize
;
303 err
= minix_prepare_chunk(page
, pos
, len
);
305 if (sbi
->s_version
== MINIX_V3
)
306 ((minix3_dirent
*) de
)->inode
= 0;
309 err
= dir_commit_chunk(page
, pos
, len
);
314 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME_SEC
;
315 mark_inode_dirty(inode
);
319 int minix_make_empty(struct inode
*inode
, struct inode
*dir
)
321 struct page
*page
= grab_cache_page(inode
->i_mapping
, 0);
322 struct minix_sb_info
*sbi
= minix_sb(inode
->i_sb
);
328 err
= minix_prepare_chunk(page
, 0, 2 * sbi
->s_dirsize
);
334 kaddr
= kmap_atomic(page
);
335 memset(kaddr
, 0, PAGE_CACHE_SIZE
);
337 if (sbi
->s_version
== MINIX_V3
) {
338 minix3_dirent
*de3
= (minix3_dirent
*)kaddr
;
340 de3
->inode
= inode
->i_ino
;
341 strcpy(de3
->name
, ".");
342 de3
= minix_next_entry(de3
, sbi
);
343 de3
->inode
= dir
->i_ino
;
344 strcpy(de3
->name
, "..");
346 minix_dirent
*de
= (minix_dirent
*)kaddr
;
348 de
->inode
= inode
->i_ino
;
349 strcpy(de
->name
, ".");
350 de
= minix_next_entry(de
, sbi
);
351 de
->inode
= dir
->i_ino
;
352 strcpy(de
->name
, "..");
354 kunmap_atomic(kaddr
);
356 err
= dir_commit_chunk(page
, 0, 2 * sbi
->s_dirsize
);
358 page_cache_release(page
);
363 * routine to check that the specified directory is empty (for rmdir)
365 int minix_empty_dir(struct inode
* inode
)
367 struct page
*page
= NULL
;
368 unsigned long i
, npages
= dir_pages(inode
);
369 struct minix_sb_info
*sbi
= minix_sb(inode
->i_sb
);
373 for (i
= 0; i
< npages
; i
++) {
374 char *p
, *kaddr
, *limit
;
376 page
= dir_get_page(inode
, i
);
380 kaddr
= (char *)page_address(page
);
381 limit
= kaddr
+ minix_last_byte(inode
, i
) - sbi
->s_dirsize
;
382 for (p
= kaddr
; p
<= limit
; p
= minix_next_entry(p
, sbi
)) {
383 if (sbi
->s_version
== MINIX_V3
) {
384 minix3_dirent
*de3
= (minix3_dirent
*)p
;
386 inumber
= de3
->inode
;
388 minix_dirent
*de
= (minix_dirent
*)p
;
394 /* check for . and .. */
398 if (inumber
!= inode
->i_ino
)
400 } else if (name
[1] != '.')
415 /* Releases the page */
416 void minix_set_link(struct minix_dir_entry
*de
, struct page
*page
,
419 struct inode
*dir
= page
->mapping
->host
;
420 struct minix_sb_info
*sbi
= minix_sb(dir
->i_sb
);
421 loff_t pos
= page_offset(page
) +
422 (char *)de
-(char*)page_address(page
);
427 err
= minix_prepare_chunk(page
, pos
, sbi
->s_dirsize
);
429 if (sbi
->s_version
== MINIX_V3
)
430 ((minix3_dirent
*) de
)->inode
= inode
->i_ino
;
432 de
->inode
= inode
->i_ino
;
433 err
= dir_commit_chunk(page
, pos
, sbi
->s_dirsize
);
438 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME_SEC
;
439 mark_inode_dirty(dir
);
442 struct minix_dir_entry
* minix_dotdot (struct inode
*dir
, struct page
**p
)
444 struct page
*page
= dir_get_page(dir
, 0);
445 struct minix_sb_info
*sbi
= minix_sb(dir
->i_sb
);
446 struct minix_dir_entry
*de
= NULL
;
449 de
= minix_next_entry(page_address(page
), sbi
);
455 ino_t
minix_inode_by_name(struct dentry
*dentry
)
458 struct minix_dir_entry
*de
= minix_find_entry(dentry
, &page
);
462 struct address_space
*mapping
= page
->mapping
;
463 struct inode
*inode
= mapping
->host
;
464 struct minix_sb_info
*sbi
= minix_sb(inode
->i_sb
);
466 if (sbi
->s_version
== MINIX_V3
)
467 res
= ((minix3_dirent
*) de
)->inode
;