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_shared = sysv_readdir,
27 .fsync = generic_file_fsync,
30 static inline void dir_put_page(struct page *page)
36 static int dir_commit_chunk(struct page *page, loff_t pos, unsigned len)
38 struct address_space *mapping = page->mapping;
39 struct inode *dir = mapping->host;
42 block_write_end(NULL, mapping, pos, len, len, page, NULL);
43 if (pos+len > dir->i_size) {
44 i_size_write(dir, pos+len);
45 mark_inode_dirty(dir);
48 err = write_one_page(page);
54 static struct page * dir_get_page(struct inode *dir, unsigned long n)
56 struct address_space *mapping = dir->i_mapping;
57 struct page *page = read_mapping_page(mapping, n, NULL);
63 static int sysv_readdir(struct file *file, struct dir_context *ctx)
65 unsigned long pos = ctx->pos;
66 struct inode *inode = file_inode(file);
67 struct super_block *sb = inode->i_sb;
68 unsigned long npages = dir_pages(inode);
72 ctx->pos = pos = (pos + SYSV_DIRSIZE-1) & ~(SYSV_DIRSIZE-1);
73 if (pos >= inode->i_size)
76 offset = pos & ~PAGE_MASK;
77 n = pos >> PAGE_SHIFT;
79 for ( ; n < npages; n++, offset = 0) {
81 struct sysv_dir_entry *de;
82 struct page *page = dir_get_page(inode, n);
86 kaddr = (char *)page_address(page);
87 de = (struct sysv_dir_entry *)(kaddr+offset);
88 limit = kaddr + PAGE_SIZE - SYSV_DIRSIZE;
89 for ( ;(char*)de <= limit; de++, ctx->pos += sizeof(*de)) {
90 char *name = de->name;
95 if (!dir_emit(ctx, name, strnlen(name,SYSV_NAMELEN),
96 fs16_to_cpu(SYSV_SB(sb), de->inode),
107 /* compare strings: name[0..len-1] (not zero-terminated) and
108 * buffer[0..] (filled with zeroes up to buffer[0..maxlen-1])
110 static inline int namecompare(int len, int maxlen,
111 const char * name, const char * buffer)
113 if (len < maxlen && buffer[len])
115 return !memcmp(name, buffer, len);
121 * finds an entry in the specified directory with the wanted name. It
122 * returns the cache buffer in which the entry was found, and the entry
123 * itself (as a parameter - res_dir). It does NOT read the inode of the
124 * entry - you'll have to do that yourself if you want to.
126 struct sysv_dir_entry *sysv_find_entry(struct dentry *dentry, struct page **res_page)
128 const char * name = dentry->d_name.name;
129 int namelen = dentry->d_name.len;
130 struct inode * dir = d_inode(dentry->d_parent);
131 unsigned long start, n;
132 unsigned long npages = dir_pages(dir);
133 struct page *page = NULL;
134 struct sysv_dir_entry *de;
138 start = SYSV_I(dir)->i_dir_start_lookup;
145 page = dir_get_page(dir, n);
147 kaddr = (char*)page_address(page);
148 de = (struct sysv_dir_entry *) kaddr;
149 kaddr += PAGE_SIZE - SYSV_DIRSIZE;
150 for ( ; (char *) de <= kaddr ; de++) {
153 if (namecompare(namelen, SYSV_NAMELEN,
162 } while (n != start);
167 SYSV_I(dir)->i_dir_start_lookup = n;
172 int sysv_add_link(struct dentry *dentry, struct inode *inode)
174 struct inode *dir = d_inode(dentry->d_parent);
175 const char * name = dentry->d_name.name;
176 int namelen = dentry->d_name.len;
177 struct page *page = NULL;
178 struct sysv_dir_entry * de;
179 unsigned long npages = dir_pages(dir);
185 /* We take care of directory expansion in the same loop */
186 for (n = 0; n <= npages; n++) {
187 page = dir_get_page(dir, n);
191 kaddr = (char*)page_address(page);
192 de = (struct sysv_dir_entry *)kaddr;
193 kaddr += PAGE_SIZE - SYSV_DIRSIZE;
194 while ((char *)de <= kaddr) {
198 if (namecompare(namelen, SYSV_NAMELEN, name, de->name))
208 pos = page_offset(page) +
209 (char*)de - (char*)page_address(page);
211 err = sysv_prepare_chunk(page, pos, SYSV_DIRSIZE);
214 memcpy (de->name, name, namelen);
215 memset (de->name + namelen, 0, SYSV_DIRSIZE - namelen - 2);
216 de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), inode->i_ino);
217 err = dir_commit_chunk(page, pos, SYSV_DIRSIZE);
218 dir->i_mtime = dir->i_ctime = current_time(dir);
219 mark_inode_dirty(dir);
229 int sysv_delete_entry(struct sysv_dir_entry *de, struct page *page)
231 struct inode *inode = page->mapping->host;
232 char *kaddr = (char*)page_address(page);
233 loff_t pos = page_offset(page) + (char *)de - kaddr;
237 err = sysv_prepare_chunk(page, pos, SYSV_DIRSIZE);
240 err = dir_commit_chunk(page, pos, SYSV_DIRSIZE);
242 inode->i_ctime = inode->i_mtime = current_time(inode);
243 mark_inode_dirty(inode);
247 int sysv_make_empty(struct inode *inode, struct inode *dir)
249 struct page *page = grab_cache_page(inode->i_mapping, 0);
250 struct sysv_dir_entry * de;
256 err = sysv_prepare_chunk(page, 0, 2 * SYSV_DIRSIZE);
263 base = (char*)page_address(page);
264 memset(base, 0, PAGE_SIZE);
266 de = (struct sysv_dir_entry *) base;
267 de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), inode->i_ino);
268 strcpy(de->name,".");
270 de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), dir->i_ino);
271 strcpy(de->name,"..");
274 err = dir_commit_chunk(page, 0, 2 * SYSV_DIRSIZE);
281 * routine to check that the specified directory is empty (for rmdir)
283 int sysv_empty_dir(struct inode * inode)
285 struct super_block *sb = inode->i_sb;
286 struct page *page = NULL;
287 unsigned long i, npages = dir_pages(inode);
289 for (i = 0; i < npages; i++) {
291 struct sysv_dir_entry * de;
292 page = dir_get_page(inode, i);
297 kaddr = (char *)page_address(page);
298 de = (struct sysv_dir_entry *)kaddr;
299 kaddr += PAGE_SIZE-SYSV_DIRSIZE;
301 for ( ;(char *)de <= kaddr; de++) {
304 /* check for . and .. */
305 if (de->name[0] != '.')
308 if (de->inode == cpu_to_fs16(SYSV_SB(sb),
313 if (de->name[1] != '.' || de->name[2])
325 /* Releases the page */
326 void sysv_set_link(struct sysv_dir_entry *de, struct page *page,
329 struct inode *dir = page->mapping->host;
330 loff_t pos = page_offset(page) +
331 (char *)de-(char*)page_address(page);
335 err = sysv_prepare_chunk(page, pos, SYSV_DIRSIZE);
337 de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), inode->i_ino);
338 err = dir_commit_chunk(page, pos, SYSV_DIRSIZE);
340 dir->i_mtime = dir->i_ctime = current_time(dir);
341 mark_inode_dirty(dir);
344 struct sysv_dir_entry * sysv_dotdot (struct inode *dir, struct page **p)
346 struct page *page = dir_get_page(dir, 0);
347 struct sysv_dir_entry *de = NULL;
350 de = (struct sysv_dir_entry*) page_address(page) + 1;
356 ino_t sysv_inode_by_name(struct dentry *dentry)
359 struct sysv_dir_entry *de = sysv_find_entry (dentry, &page);
363 res = fs16_to_cpu(SYSV_SB(dentry->d_sb), de->inode);