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/swap.h>
21 static int sysv_readdir(struct file
*, void *, filldir_t
);
23 const struct file_operations sysv_dir_operations
= {
24 .llseek
= generic_file_llseek
,
25 .read
= generic_read_dir
,
26 .readdir
= sysv_readdir
,
27 .fsync
= generic_file_fsync
,
30 static inline void dir_put_page(struct page
*page
)
33 page_cache_release(page
);
36 static inline unsigned long dir_pages(struct inode
*inode
)
38 return (inode
->i_size
+PAGE_CACHE_SIZE
-1)>>PAGE_CACHE_SHIFT
;
41 static int dir_commit_chunk(struct page
*page
, loff_t pos
, unsigned len
)
43 struct address_space
*mapping
= page
->mapping
;
44 struct inode
*dir
= mapping
->host
;
47 block_write_end(NULL
, mapping
, pos
, len
, len
, page
, NULL
);
48 if (pos
+len
> dir
->i_size
) {
49 i_size_write(dir
, pos
+len
);
50 mark_inode_dirty(dir
);
53 err
= write_one_page(page
, 1);
59 static struct page
* dir_get_page(struct inode
*dir
, unsigned long n
)
61 struct address_space
*mapping
= dir
->i_mapping
;
62 struct page
*page
= read_mapping_page(mapping
, n
, NULL
);
68 static int sysv_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
70 unsigned long pos
= filp
->f_pos
;
71 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
72 struct super_block
*sb
= inode
->i_sb
;
73 unsigned offset
= pos
& ~PAGE_CACHE_MASK
;
74 unsigned long n
= pos
>> PAGE_CACHE_SHIFT
;
75 unsigned long npages
= dir_pages(inode
);
77 pos
= (pos
+ SYSV_DIRSIZE
-1) & ~(SYSV_DIRSIZE
-1);
78 if (pos
>= inode
->i_size
)
81 for ( ; n
< npages
; n
++, offset
= 0) {
83 struct sysv_dir_entry
*de
;
84 struct page
*page
= dir_get_page(inode
, n
);
88 kaddr
= (char *)page_address(page
);
89 de
= (struct sysv_dir_entry
*)(kaddr
+offset
);
90 limit
= kaddr
+ PAGE_CACHE_SIZE
- SYSV_DIRSIZE
;
91 for ( ;(char*)de
<= limit
; de
++) {
92 char *name
= de
->name
;
98 offset
= (char *)de
- kaddr
;
100 over
= filldir(dirent
, name
, strnlen(name
,SYSV_NAMELEN
),
101 ((loff_t
)n
<<PAGE_CACHE_SHIFT
) | offset
,
102 fs16_to_cpu(SYSV_SB(sb
), de
->inode
),
113 filp
->f_pos
= ((loff_t
)n
<< PAGE_CACHE_SHIFT
) | offset
;
117 /* compare strings: name[0..len-1] (not zero-terminated) and
118 * buffer[0..] (filled with zeroes up to buffer[0..maxlen-1])
120 static inline int namecompare(int len
, int maxlen
,
121 const char * name
, const char * buffer
)
123 if (len
< maxlen
&& buffer
[len
])
125 return !memcmp(name
, buffer
, len
);
131 * finds an entry in the specified directory with the wanted name. It
132 * returns the cache buffer in which the entry was found, and the entry
133 * itself (as a parameter - res_dir). It does NOT read the inode of the
134 * entry - you'll have to do that yourself if you want to.
136 struct sysv_dir_entry
*sysv_find_entry(struct dentry
*dentry
, struct page
**res_page
)
138 const char * name
= dentry
->d_name
.name
;
139 int namelen
= dentry
->d_name
.len
;
140 struct inode
* dir
= dentry
->d_parent
->d_inode
;
141 unsigned long start
, n
;
142 unsigned long npages
= dir_pages(dir
);
143 struct page
*page
= NULL
;
144 struct sysv_dir_entry
*de
;
148 start
= SYSV_I(dir
)->i_dir_start_lookup
;
155 page
= dir_get_page(dir
, n
);
157 kaddr
= (char*)page_address(page
);
158 de
= (struct sysv_dir_entry
*) kaddr
;
159 kaddr
+= PAGE_CACHE_SIZE
- SYSV_DIRSIZE
;
160 for ( ; (char *) de
<= kaddr
; de
++) {
163 if (namecompare(namelen
, SYSV_NAMELEN
,
172 } while (n
!= start
);
177 SYSV_I(dir
)->i_dir_start_lookup
= n
;
182 int sysv_add_link(struct dentry
*dentry
, struct inode
*inode
)
184 struct inode
*dir
= dentry
->d_parent
->d_inode
;
185 const char * name
= dentry
->d_name
.name
;
186 int namelen
= dentry
->d_name
.len
;
187 struct page
*page
= NULL
;
188 struct sysv_dir_entry
* de
;
189 unsigned long npages
= dir_pages(dir
);
195 /* We take care of directory expansion in the same loop */
196 for (n
= 0; n
<= npages
; n
++) {
197 page
= dir_get_page(dir
, n
);
201 kaddr
= (char*)page_address(page
);
202 de
= (struct sysv_dir_entry
*)kaddr
;
203 kaddr
+= PAGE_CACHE_SIZE
- SYSV_DIRSIZE
;
204 while ((char *)de
<= kaddr
) {
208 if (namecompare(namelen
, SYSV_NAMELEN
, name
, de
->name
))
218 pos
= page_offset(page
) +
219 (char*)de
- (char*)page_address(page
);
221 err
= __sysv_write_begin(NULL
, page
->mapping
, pos
, SYSV_DIRSIZE
,
222 AOP_FLAG_UNINTERRUPTIBLE
, &page
, NULL
);
225 memcpy (de
->name
, name
, namelen
);
226 memset (de
->name
+ namelen
, 0, SYSV_DIRSIZE
- namelen
- 2);
227 de
->inode
= cpu_to_fs16(SYSV_SB(inode
->i_sb
), inode
->i_ino
);
228 err
= dir_commit_chunk(page
, pos
, SYSV_DIRSIZE
);
229 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME_SEC
;
230 mark_inode_dirty(dir
);
240 int sysv_delete_entry(struct sysv_dir_entry
*de
, struct page
*page
)
242 struct address_space
*mapping
= page
->mapping
;
243 struct inode
*inode
= (struct inode
*)mapping
->host
;
244 char *kaddr
= (char*)page_address(page
);
245 loff_t pos
= page_offset(page
) + (char *)de
- kaddr
;
249 err
= __sysv_write_begin(NULL
, mapping
, pos
, SYSV_DIRSIZE
,
250 AOP_FLAG_UNINTERRUPTIBLE
, &page
, NULL
);
253 err
= dir_commit_chunk(page
, pos
, SYSV_DIRSIZE
);
255 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME_SEC
;
256 mark_inode_dirty(inode
);
260 int sysv_make_empty(struct inode
*inode
, struct inode
*dir
)
262 struct address_space
*mapping
= inode
->i_mapping
;
263 struct page
*page
= grab_cache_page(mapping
, 0);
264 struct sysv_dir_entry
* de
;
270 err
= __sysv_write_begin(NULL
, mapping
, 0, 2 * SYSV_DIRSIZE
,
271 AOP_FLAG_UNINTERRUPTIBLE
, &page
, NULL
);
278 base
= (char*)page_address(page
);
279 memset(base
, 0, PAGE_CACHE_SIZE
);
281 de
= (struct sysv_dir_entry
*) base
;
282 de
->inode
= cpu_to_fs16(SYSV_SB(inode
->i_sb
), inode
->i_ino
);
283 strcpy(de
->name
,".");
285 de
->inode
= cpu_to_fs16(SYSV_SB(inode
->i_sb
), dir
->i_ino
);
286 strcpy(de
->name
,"..");
289 err
= dir_commit_chunk(page
, 0, 2 * SYSV_DIRSIZE
);
291 page_cache_release(page
);
296 * routine to check that the specified directory is empty (for rmdir)
298 int sysv_empty_dir(struct inode
* inode
)
300 struct super_block
*sb
= inode
->i_sb
;
301 struct page
*page
= NULL
;
302 unsigned long i
, npages
= dir_pages(inode
);
304 for (i
= 0; i
< npages
; i
++) {
306 struct sysv_dir_entry
* de
;
307 page
= dir_get_page(inode
, i
);
312 kaddr
= (char *)page_address(page
);
313 de
= (struct sysv_dir_entry
*)kaddr
;
314 kaddr
+= PAGE_CACHE_SIZE
-SYSV_DIRSIZE
;
316 for ( ;(char *)de
<= kaddr
; de
++) {
319 /* check for . and .. */
320 if (de
->name
[0] != '.')
323 if (de
->inode
== cpu_to_fs16(SYSV_SB(sb
),
328 if (de
->name
[1] != '.' || de
->name
[2])
340 /* Releases the page */
341 void sysv_set_link(struct sysv_dir_entry
*de
, struct page
*page
,
344 struct address_space
*mapping
= page
->mapping
;
345 struct inode
*dir
= mapping
->host
;
346 loff_t pos
= page_offset(page
) +
347 (char *)de
-(char*)page_address(page
);
351 err
= __sysv_write_begin(NULL
, mapping
, pos
, SYSV_DIRSIZE
,
352 AOP_FLAG_UNINTERRUPTIBLE
, &page
, NULL
);
354 de
->inode
= cpu_to_fs16(SYSV_SB(inode
->i_sb
), inode
->i_ino
);
355 err
= dir_commit_chunk(page
, pos
, SYSV_DIRSIZE
);
357 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME_SEC
;
358 mark_inode_dirty(dir
);
361 struct sysv_dir_entry
* sysv_dotdot (struct inode
*dir
, struct page
**p
)
363 struct page
*page
= dir_get_page(dir
, 0);
364 struct sysv_dir_entry
*de
= NULL
;
367 de
= (struct sysv_dir_entry
*) page_address(page
) + 1;
373 ino_t
sysv_inode_by_name(struct dentry
*dentry
)
376 struct sysv_dir_entry
*de
= sysv_find_entry (dentry
, &page
);
380 res
= fs16_to_cpu(SYSV_SB(dentry
->d_sb
), de
->inode
);