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/smp_lock.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
*, void *, filldir_t
);
22 const struct file_operations minix_dir_operations
= {
23 .read
= generic_read_dir
,
24 .readdir
= minix_readdir
,
25 .fsync
= minix_sync_file
,
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
);
77 if (!PageUptodate(page
))
87 static inline void *minix_next_entry(void *de
, struct minix_sb_info
*sbi
)
89 return (void*)((char*)de
+ sbi
->s_dirsize
);
92 static int minix_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
94 unsigned long pos
= filp
->f_pos
;
95 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
96 struct super_block
*sb
= inode
->i_sb
;
97 unsigned offset
= pos
& ~PAGE_CACHE_MASK
;
98 unsigned long n
= pos
>> PAGE_CACHE_SHIFT
;
99 unsigned long npages
= dir_pages(inode
);
100 struct minix_sb_info
*sbi
= minix_sb(sb
);
101 unsigned chunk_size
= sbi
->s_dirsize
;
107 pos
= (pos
+ chunk_size
-1) & ~(chunk_size
-1);
108 if (pos
>= inode
->i_size
)
111 for ( ; n
< npages
; n
++, offset
= 0) {
112 char *p
, *kaddr
, *limit
;
113 struct page
*page
= dir_get_page(inode
, n
);
117 kaddr
= (char *)page_address(page
);
119 limit
= kaddr
+ minix_last_byte(inode
, n
) - chunk_size
;
120 for ( ; p
<= limit
; p
= minix_next_entry(p
, sbi
)) {
121 if (sbi
->s_version
== MINIX_V3
) {
122 minix3_dirent
*de3
= (minix3_dirent
*)p
;
124 inumber
= de3
->inode
;
126 minix_dirent
*de
= (minix_dirent
*)p
;
133 unsigned l
= strnlen(name
, sbi
->s_namelen
);
135 over
= filldir(dirent
, name
, l
,
136 (n
<< PAGE_CACHE_SHIFT
) | offset
,
137 inumber
, DT_UNKNOWN
);
148 filp
->f_pos
= (n
<< PAGE_CACHE_SHIFT
) | offset
;
153 static inline int namecompare(int len
, int maxlen
,
154 const char * name
, const char * buffer
)
156 if (len
< maxlen
&& buffer
[len
])
158 return !memcmp(name
, buffer
, len
);
164 * finds an entry in the specified directory with the wanted name. It
165 * returns the cache buffer in which the entry was found, and the entry
166 * itself (as a parameter - res_dir). It does NOT read the inode of the
167 * entry - you'll have to do that yourself if you want to.
169 minix_dirent
*minix_find_entry(struct dentry
*dentry
, struct page
**res_page
)
171 const char * name
= dentry
->d_name
.name
;
172 int namelen
= dentry
->d_name
.len
;
173 struct inode
* dir
= dentry
->d_parent
->d_inode
;
174 struct super_block
* sb
= dir
->i_sb
;
175 struct minix_sb_info
* sbi
= minix_sb(sb
);
177 unsigned long npages
= dir_pages(dir
);
178 struct page
*page
= NULL
;
185 for (n
= 0; n
< npages
; n
++) {
188 page
= dir_get_page(dir
, n
);
192 kaddr
= (char*)page_address(page
);
193 limit
= kaddr
+ minix_last_byte(dir
, n
) - sbi
->s_dirsize
;
194 for (p
= kaddr
; p
<= limit
; p
= minix_next_entry(p
, sbi
)) {
195 if (sbi
->s_version
== MINIX_V3
) {
196 minix3_dirent
*de3
= (minix3_dirent
*)p
;
198 inumber
= de3
->inode
;
200 minix_dirent
*de
= (minix_dirent
*)p
;
206 if (namecompare(namelen
, sbi
->s_namelen
, name
, namx
))
215 return (minix_dirent
*)p
;
218 int minix_add_link(struct dentry
*dentry
, struct inode
*inode
)
220 struct inode
*dir
= dentry
->d_parent
->d_inode
;
221 const char * name
= dentry
->d_name
.name
;
222 int namelen
= dentry
->d_name
.len
;
223 struct super_block
* sb
= dir
->i_sb
;
224 struct minix_sb_info
* sbi
= minix_sb(sb
);
225 struct page
*page
= NULL
;
226 unsigned long npages
= dir_pages(dir
);
237 * We take care of directory expansion in the same loop
238 * This code plays outside i_size, so it locks the page
239 * to protect that region.
241 for (n
= 0; n
<= npages
; n
++) {
242 char *limit
, *dir_end
;
244 page
= dir_get_page(dir
, n
);
249 kaddr
= (char*)page_address(page
);
250 dir_end
= kaddr
+ minix_last_byte(dir
, n
);
251 limit
= kaddr
+ PAGE_CACHE_SIZE
- sbi
->s_dirsize
;
252 for (p
= kaddr
; p
<= limit
; p
= minix_next_entry(p
, sbi
)) {
253 de
= (minix_dirent
*)p
;
254 de3
= (minix3_dirent
*)p
;
255 if (sbi
->s_version
== MINIX_V3
) {
257 inumber
= de3
->inode
;
264 if (sbi
->s_version
== MINIX_V3
)
273 if (namecompare(namelen
, sbi
->s_namelen
, name
, namx
))
283 pos
= (page
->index
>> PAGE_CACHE_SHIFT
) + p
- (char*)page_address(page
);
284 err
= __minix_write_begin(NULL
, page
->mapping
, pos
, sbi
->s_dirsize
,
285 AOP_FLAG_UNINTERRUPTIBLE
, &page
, NULL
);
288 memcpy (namx
, name
, namelen
);
289 if (sbi
->s_version
== MINIX_V3
) {
290 memset (namx
+ namelen
, 0, sbi
->s_dirsize
- namelen
- 4);
291 de3
->inode
= inode
->i_ino
;
293 memset (namx
+ namelen
, 0, sbi
->s_dirsize
- namelen
- 2);
294 de
->inode
= inode
->i_ino
;
296 err
= dir_commit_chunk(page
, pos
, sbi
->s_dirsize
);
297 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME_SEC
;
298 mark_inode_dirty(dir
);
308 int minix_delete_entry(struct minix_dir_entry
*de
, struct page
*page
)
310 struct address_space
*mapping
= page
->mapping
;
311 struct inode
*inode
= (struct inode
*)mapping
->host
;
312 char *kaddr
= page_address(page
);
313 loff_t pos
= page_offset(page
) + (char*)de
- kaddr
;
314 unsigned len
= minix_sb(inode
->i_sb
)->s_dirsize
;
318 err
= __minix_write_begin(NULL
, mapping
, pos
, len
,
319 AOP_FLAG_UNINTERRUPTIBLE
, &page
, NULL
);
322 err
= dir_commit_chunk(page
, pos
, len
);
327 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME_SEC
;
328 mark_inode_dirty(inode
);
332 int minix_make_empty(struct inode
*inode
, struct inode
*dir
)
334 struct address_space
*mapping
= inode
->i_mapping
;
335 struct page
*page
= grab_cache_page(mapping
, 0);
336 struct minix_sb_info
*sbi
= minix_sb(inode
->i_sb
);
342 err
= __minix_write_begin(NULL
, mapping
, 0, 2 * sbi
->s_dirsize
,
343 AOP_FLAG_UNINTERRUPTIBLE
, &page
, NULL
);
349 kaddr
= kmap_atomic(page
, KM_USER0
);
350 memset(kaddr
, 0, PAGE_CACHE_SIZE
);
352 if (sbi
->s_version
== MINIX_V3
) {
353 minix3_dirent
*de3
= (minix3_dirent
*)kaddr
;
355 de3
->inode
= inode
->i_ino
;
356 strcpy(de3
->name
, ".");
357 de3
= minix_next_entry(de3
, sbi
);
358 de3
->inode
= dir
->i_ino
;
359 strcpy(de3
->name
, "..");
361 minix_dirent
*de
= (minix_dirent
*)kaddr
;
363 de
->inode
= inode
->i_ino
;
364 strcpy(de
->name
, ".");
365 de
= minix_next_entry(de
, sbi
);
366 de
->inode
= dir
->i_ino
;
367 strcpy(de
->name
, "..");
369 kunmap_atomic(kaddr
, KM_USER0
);
371 err
= dir_commit_chunk(page
, 0, 2 * sbi
->s_dirsize
);
373 page_cache_release(page
);
378 * routine to check that the specified directory is empty (for rmdir)
380 int minix_empty_dir(struct inode
* inode
)
382 struct page
*page
= NULL
;
383 unsigned long i
, npages
= dir_pages(inode
);
384 struct minix_sb_info
*sbi
= minix_sb(inode
->i_sb
);
388 for (i
= 0; i
< npages
; i
++) {
389 char *p
, *kaddr
, *limit
;
391 page
= dir_get_page(inode
, i
);
395 kaddr
= (char *)page_address(page
);
396 limit
= kaddr
+ minix_last_byte(inode
, i
) - sbi
->s_dirsize
;
397 for (p
= kaddr
; p
<= limit
; p
= minix_next_entry(p
, sbi
)) {
398 if (sbi
->s_version
== MINIX_V3
) {
399 minix3_dirent
*de3
= (minix3_dirent
*)p
;
401 inumber
= de3
->inode
;
403 minix_dirent
*de
= (minix_dirent
*)p
;
409 /* check for . and .. */
413 if (inumber
!= inode
->i_ino
)
415 } else if (name
[1] != '.')
430 /* Releases the page */
431 void minix_set_link(struct minix_dir_entry
*de
, struct page
*page
,
434 struct address_space
*mapping
= page
->mapping
;
435 struct inode
*dir
= mapping
->host
;
436 struct minix_sb_info
*sbi
= minix_sb(dir
->i_sb
);
437 loff_t pos
= page_offset(page
) +
438 (char *)de
-(char*)page_address(page
);
443 err
= __minix_write_begin(NULL
, mapping
, pos
, sbi
->s_dirsize
,
444 AOP_FLAG_UNINTERRUPTIBLE
, &page
, NULL
);
446 de
->inode
= inode
->i_ino
;
447 err
= dir_commit_chunk(page
, pos
, sbi
->s_dirsize
);
452 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME_SEC
;
453 mark_inode_dirty(dir
);
456 struct minix_dir_entry
* minix_dotdot (struct inode
*dir
, struct page
**p
)
458 struct page
*page
= dir_get_page(dir
, 0);
459 struct minix_sb_info
*sbi
= minix_sb(dir
->i_sb
);
460 struct minix_dir_entry
*de
= NULL
;
463 de
= minix_next_entry(page_address(page
), sbi
);
469 ino_t
minix_inode_by_name(struct dentry
*dentry
)
472 struct minix_dir_entry
*de
= minix_find_entry(dentry
, &page
);