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_mapping_page(mapping, n, NULL);
58 wait_on_page_locked(page);
60 if (!PageUptodate(page))
70 static int sysv_readdir(struct file * filp, void * dirent, filldir_t filldir)
72 unsigned long pos = filp->f_pos;
73 struct inode *inode = filp->f_dentry->d_inode;
74 struct super_block *sb = inode->i_sb;
75 unsigned offset = pos & ~PAGE_CACHE_MASK;
76 unsigned long n = pos >> PAGE_CACHE_SHIFT;
77 unsigned long npages = dir_pages(inode);
81 pos = (pos + SYSV_DIRSIZE-1) & ~(SYSV_DIRSIZE-1);
82 if (pos >= inode->i_size)
85 for ( ; n < npages; n++, offset = 0) {
87 struct sysv_dir_entry *de;
88 struct page *page = dir_get_page(inode, n);
92 kaddr = (char *)page_address(page);
93 de = (struct sysv_dir_entry *)(kaddr+offset);
94 limit = kaddr + PAGE_CACHE_SIZE - SYSV_DIRSIZE;
95 for ( ;(char*)de <= limit; de++) {
96 char *name = de->name;
102 offset = (char *)de - kaddr;
104 over = filldir(dirent, name, strnlen(name,SYSV_NAMELEN),
105 ((loff_t)n<<PAGE_CACHE_SHIFT) | offset,
106 fs16_to_cpu(SYSV_SB(sb), de->inode),
117 filp->f_pos = ((loff_t)n << PAGE_CACHE_SHIFT) | offset;
122 /* compare strings: name[0..len-1] (not zero-terminated) and
123 * buffer[0..] (filled with zeroes up to buffer[0..maxlen-1])
125 static inline int namecompare(int len, int maxlen,
126 const char * name, const char * buffer)
128 if (len < maxlen && buffer[len])
130 return !memcmp(name, buffer, len);
136 * finds an entry in the specified directory with the wanted name. It
137 * returns the cache buffer in which the entry was found, and the entry
138 * itself (as a parameter - res_dir). It does NOT read the inode of the
139 * entry - you'll have to do that yourself if you want to.
141 struct sysv_dir_entry *sysv_find_entry(struct dentry *dentry, struct page **res_page)
143 const char * name = dentry->d_name.name;
144 int namelen = dentry->d_name.len;
145 struct inode * dir = dentry->d_parent->d_inode;
146 unsigned long start, n;
147 unsigned long npages = dir_pages(dir);
148 struct page *page = NULL;
149 struct sysv_dir_entry *de;
153 start = SYSV_I(dir)->i_dir_start_lookup;
160 page = dir_get_page(dir, n);
162 kaddr = (char*)page_address(page);
163 de = (struct sysv_dir_entry *) kaddr;
164 kaddr += PAGE_CACHE_SIZE - SYSV_DIRSIZE;
165 for ( ; (char *) de <= kaddr ; de++) {
168 if (namecompare(namelen, SYSV_NAMELEN,
177 } while (n != start);
182 SYSV_I(dir)->i_dir_start_lookup = n;
187 int sysv_add_link(struct dentry *dentry, struct inode *inode)
189 struct inode *dir = dentry->d_parent->d_inode;
190 const char * name = dentry->d_name.name;
191 int namelen = dentry->d_name.len;
192 struct page *page = NULL;
193 struct sysv_dir_entry * de;
194 unsigned long npages = dir_pages(dir);
200 /* We take care of directory expansion in the same loop */
201 for (n = 0; n <= npages; n++) {
202 page = dir_get_page(dir, n);
206 kaddr = (char*)page_address(page);
207 de = (struct sysv_dir_entry *)kaddr;
208 kaddr += PAGE_CACHE_SIZE - SYSV_DIRSIZE;
209 while ((char *)de <= kaddr) {
213 if (namecompare(namelen, SYSV_NAMELEN, name, de->name))
223 from = (char*)de - (char*)page_address(page);
224 to = from + SYSV_DIRSIZE;
226 err = page->mapping->a_ops->prepare_write(NULL, page, from, to);
229 memcpy (de->name, name, namelen);
230 memset (de->name + namelen, 0, SYSV_DIRSIZE - namelen - 2);
231 de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), inode->i_ino);
232 err = dir_commit_chunk(page, from, to);
233 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
234 mark_inode_dirty(dir);
244 int sysv_delete_entry(struct sysv_dir_entry *de, struct page *page)
246 struct address_space *mapping = page->mapping;
247 struct inode *inode = (struct inode*)mapping->host;
248 char *kaddr = (char*)page_address(page);
249 unsigned from = (char*)de - kaddr;
250 unsigned to = from + SYSV_DIRSIZE;
254 err = mapping->a_ops->prepare_write(NULL, page, from, to);
257 err = dir_commit_chunk(page, from, to);
259 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
260 mark_inode_dirty(inode);
264 int sysv_make_empty(struct inode *inode, struct inode *dir)
266 struct address_space *mapping = inode->i_mapping;
267 struct page *page = grab_cache_page(mapping, 0);
268 struct sysv_dir_entry * de;
275 err = mapping->a_ops->prepare_write(NULL, page, 0, 2 * SYSV_DIRSIZE);
281 base = (char*)page_address(page);
282 memset(base, 0, PAGE_CACHE_SIZE);
284 de = (struct sysv_dir_entry *) base;
285 de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), inode->i_ino);
286 strcpy(de->name,".");
288 de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), dir->i_ino);
289 strcpy(de->name,"..");
291 err = dir_commit_chunk(page, 0, 2 * SYSV_DIRSIZE);
294 page_cache_release(page);
299 * routine to check that the specified directory is empty (for rmdir)
301 int sysv_empty_dir(struct inode * inode)
303 struct super_block *sb = inode->i_sb;
304 struct page *page = NULL;
305 unsigned long i, npages = dir_pages(inode);
307 for (i = 0; i < npages; i++) {
309 struct sysv_dir_entry * de;
310 page = dir_get_page(inode, i);
315 kaddr = (char *)page_address(page);
316 de = (struct sysv_dir_entry *)kaddr;
317 kaddr += PAGE_CACHE_SIZE-SYSV_DIRSIZE;
319 for ( ;(char *)de <= kaddr; de++) {
322 /* check for . and .. */
323 if (de->name[0] != '.')
326 if (de->inode == cpu_to_fs16(SYSV_SB(sb),
331 if (de->name[1] != '.' || de->name[2])
343 /* Releases the page */
344 void sysv_set_link(struct sysv_dir_entry *de, struct page *page,
347 struct inode *dir = (struct inode*)page->mapping->host;
348 unsigned from = (char *)de-(char*)page_address(page);
349 unsigned to = from + SYSV_DIRSIZE;
353 err = page->mapping->a_ops->prepare_write(NULL, page, from, to);
355 de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), inode->i_ino);
356 err = dir_commit_chunk(page, from, to);
358 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
359 mark_inode_dirty(dir);
362 struct sysv_dir_entry * sysv_dotdot (struct inode *dir, struct page **p)
364 struct page *page = dir_get_page(dir, 0);
365 struct sysv_dir_entry *de = NULL;
368 de = (struct sysv_dir_entry*) page_address(page) + 1;
374 ino_t sysv_inode_by_name(struct dentry *dentry)
377 struct sysv_dir_entry *de = sysv_find_entry (dentry, &page);
381 res = fs16_to_cpu(SYSV_SB(dentry->d_sb), de->inode);