4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * minix directory handling functions
10 #include <linux/highmem.h>
11 #include <linux/smp_lock.h>
13 typedef struct minix_dir_entry minix_dirent
;
15 static int minix_readdir(struct file
*, void *, filldir_t
);
17 struct file_operations minix_dir_operations
= {
18 .read
= generic_read_dir
,
19 .readdir
= minix_readdir
,
20 .fsync
= minix_sync_file
,
23 static inline void dir_put_page(struct page
*page
)
26 page_cache_release(page
);
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_CACHE_SIZE
;
38 if (page_nr
== (inode
->i_size
>> PAGE_CACHE_SHIFT
))
39 last_byte
= inode
->i_size
& (PAGE_CACHE_SIZE
- 1);
43 static inline unsigned long dir_pages(struct inode
*inode
)
45 return (inode
->i_size
+PAGE_CACHE_SIZE
-1)>>PAGE_CACHE_SHIFT
;
48 static int dir_commit_chunk(struct page
*page
, unsigned from
, unsigned to
)
50 struct inode
*dir
= (struct inode
*)page
->mapping
->host
;
52 page
->mapping
->a_ops
->commit_write(NULL
, page
, from
, to
);
54 err
= write_one_page(page
, 1);
60 static struct page
* dir_get_page(struct inode
*dir
, unsigned long n
)
62 struct address_space
*mapping
= dir
->i_mapping
;
63 struct page
*page
= read_cache_page(mapping
, n
,
64 (filler_t
*)mapping
->a_ops
->readpage
, NULL
);
66 wait_on_page_locked(page
);
68 if (!PageUptodate(page
))
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
* filp
, void * dirent
, filldir_t filldir
)
85 unsigned long pos
= filp
->f_pos
;
86 struct inode
*inode
= filp
->f_dentry
->d_inode
;
87 struct super_block
*sb
= inode
->i_sb
;
88 unsigned offset
= pos
& ~PAGE_CACHE_MASK
;
89 unsigned long n
= pos
>> PAGE_CACHE_SHIFT
;
90 unsigned long npages
= dir_pages(inode
);
91 struct minix_sb_info
*sbi
= minix_sb(sb
);
92 unsigned chunk_size
= sbi
->s_dirsize
;
96 pos
= (pos
+ chunk_size
-1) & ~(chunk_size
-1);
97 if (pos
>= inode
->i_size
)
100 for ( ; n
< npages
; n
++, offset
= 0) {
101 char *p
, *kaddr
, *limit
;
102 struct page
*page
= dir_get_page(inode
, n
);
106 kaddr
= (char *)page_address(page
);
108 limit
= kaddr
+ minix_last_byte(inode
, n
) - chunk_size
;
109 for ( ; p
<= limit
; p
= minix_next_entry(p
, sbi
)) {
110 minix_dirent
*de
= (minix_dirent
*)p
;
113 unsigned l
= strnlen(de
->name
,sbi
->s_namelen
);
116 over
= filldir(dirent
, de
->name
, l
,
117 (n
<<PAGE_CACHE_SHIFT
) | offset
,
118 de
->inode
, DT_UNKNOWN
);
129 filp
->f_pos
= (n
<< PAGE_CACHE_SHIFT
) | offset
;
134 static inline int namecompare(int len
, int maxlen
,
135 const char * name
, const char * buffer
)
137 if (len
< maxlen
&& buffer
[len
])
139 return !memcmp(name
, buffer
, len
);
145 * finds an entry in the specified directory with the wanted name. It
146 * returns the cache buffer in which the entry was found, and the entry
147 * itself (as a parameter - res_dir). It does NOT read the inode of the
148 * entry - you'll have to do that yourself if you want to.
150 minix_dirent
*minix_find_entry(struct dentry
*dentry
, struct page
**res_page
)
152 const char * name
= dentry
->d_name
.name
;
153 int namelen
= dentry
->d_name
.len
;
154 struct inode
* dir
= dentry
->d_parent
->d_inode
;
155 struct super_block
* sb
= dir
->i_sb
;
156 struct minix_sb_info
* sbi
= minix_sb(sb
);
158 unsigned long npages
= dir_pages(dir
);
159 struct page
*page
= NULL
;
160 struct minix_dir_entry
*de
;
164 for (n
= 0; n
< npages
; n
++) {
166 page
= dir_get_page(dir
, n
);
170 kaddr
= (char*)page_address(page
);
171 de
= (struct minix_dir_entry
*) kaddr
;
172 kaddr
+= minix_last_byte(dir
, n
) - sbi
->s_dirsize
;
173 for ( ; (char *) de
<= kaddr
; de
= minix_next_entry(de
,sbi
)) {
176 if (namecompare(namelen
,sbi
->s_namelen
,name
,de
->name
))
188 int minix_add_link(struct dentry
*dentry
, struct inode
*inode
)
190 struct inode
*dir
= dentry
->d_parent
->d_inode
;
191 const char * name
= dentry
->d_name
.name
;
192 int namelen
= dentry
->d_name
.len
;
193 struct super_block
* sb
= dir
->i_sb
;
194 struct minix_sb_info
* sbi
= minix_sb(sb
);
195 struct page
*page
= NULL
;
196 struct minix_dir_entry
* de
;
197 unsigned long npages
= dir_pages(dir
);
204 * We take care of directory expansion in the same loop
205 * This code plays outside i_size, so it locks the page
206 * to protect that region.
208 for (n
= 0; n
<= npages
; n
++) {
211 page
= dir_get_page(dir
, n
);
216 kaddr
= (char*)page_address(page
);
217 dir_end
= kaddr
+ minix_last_byte(dir
, n
);
218 de
= (minix_dirent
*)kaddr
;
219 kaddr
+= PAGE_CACHE_SIZE
- sbi
->s_dirsize
;
220 while ((char *)de
<= kaddr
) {
221 if ((char *)de
== dir_end
) {
229 if (namecompare(namelen
,sbi
->s_namelen
,name
,de
->name
))
231 de
= minix_next_entry(de
, sbi
);
240 from
= (char*)de
- (char*)page_address(page
);
241 to
= from
+ sbi
->s_dirsize
;
242 err
= page
->mapping
->a_ops
->prepare_write(NULL
, page
, from
, to
);
245 memcpy (de
->name
, name
, namelen
);
246 memset (de
->name
+ namelen
, 0, sbi
->s_dirsize
- namelen
- 2);
247 de
->inode
= inode
->i_ino
;
248 err
= dir_commit_chunk(page
, from
, to
);
249 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME_SEC
;
250 mark_inode_dirty(dir
);
260 int minix_delete_entry(struct minix_dir_entry
*de
, struct page
*page
)
262 struct address_space
*mapping
= page
->mapping
;
263 struct inode
*inode
= (struct inode
*)mapping
->host
;
264 char *kaddr
= page_address(page
);
265 unsigned from
= (char*)de
- kaddr
;
266 unsigned to
= from
+ minix_sb(inode
->i_sb
)->s_dirsize
;
270 err
= mapping
->a_ops
->prepare_write(NULL
, page
, from
, to
);
273 err
= dir_commit_chunk(page
, from
, to
);
278 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME_SEC
;
279 mark_inode_dirty(inode
);
283 int minix_make_empty(struct inode
*inode
, struct inode
*dir
)
285 struct address_space
*mapping
= inode
->i_mapping
;
286 struct page
*page
= grab_cache_page(mapping
, 0);
287 struct minix_sb_info
* sbi
= minix_sb(inode
->i_sb
);
288 struct minix_dir_entry
* de
;
294 err
= mapping
->a_ops
->prepare_write(NULL
, page
, 0, 2 * sbi
->s_dirsize
);
300 kaddr
= kmap_atomic(page
, KM_USER0
);
301 memset(kaddr
, 0, PAGE_CACHE_SIZE
);
303 de
= (struct minix_dir_entry
*)kaddr
;
304 de
->inode
= inode
->i_ino
;
305 strcpy(de
->name
,".");
306 de
= minix_next_entry(de
, sbi
);
307 de
->inode
= dir
->i_ino
;
308 strcpy(de
->name
,"..");
309 kunmap_atomic(kaddr
, KM_USER0
);
311 err
= dir_commit_chunk(page
, 0, 2 * sbi
->s_dirsize
);
313 page_cache_release(page
);
318 * routine to check that the specified directory is empty (for rmdir)
320 int minix_empty_dir(struct inode
* inode
)
322 struct page
*page
= NULL
;
323 unsigned long i
, npages
= dir_pages(inode
);
324 struct minix_sb_info
*sbi
= minix_sb(inode
->i_sb
);
326 for (i
= 0; i
< npages
; i
++) {
329 page
= dir_get_page(inode
, i
);
334 kaddr
= (char *)page_address(page
);
335 de
= (minix_dirent
*)kaddr
;
336 kaddr
+= minix_last_byte(inode
, i
) - sbi
->s_dirsize
;
338 while ((char *)de
<= kaddr
) {
339 if (de
->inode
!= 0) {
340 /* check for . and .. */
341 if (de
->name
[0] != '.')
344 if (de
->inode
!= inode
->i_ino
)
346 } else if (de
->name
[1] != '.')
348 else if (de
->name
[2])
351 de
= minix_next_entry(de
, sbi
);
362 /* Releases the page */
363 void minix_set_link(struct minix_dir_entry
*de
, struct page
*page
,
366 struct inode
*dir
= (struct inode
*)page
->mapping
->host
;
367 struct minix_sb_info
*sbi
= minix_sb(dir
->i_sb
);
368 unsigned from
= (char *)de
-(char*)page_address(page
);
369 unsigned to
= from
+ sbi
->s_dirsize
;
373 err
= page
->mapping
->a_ops
->prepare_write(NULL
, page
, from
, to
);
375 de
->inode
= inode
->i_ino
;
376 err
= dir_commit_chunk(page
, from
, to
);
381 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME_SEC
;
382 mark_inode_dirty(dir
);
385 struct minix_dir_entry
* minix_dotdot (struct inode
*dir
, struct page
**p
)
387 struct page
*page
= dir_get_page(dir
, 0);
388 struct minix_sb_info
*sbi
= minix_sb(dir
->i_sb
);
389 struct minix_dir_entry
*de
= NULL
;
392 de
= minix_next_entry(page_address(page
), sbi
);
398 ino_t
minix_inode_by_name(struct dentry
*dentry
)
401 struct minix_dir_entry
*de
= minix_find_entry(dentry
, &page
);