Linux 2.6.17.7
[linux/fpc-iii.git] / fs / sysv / dir.c
blobd7074341ee87e4996715bfbca1bbe2210c62d9e8
1 /*
2 * linux/fs/sysv/dir.c
4 * minix/dir.c
5 * Copyright (C) 1991, 1992 Linus Torvalds
7 * coh/dir.c
8 * Copyright (C) 1993 Pascal Haible, Bruno Haible
10 * sysv/dir.c
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>
19 #include "sysv.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)
31 kunmap(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;
43 int err = 0;
45 page->mapping->a_ops->commit_write(NULL, page, from, to);
46 if (IS_DIRSYNC(dir))
47 err = write_one_page(page, 1);
48 else
49 unlock_page(page);
50 return err;
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);
58 if (!IS_ERR(page)) {
59 wait_on_page_locked(page);
60 kmap(page);
61 if (!PageUptodate(page))
62 goto fail;
64 return page;
66 fail:
67 dir_put_page(page);
68 return ERR_PTR(-EIO);
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);
80 lock_kernel();
82 pos = (pos + SYSV_DIRSIZE-1) & ~(SYSV_DIRSIZE-1);
83 if (pos >= inode->i_size)
84 goto done;
86 for ( ; n < npages; n++, offset = 0) {
87 char *kaddr, *limit;
88 struct sysv_dir_entry *de;
89 struct page *page = dir_get_page(inode, n);
91 if (IS_ERR(page))
92 continue;
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;
98 int over;
100 if (!de->inode)
101 continue;
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),
108 DT_UNKNOWN);
109 if (over) {
110 dir_put_page(page);
111 goto done;
114 dir_put_page(page);
117 done:
118 filp->f_pos = ((loff_t)n << PAGE_CACHE_SHIFT) | offset;
119 unlock_kernel();
120 return 0;
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])
130 return 0;
131 return !memcmp(name, buffer, len);
135 * sysv_find_entry()
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;
152 *res_page = NULL;
154 start = SYSV_I(dir)->i_dir_start_lookup;
155 if (start >= npages)
156 start = 0;
157 n = start;
159 do {
160 char *kaddr;
161 page = dir_get_page(dir, n);
162 if (!IS_ERR(page)) {
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++) {
167 if (!de->inode)
168 continue;
169 if (namecompare(namelen, SYSV_NAMELEN,
170 name, de->name))
171 goto found;
174 dir_put_page(page);
176 if (++n >= npages)
177 n = 0;
178 } while (n != start);
180 return NULL;
182 found:
183 SYSV_I(dir)->i_dir_start_lookup = n;
184 *res_page = page;
185 return de;
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);
196 unsigned long n;
197 char *kaddr;
198 unsigned from, to;
199 int err;
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);
204 err = PTR_ERR(page);
205 if (IS_ERR(page))
206 goto out;
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) {
211 if (!de->inode)
212 goto got_it;
213 err = -EEXIST;
214 if (namecompare(namelen, SYSV_NAMELEN, name, de->name))
215 goto out_page;
216 de++;
218 dir_put_page(page);
220 BUG();
221 return -EINVAL;
223 got_it:
224 from = (char*)de - (char*)page_address(page);
225 to = from + SYSV_DIRSIZE;
226 lock_page(page);
227 err = page->mapping->a_ops->prepare_write(NULL, page, from, to);
228 if (err)
229 goto out_unlock;
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);
236 out_page:
237 dir_put_page(page);
238 out:
239 return err;
240 out_unlock:
241 unlock_page(page);
242 goto out_page;
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;
252 int err;
254 lock_page(page);
255 err = mapping->a_ops->prepare_write(NULL, page, from, to);
256 BUG_ON(err);
257 de->inode = 0;
258 err = dir_commit_chunk(page, from, to);
259 dir_put_page(page);
260 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
261 mark_inode_dirty(inode);
262 return err;
265 int sysv_make_empty(struct inode *inode, struct inode *dir)
267 struct address_space *mapping = inode->i_mapping;
268 struct page *page = grab_cache_page(mapping, 0);
269 struct sysv_dir_entry * de;
270 char *base;
271 int err;
273 if (!page)
274 return -ENOMEM;
275 kmap(page);
276 err = mapping->a_ops->prepare_write(NULL, page, 0, 2 * SYSV_DIRSIZE);
277 if (err) {
278 unlock_page(page);
279 goto fail;
282 base = (char*)page_address(page);
283 memset(base, 0, PAGE_CACHE_SIZE);
285 de = (struct sysv_dir_entry *) base;
286 de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), inode->i_ino);
287 strcpy(de->name,".");
288 de++;
289 de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), dir->i_ino);
290 strcpy(de->name,"..");
292 err = dir_commit_chunk(page, 0, 2 * SYSV_DIRSIZE);
293 fail:
294 kunmap(page);
295 page_cache_release(page);
296 return err;
300 * routine to check that the specified directory is empty (for rmdir)
302 int sysv_empty_dir(struct inode * inode)
304 struct super_block *sb = inode->i_sb;
305 struct page *page = NULL;
306 unsigned long i, npages = dir_pages(inode);
308 for (i = 0; i < npages; i++) {
309 char *kaddr;
310 struct sysv_dir_entry * de;
311 page = dir_get_page(inode, i);
313 if (IS_ERR(page))
314 continue;
316 kaddr = (char *)page_address(page);
317 de = (struct sysv_dir_entry *)kaddr;
318 kaddr += PAGE_CACHE_SIZE-SYSV_DIRSIZE;
320 for ( ;(char *)de <= kaddr; de++) {
321 if (!de->inode)
322 continue;
323 /* check for . and .. */
324 if (de->name[0] != '.')
325 goto not_empty;
326 if (!de->name[1]) {
327 if (de->inode == cpu_to_fs16(SYSV_SB(sb),
328 inode->i_ino))
329 continue;
330 goto not_empty;
332 if (de->name[1] != '.' || de->name[2])
333 goto not_empty;
335 dir_put_page(page);
337 return 1;
339 not_empty:
340 dir_put_page(page);
341 return 0;
344 /* Releases the page */
345 void sysv_set_link(struct sysv_dir_entry *de, struct page *page,
346 struct inode *inode)
348 struct inode *dir = (struct inode*)page->mapping->host;
349 unsigned from = (char *)de-(char*)page_address(page);
350 unsigned to = from + SYSV_DIRSIZE;
351 int err;
353 lock_page(page);
354 err = page->mapping->a_ops->prepare_write(NULL, page, from, to);
355 BUG_ON(err);
356 de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), inode->i_ino);
357 err = dir_commit_chunk(page, from, to);
358 dir_put_page(page);
359 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
360 mark_inode_dirty(dir);
363 struct sysv_dir_entry * sysv_dotdot (struct inode *dir, struct page **p)
365 struct page *page = dir_get_page(dir, 0);
366 struct sysv_dir_entry *de = NULL;
368 if (!IS_ERR(page)) {
369 de = (struct sysv_dir_entry*) page_address(page) + 1;
370 *p = page;
372 return de;
375 ino_t sysv_inode_by_name(struct dentry *dentry)
377 struct page *page;
378 struct sysv_dir_entry *de = sysv_find_entry (dentry, &page);
379 ino_t res = 0;
381 if (de) {
382 res = fs16_to_cpu(SYSV_SB(dentry->d_sb), de->inode);
383 dir_put_page(page);
385 return res;