[PATCH] i386: clear segment register padding in core dumps
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / sysv / dir.c
blobebf7007fa1612ef41a16317594eeb006264aa7c5
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_mapping_page(mapping, n, NULL);
57 if (!IS_ERR(page)) {
58 wait_on_page_locked(page);
59 kmap(page);
60 if (!PageUptodate(page))
61 goto fail;
63 return page;
65 fail:
66 dir_put_page(page);
67 return ERR_PTR(-EIO);
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_path.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);
79 lock_kernel();
81 pos = (pos + SYSV_DIRSIZE-1) & ~(SYSV_DIRSIZE-1);
82 if (pos >= inode->i_size)
83 goto done;
85 for ( ; n < npages; n++, offset = 0) {
86 char *kaddr, *limit;
87 struct sysv_dir_entry *de;
88 struct page *page = dir_get_page(inode, n);
90 if (IS_ERR(page))
91 continue;
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;
97 int over;
99 if (!de->inode)
100 continue;
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),
107 DT_UNKNOWN);
108 if (over) {
109 dir_put_page(page);
110 goto done;
113 dir_put_page(page);
116 done:
117 filp->f_pos = ((loff_t)n << PAGE_CACHE_SHIFT) | offset;
118 unlock_kernel();
119 return 0;
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])
129 return 0;
130 return !memcmp(name, buffer, len);
134 * sysv_find_entry()
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;
151 *res_page = NULL;
153 start = SYSV_I(dir)->i_dir_start_lookup;
154 if (start >= npages)
155 start = 0;
156 n = start;
158 do {
159 char *kaddr;
160 page = dir_get_page(dir, n);
161 if (!IS_ERR(page)) {
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++) {
166 if (!de->inode)
167 continue;
168 if (namecompare(namelen, SYSV_NAMELEN,
169 name, de->name))
170 goto found;
173 dir_put_page(page);
175 if (++n >= npages)
176 n = 0;
177 } while (n != start);
179 return NULL;
181 found:
182 SYSV_I(dir)->i_dir_start_lookup = n;
183 *res_page = page;
184 return de;
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);
195 unsigned long n;
196 char *kaddr;
197 unsigned from, to;
198 int err;
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);
203 err = PTR_ERR(page);
204 if (IS_ERR(page))
205 goto out;
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) {
210 if (!de->inode)
211 goto got_it;
212 err = -EEXIST;
213 if (namecompare(namelen, SYSV_NAMELEN, name, de->name))
214 goto out_page;
215 de++;
217 dir_put_page(page);
219 BUG();
220 return -EINVAL;
222 got_it:
223 from = (char*)de - (char*)page_address(page);
224 to = from + SYSV_DIRSIZE;
225 lock_page(page);
226 err = page->mapping->a_ops->prepare_write(NULL, page, from, to);
227 if (err)
228 goto out_unlock;
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);
235 out_page:
236 dir_put_page(page);
237 out:
238 return err;
239 out_unlock:
240 unlock_page(page);
241 goto out_page;
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;
251 int err;
253 lock_page(page);
254 err = mapping->a_ops->prepare_write(NULL, page, from, to);
255 BUG_ON(err);
256 de->inode = 0;
257 err = dir_commit_chunk(page, from, to);
258 dir_put_page(page);
259 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
260 mark_inode_dirty(inode);
261 return err;
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;
269 char *base;
270 int err;
272 if (!page)
273 return -ENOMEM;
274 kmap(page);
275 err = mapping->a_ops->prepare_write(NULL, page, 0, 2 * SYSV_DIRSIZE);
276 if (err) {
277 unlock_page(page);
278 goto fail;
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,".");
287 de++;
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);
292 fail:
293 kunmap(page);
294 page_cache_release(page);
295 return err;
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++) {
308 char *kaddr;
309 struct sysv_dir_entry * de;
310 page = dir_get_page(inode, i);
312 if (IS_ERR(page))
313 continue;
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++) {
320 if (!de->inode)
321 continue;
322 /* check for . and .. */
323 if (de->name[0] != '.')
324 goto not_empty;
325 if (!de->name[1]) {
326 if (de->inode == cpu_to_fs16(SYSV_SB(sb),
327 inode->i_ino))
328 continue;
329 goto not_empty;
331 if (de->name[1] != '.' || de->name[2])
332 goto not_empty;
334 dir_put_page(page);
336 return 1;
338 not_empty:
339 dir_put_page(page);
340 return 0;
343 /* Releases the page */
344 void sysv_set_link(struct sysv_dir_entry *de, struct page *page,
345 struct inode *inode)
347 struct inode *dir = (struct inode*)page->mapping->host;
348 unsigned from = (char *)de-(char*)page_address(page);
349 unsigned to = from + SYSV_DIRSIZE;
350 int err;
352 lock_page(page);
353 err = page->mapping->a_ops->prepare_write(NULL, page, from, to);
354 BUG_ON(err);
355 de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), inode->i_ino);
356 err = dir_commit_chunk(page, from, to);
357 dir_put_page(page);
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;
367 if (!IS_ERR(page)) {
368 de = (struct sysv_dir_entry*) page_address(page) + 1;
369 *p = page;
371 return de;
374 ino_t sysv_inode_by_name(struct dentry *dentry)
376 struct page *page;
377 struct sysv_dir_entry *de = sysv_find_entry (dentry, &page);
378 ino_t res = 0;
380 if (de) {
381 res = fs16_to_cpu(SYSV_SB(dentry->d_sb), de->inode);
382 dir_put_page(page);
384 return res;