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
*, struct dir_context
*);
23 const struct file_operations sysv_dir_operations
= {
24 .llseek
= generic_file_llseek
,
25 .read
= generic_read_dir
,
26 .iterate
= 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
*file
, struct dir_context
*ctx
)
70 unsigned long pos
= ctx
->pos
;
71 struct inode
*inode
= file_inode(file
);
72 struct super_block
*sb
= inode
->i_sb
;
73 unsigned long npages
= dir_pages(inode
);
77 ctx
->pos
= pos
= (pos
+ SYSV_DIRSIZE
-1) & ~(SYSV_DIRSIZE
-1);
78 if (pos
>= inode
->i_size
)
81 offset
= pos
& ~PAGE_CACHE_MASK
;
82 n
= pos
>> PAGE_CACHE_SHIFT
;
84 for ( ; n
< npages
; n
++, offset
= 0) {
86 struct sysv_dir_entry
*de
;
87 struct page
*page
= dir_get_page(inode
, n
);
91 kaddr
= (char *)page_address(page
);
92 de
= (struct sysv_dir_entry
*)(kaddr
+offset
);
93 limit
= kaddr
+ PAGE_CACHE_SIZE
- SYSV_DIRSIZE
;
94 for ( ;(char*)de
<= limit
; de
++, ctx
->pos
+= sizeof(*de
)) {
95 char *name
= de
->name
;
100 if (!dir_emit(ctx
, name
, strnlen(name
,SYSV_NAMELEN
),
101 fs16_to_cpu(SYSV_SB(sb
), de
->inode
),
112 /* compare strings: name[0..len-1] (not zero-terminated) and
113 * buffer[0..] (filled with zeroes up to buffer[0..maxlen-1])
115 static inline int namecompare(int len
, int maxlen
,
116 const char * name
, const char * buffer
)
118 if (len
< maxlen
&& buffer
[len
])
120 return !memcmp(name
, buffer
, len
);
126 * finds an entry in the specified directory with the wanted name. It
127 * returns the cache buffer in which the entry was found, and the entry
128 * itself (as a parameter - res_dir). It does NOT read the inode of the
129 * entry - you'll have to do that yourself if you want to.
131 struct sysv_dir_entry
*sysv_find_entry(struct dentry
*dentry
, struct page
**res_page
)
133 const char * name
= dentry
->d_name
.name
;
134 int namelen
= dentry
->d_name
.len
;
135 struct inode
* dir
= dentry
->d_parent
->d_inode
;
136 unsigned long start
, n
;
137 unsigned long npages
= dir_pages(dir
);
138 struct page
*page
= NULL
;
139 struct sysv_dir_entry
*de
;
143 start
= SYSV_I(dir
)->i_dir_start_lookup
;
150 page
= dir_get_page(dir
, n
);
152 kaddr
= (char*)page_address(page
);
153 de
= (struct sysv_dir_entry
*) kaddr
;
154 kaddr
+= PAGE_CACHE_SIZE
- SYSV_DIRSIZE
;
155 for ( ; (char *) de
<= kaddr
; de
++) {
158 if (namecompare(namelen
, SYSV_NAMELEN
,
167 } while (n
!= start
);
172 SYSV_I(dir
)->i_dir_start_lookup
= n
;
177 int sysv_add_link(struct dentry
*dentry
, struct inode
*inode
)
179 struct inode
*dir
= dentry
->d_parent
->d_inode
;
180 const char * name
= dentry
->d_name
.name
;
181 int namelen
= dentry
->d_name
.len
;
182 struct page
*page
= NULL
;
183 struct sysv_dir_entry
* de
;
184 unsigned long npages
= dir_pages(dir
);
190 /* We take care of directory expansion in the same loop */
191 for (n
= 0; n
<= npages
; n
++) {
192 page
= dir_get_page(dir
, n
);
196 kaddr
= (char*)page_address(page
);
197 de
= (struct sysv_dir_entry
*)kaddr
;
198 kaddr
+= PAGE_CACHE_SIZE
- SYSV_DIRSIZE
;
199 while ((char *)de
<= kaddr
) {
203 if (namecompare(namelen
, SYSV_NAMELEN
, name
, de
->name
))
213 pos
= page_offset(page
) +
214 (char*)de
- (char*)page_address(page
);
216 err
= sysv_prepare_chunk(page
, pos
, SYSV_DIRSIZE
);
219 memcpy (de
->name
, name
, namelen
);
220 memset (de
->name
+ namelen
, 0, SYSV_DIRSIZE
- namelen
- 2);
221 de
->inode
= cpu_to_fs16(SYSV_SB(inode
->i_sb
), inode
->i_ino
);
222 err
= dir_commit_chunk(page
, pos
, SYSV_DIRSIZE
);
223 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME_SEC
;
224 mark_inode_dirty(dir
);
234 int sysv_delete_entry(struct sysv_dir_entry
*de
, struct page
*page
)
236 struct inode
*inode
= page
->mapping
->host
;
237 char *kaddr
= (char*)page_address(page
);
238 loff_t pos
= page_offset(page
) + (char *)de
- kaddr
;
242 err
= sysv_prepare_chunk(page
, pos
, SYSV_DIRSIZE
);
245 err
= dir_commit_chunk(page
, pos
, SYSV_DIRSIZE
);
247 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME_SEC
;
248 mark_inode_dirty(inode
);
252 int sysv_make_empty(struct inode
*inode
, struct inode
*dir
)
254 struct page
*page
= grab_cache_page(inode
->i_mapping
, 0);
255 struct sysv_dir_entry
* de
;
261 err
= sysv_prepare_chunk(page
, 0, 2 * SYSV_DIRSIZE
);
268 base
= (char*)page_address(page
);
269 memset(base
, 0, PAGE_CACHE_SIZE
);
271 de
= (struct sysv_dir_entry
*) base
;
272 de
->inode
= cpu_to_fs16(SYSV_SB(inode
->i_sb
), inode
->i_ino
);
273 strcpy(de
->name
,".");
275 de
->inode
= cpu_to_fs16(SYSV_SB(inode
->i_sb
), dir
->i_ino
);
276 strcpy(de
->name
,"..");
279 err
= dir_commit_chunk(page
, 0, 2 * SYSV_DIRSIZE
);
281 page_cache_release(page
);
286 * routine to check that the specified directory is empty (for rmdir)
288 int sysv_empty_dir(struct inode
* inode
)
290 struct super_block
*sb
= inode
->i_sb
;
291 struct page
*page
= NULL
;
292 unsigned long i
, npages
= dir_pages(inode
);
294 for (i
= 0; i
< npages
; i
++) {
296 struct sysv_dir_entry
* de
;
297 page
= dir_get_page(inode
, i
);
302 kaddr
= (char *)page_address(page
);
303 de
= (struct sysv_dir_entry
*)kaddr
;
304 kaddr
+= PAGE_CACHE_SIZE
-SYSV_DIRSIZE
;
306 for ( ;(char *)de
<= kaddr
; de
++) {
309 /* check for . and .. */
310 if (de
->name
[0] != '.')
313 if (de
->inode
== cpu_to_fs16(SYSV_SB(sb
),
318 if (de
->name
[1] != '.' || de
->name
[2])
330 /* Releases the page */
331 void sysv_set_link(struct sysv_dir_entry
*de
, struct page
*page
,
334 struct inode
*dir
= page
->mapping
->host
;
335 loff_t pos
= page_offset(page
) +
336 (char *)de
-(char*)page_address(page
);
340 err
= sysv_prepare_chunk(page
, pos
, SYSV_DIRSIZE
);
342 de
->inode
= cpu_to_fs16(SYSV_SB(inode
->i_sb
), inode
->i_ino
);
343 err
= dir_commit_chunk(page
, pos
, SYSV_DIRSIZE
);
345 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME_SEC
;
346 mark_inode_dirty(dir
);
349 struct sysv_dir_entry
* sysv_dotdot (struct inode
*dir
, struct page
**p
)
351 struct page
*page
= dir_get_page(dir
, 0);
352 struct sysv_dir_entry
*de
= NULL
;
355 de
= (struct sysv_dir_entry
*) page_address(page
) + 1;
361 ino_t
sysv_inode_by_name(struct dentry
*dentry
)
364 struct sysv_dir_entry
*de
= sysv_find_entry (dentry
, &page
);
368 res
= fs16_to_cpu(SYSV_SB(dentry
->d_sb
), de
->inode
);