5 * Copyright (C) 1991, 1992 Linus Torvalds
8 * Copyright (C) 1993 Pascal Haible, Bruno Haible
11 * Copyright (C) 1993 Bruno Haible
13 * SystemV/Coherent directory handling functions
16 #include <linux/pagemap.h>
17 #include <linux/highmem.h>
18 #include <linux/smp_lock.h>
21 static int sysv_readdir(struct file
*, void *, filldir_t
);
23 const struct file_operations sysv_dir_operations
= {
24 .read
= generic_read_dir
,
25 .readdir
= sysv_readdir
,
26 .fsync
= sysv_sync_file
,
29 static inline void dir_put_page(struct page
*page
)
32 page_cache_release(page
);
35 static inline unsigned long dir_pages(struct inode
*inode
)
37 return (inode
->i_size
+PAGE_CACHE_SIZE
-1)>>PAGE_CACHE_SHIFT
;
40 static int dir_commit_chunk(struct page
*page
, unsigned from
, unsigned to
)
42 struct inode
*dir
= (struct inode
*)page
->mapping
->host
;
45 page
->mapping
->a_ops
->commit_write(NULL
, page
, from
, to
);
47 err
= write_one_page(page
, 1);
53 static struct page
* dir_get_page(struct inode
*dir
, unsigned long n
)
55 struct address_space
*mapping
= dir
->i_mapping
;
56 struct page
*page
= read_cache_page(mapping
, n
,
57 (filler_t
*)mapping
->a_ops
->readpage
, NULL
);
59 wait_on_page_locked(page
);
61 if (!PageUptodate(page
))
71 static int sysv_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
73 unsigned long pos
= filp
->f_pos
;
74 struct inode
*inode
= filp
->f_dentry
->d_inode
;
75 struct super_block
*sb
= inode
->i_sb
;
76 unsigned offset
= pos
& ~PAGE_CACHE_MASK
;
77 unsigned long n
= pos
>> PAGE_CACHE_SHIFT
;
78 unsigned long npages
= dir_pages(inode
);
82 pos
= (pos
+ SYSV_DIRSIZE
-1) & ~(SYSV_DIRSIZE
-1);
83 if (pos
>= inode
->i_size
)
86 for ( ; n
< npages
; n
++, offset
= 0) {
88 struct sysv_dir_entry
*de
;
89 struct page
*page
= dir_get_page(inode
, n
);
93 kaddr
= (char *)page_address(page
);
94 de
= (struct sysv_dir_entry
*)(kaddr
+offset
);
95 limit
= kaddr
+ PAGE_CACHE_SIZE
- SYSV_DIRSIZE
;
96 for ( ;(char*)de
<= limit
; de
++) {
97 char *name
= de
->name
;
103 offset
= (char *)de
- kaddr
;
105 over
= filldir(dirent
, name
, strnlen(name
,SYSV_NAMELEN
),
106 ((loff_t
)n
<<PAGE_CACHE_SHIFT
) | offset
,
107 fs16_to_cpu(SYSV_SB(sb
), de
->inode
),
118 filp
->f_pos
= ((loff_t
)n
<< PAGE_CACHE_SHIFT
) | offset
;
123 /* compare strings: name[0..len-1] (not zero-terminated) and
124 * buffer[0..] (filled with zeroes up to buffer[0..maxlen-1])
126 static inline int namecompare(int len
, int maxlen
,
127 const char * name
, const char * buffer
)
129 if (len
< maxlen
&& buffer
[len
])
131 return !memcmp(name
, buffer
, len
);
137 * finds an entry in the specified directory with the wanted name. It
138 * returns the cache buffer in which the entry was found, and the entry
139 * itself (as a parameter - res_dir). It does NOT read the inode of the
140 * entry - you'll have to do that yourself if you want to.
142 struct sysv_dir_entry
*sysv_find_entry(struct dentry
*dentry
, struct page
**res_page
)
144 const char * name
= dentry
->d_name
.name
;
145 int namelen
= dentry
->d_name
.len
;
146 struct inode
* dir
= dentry
->d_parent
->d_inode
;
147 unsigned long start
, n
;
148 unsigned long npages
= dir_pages(dir
);
149 struct page
*page
= NULL
;
150 struct sysv_dir_entry
*de
;
154 start
= SYSV_I(dir
)->i_dir_start_lookup
;
161 page
= dir_get_page(dir
, n
);
163 kaddr
= (char*)page_address(page
);
164 de
= (struct sysv_dir_entry
*) kaddr
;
165 kaddr
+= PAGE_CACHE_SIZE
- SYSV_DIRSIZE
;
166 for ( ; (char *) de
<= kaddr
; de
++) {
169 if (namecompare(namelen
, SYSV_NAMELEN
,
178 } while (n
!= start
);
183 SYSV_I(dir
)->i_dir_start_lookup
= n
;
188 int sysv_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 page
*page
= NULL
;
194 struct sysv_dir_entry
* de
;
195 unsigned long npages
= dir_pages(dir
);
201 /* We take care of directory expansion in the same loop */
202 for (n
= 0; n
<= npages
; n
++) {
203 page
= dir_get_page(dir
, n
);
207 kaddr
= (char*)page_address(page
);
208 de
= (struct sysv_dir_entry
*)kaddr
;
209 kaddr
+= PAGE_CACHE_SIZE
- SYSV_DIRSIZE
;
210 while ((char *)de
<= kaddr
) {
214 if (namecompare(namelen
, SYSV_NAMELEN
, name
, de
->name
))
224 from
= (char*)de
- (char*)page_address(page
);
225 to
= from
+ SYSV_DIRSIZE
;
227 err
= page
->mapping
->a_ops
->prepare_write(NULL
, page
, from
, to
);
230 memcpy (de
->name
, name
, namelen
);
231 memset (de
->name
+ namelen
, 0, SYSV_DIRSIZE
- namelen
- 2);
232 de
->inode
= cpu_to_fs16(SYSV_SB(inode
->i_sb
), inode
->i_ino
);
233 err
= dir_commit_chunk(page
, from
, to
);
234 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME_SEC
;
235 mark_inode_dirty(dir
);
245 int sysv_delete_entry(struct sysv_dir_entry
*de
, struct page
*page
)
247 struct address_space
*mapping
= page
->mapping
;
248 struct inode
*inode
= (struct inode
*)mapping
->host
;
249 char *kaddr
= (char*)page_address(page
);
250 unsigned from
= (char*)de
- kaddr
;
251 unsigned to
= from
+ SYSV_DIRSIZE
;
255 err
= mapping
->a_ops
->prepare_write(NULL
, page
, from
, to
);
259 err
= dir_commit_chunk(page
, from
, to
);
261 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME_SEC
;
262 mark_inode_dirty(inode
);
266 int sysv_make_empty(struct inode
*inode
, struct inode
*dir
)
268 struct address_space
*mapping
= inode
->i_mapping
;
269 struct page
*page
= grab_cache_page(mapping
, 0);
270 struct sysv_dir_entry
* de
;
277 err
= mapping
->a_ops
->prepare_write(NULL
, page
, 0, 2 * SYSV_DIRSIZE
);
283 base
= (char*)page_address(page
);
284 memset(base
, 0, PAGE_CACHE_SIZE
);
286 de
= (struct sysv_dir_entry
*) base
;
287 de
->inode
= cpu_to_fs16(SYSV_SB(inode
->i_sb
), inode
->i_ino
);
288 strcpy(de
->name
,".");
290 de
->inode
= cpu_to_fs16(SYSV_SB(inode
->i_sb
), dir
->i_ino
);
291 strcpy(de
->name
,"..");
293 err
= dir_commit_chunk(page
, 0, 2 * SYSV_DIRSIZE
);
296 page_cache_release(page
);
301 * routine to check that the specified directory is empty (for rmdir)
303 int sysv_empty_dir(struct inode
* inode
)
305 struct super_block
*sb
= inode
->i_sb
;
306 struct page
*page
= NULL
;
307 unsigned long i
, npages
= dir_pages(inode
);
309 for (i
= 0; i
< npages
; i
++) {
311 struct sysv_dir_entry
* de
;
312 page
= dir_get_page(inode
, i
);
317 kaddr
= (char *)page_address(page
);
318 de
= (struct sysv_dir_entry
*)kaddr
;
319 kaddr
+= PAGE_CACHE_SIZE
-SYSV_DIRSIZE
;
321 for ( ;(char *)de
<= kaddr
; de
++) {
324 /* check for . and .. */
325 if (de
->name
[0] != '.')
328 if (de
->inode
== cpu_to_fs16(SYSV_SB(sb
),
333 if (de
->name
[1] != '.' || de
->name
[2])
345 /* Releases the page */
346 void sysv_set_link(struct sysv_dir_entry
*de
, struct page
*page
,
349 struct inode
*dir
= (struct inode
*)page
->mapping
->host
;
350 unsigned from
= (char *)de
-(char*)page_address(page
);
351 unsigned to
= from
+ SYSV_DIRSIZE
;
355 err
= page
->mapping
->a_ops
->prepare_write(NULL
, page
, from
, to
);
358 de
->inode
= cpu_to_fs16(SYSV_SB(inode
->i_sb
), inode
->i_ino
);
359 err
= dir_commit_chunk(page
, from
, to
);
361 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME_SEC
;
362 mark_inode_dirty(dir
);
365 struct sysv_dir_entry
* sysv_dotdot (struct inode
*dir
, struct page
**p
)
367 struct page
*page
= dir_get_page(dir
, 0);
368 struct sysv_dir_entry
*de
= NULL
;
371 de
= (struct sysv_dir_entry
*) page_address(page
) + 1;
377 ino_t
sysv_inode_by_name(struct dentry
*dentry
)
380 struct sysv_dir_entry
*de
= sysv_find_entry (dentry
, &page
);
384 res
= fs16_to_cpu(SYSV_SB(dentry
->d_sb
), de
->inode
);