[ALSA] Remove xxx_t typedefs: PCI emu10k1
[linux-2.6/s3c2410-cpufreq.git] / fs / sysv / dir.c
blob69a085abad6f8b12ffece70c95b1db0c5b5502ba
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 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 (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 = (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 if (err)
257 BUG();
258 de->inode = 0;
259 err = dir_commit_chunk(page, from, to);
260 dir_put_page(page);
261 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
262 mark_inode_dirty(inode);
263 return err;
266 int sysv_make_empty(struct inode *inode, struct inode *dir)
268 struct address_space *mapping = inode->i_mapping;
269 struct page *page = grab_cache_page(mapping, 0);
270 struct sysv_dir_entry * de;
271 char *base;
272 int err;
274 if (!page)
275 return -ENOMEM;
276 kmap(page);
277 err = mapping->a_ops->prepare_write(NULL, page, 0, 2 * SYSV_DIRSIZE);
278 if (err) {
279 unlock_page(page);
280 goto fail;
283 base = (char*)page_address(page);
284 memset(base, 0, PAGE_CACHE_SIZE);
286 de = (struct sysv_dir_entry *) base;
287 de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), inode->i_ino);
288 strcpy(de->name,".");
289 de++;
290 de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), dir->i_ino);
291 strcpy(de->name,"..");
293 err = dir_commit_chunk(page, 0, 2 * SYSV_DIRSIZE);
294 fail:
295 kunmap(page);
296 page_cache_release(page);
297 return err;
301 * routine to check that the specified directory is empty (for rmdir)
303 int sysv_empty_dir(struct inode * inode)
305 struct super_block *sb = inode->i_sb;
306 struct page *page = NULL;
307 unsigned long i, npages = dir_pages(inode);
309 for (i = 0; i < npages; i++) {
310 char *kaddr;
311 struct sysv_dir_entry * de;
312 page = dir_get_page(inode, i);
314 if (IS_ERR(page))
315 continue;
317 kaddr = (char *)page_address(page);
318 de = (struct sysv_dir_entry *)kaddr;
319 kaddr += PAGE_CACHE_SIZE-SYSV_DIRSIZE;
321 for ( ;(char *)de <= kaddr; de++) {
322 if (!de->inode)
323 continue;
324 /* check for . and .. */
325 if (de->name[0] != '.')
326 goto not_empty;
327 if (!de->name[1]) {
328 if (de->inode == cpu_to_fs16(SYSV_SB(sb),
329 inode->i_ino))
330 continue;
331 goto not_empty;
333 if (de->name[1] != '.' || de->name[2])
334 goto not_empty;
336 dir_put_page(page);
338 return 1;
340 not_empty:
341 dir_put_page(page);
342 return 0;
345 /* Releases the page */
346 void sysv_set_link(struct sysv_dir_entry *de, struct page *page,
347 struct inode *inode)
349 struct inode *dir = (struct inode*)page->mapping->host;
350 unsigned from = (char *)de-(char*)page_address(page);
351 unsigned to = from + SYSV_DIRSIZE;
352 int err;
354 lock_page(page);
355 err = page->mapping->a_ops->prepare_write(NULL, page, from, to);
356 if (err)
357 BUG();
358 de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), inode->i_ino);
359 err = dir_commit_chunk(page, from, to);
360 dir_put_page(page);
361 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
362 mark_inode_dirty(dir);
365 struct sysv_dir_entry * sysv_dotdot (struct inode *dir, struct page **p)
367 struct page *page = dir_get_page(dir, 0);
368 struct sysv_dir_entry *de = NULL;
370 if (!IS_ERR(page)) {
371 de = (struct sysv_dir_entry*) page_address(page) + 1;
372 *p = page;
374 return de;
377 ino_t sysv_inode_by_name(struct dentry *dentry)
379 struct page *page;
380 struct sysv_dir_entry *de = sysv_find_entry (dentry, &page);
381 ino_t res = 0;
383 if (de) {
384 res = fs16_to_cpu(SYSV_SB(dentry->d_sb), de->inode);
385 dir_put_page(page);
387 return res;