xfs: fix type usage
[linux/fpc-iii.git] / fs / sysv / dir.c
blob88e38cd8f5c9ae435979a71c367d22f65de26759
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * linux/fs/sysv/dir.c
5 * minix/dir.c
6 * Copyright (C) 1991, 1992 Linus Torvalds
8 * coh/dir.c
9 * Copyright (C) 1993 Pascal Haible, Bruno Haible
11 * sysv/dir.c
12 * Copyright (C) 1993 Bruno Haible
14 * SystemV/Coherent directory handling functions
17 #include <linux/pagemap.h>
18 #include <linux/highmem.h>
19 #include <linux/swap.h>
20 #include "sysv.h"
22 static int sysv_readdir(struct file *, struct dir_context *);
24 const struct file_operations sysv_dir_operations = {
25 .llseek = generic_file_llseek,
26 .read = generic_read_dir,
27 .iterate_shared = sysv_readdir,
28 .fsync = generic_file_fsync,
31 static inline void dir_put_page(struct page *page)
33 kunmap(page);
34 put_page(page);
37 static int dir_commit_chunk(struct page *page, loff_t pos, unsigned len)
39 struct address_space *mapping = page->mapping;
40 struct inode *dir = mapping->host;
41 int err = 0;
43 block_write_end(NULL, mapping, pos, len, len, page, NULL);
44 if (pos+len > dir->i_size) {
45 i_size_write(dir, pos+len);
46 mark_inode_dirty(dir);
48 if (IS_DIRSYNC(dir))
49 err = write_one_page(page);
50 else
51 unlock_page(page);
52 return err;
55 static struct page * dir_get_page(struct inode *dir, unsigned long n)
57 struct address_space *mapping = dir->i_mapping;
58 struct page *page = read_mapping_page(mapping, n, NULL);
59 if (!IS_ERR(page))
60 kmap(page);
61 return page;
64 static int sysv_readdir(struct file *file, struct dir_context *ctx)
66 unsigned long pos = ctx->pos;
67 struct inode *inode = file_inode(file);
68 struct super_block *sb = inode->i_sb;
69 unsigned long npages = dir_pages(inode);
70 unsigned offset;
71 unsigned long n;
73 ctx->pos = pos = (pos + SYSV_DIRSIZE-1) & ~(SYSV_DIRSIZE-1);
74 if (pos >= inode->i_size)
75 return 0;
77 offset = pos & ~PAGE_MASK;
78 n = pos >> PAGE_SHIFT;
80 for ( ; n < npages; n++, offset = 0) {
81 char *kaddr, *limit;
82 struct sysv_dir_entry *de;
83 struct page *page = dir_get_page(inode, n);
85 if (IS_ERR(page))
86 continue;
87 kaddr = (char *)page_address(page);
88 de = (struct sysv_dir_entry *)(kaddr+offset);
89 limit = kaddr + PAGE_SIZE - SYSV_DIRSIZE;
90 for ( ;(char*)de <= limit; de++, ctx->pos += sizeof(*de)) {
91 char *name = de->name;
93 if (!de->inode)
94 continue;
96 if (!dir_emit(ctx, name, strnlen(name,SYSV_NAMELEN),
97 fs16_to_cpu(SYSV_SB(sb), de->inode),
98 DT_UNKNOWN)) {
99 dir_put_page(page);
100 return 0;
103 dir_put_page(page);
105 return 0;
108 /* compare strings: name[0..len-1] (not zero-terminated) and
109 * buffer[0..] (filled with zeroes up to buffer[0..maxlen-1])
111 static inline int namecompare(int len, int maxlen,
112 const char * name, const char * buffer)
114 if (len < maxlen && buffer[len])
115 return 0;
116 return !memcmp(name, buffer, len);
120 * sysv_find_entry()
122 * finds an entry in the specified directory with the wanted name. It
123 * returns the cache buffer in which the entry was found, and the entry
124 * itself (as a parameter - res_dir). It does NOT read the inode of the
125 * entry - you'll have to do that yourself if you want to.
127 struct sysv_dir_entry *sysv_find_entry(struct dentry *dentry, struct page **res_page)
129 const char * name = dentry->d_name.name;
130 int namelen = dentry->d_name.len;
131 struct inode * dir = d_inode(dentry->d_parent);
132 unsigned long start, n;
133 unsigned long npages = dir_pages(dir);
134 struct page *page = NULL;
135 struct sysv_dir_entry *de;
137 *res_page = NULL;
139 start = SYSV_I(dir)->i_dir_start_lookup;
140 if (start >= npages)
141 start = 0;
142 n = start;
144 do {
145 char *kaddr;
146 page = dir_get_page(dir, n);
147 if (!IS_ERR(page)) {
148 kaddr = (char*)page_address(page);
149 de = (struct sysv_dir_entry *) kaddr;
150 kaddr += PAGE_SIZE - SYSV_DIRSIZE;
151 for ( ; (char *) de <= kaddr ; de++) {
152 if (!de->inode)
153 continue;
154 if (namecompare(namelen, SYSV_NAMELEN,
155 name, de->name))
156 goto found;
158 dir_put_page(page);
161 if (++n >= npages)
162 n = 0;
163 } while (n != start);
165 return NULL;
167 found:
168 SYSV_I(dir)->i_dir_start_lookup = n;
169 *res_page = page;
170 return de;
173 int sysv_add_link(struct dentry *dentry, struct inode *inode)
175 struct inode *dir = d_inode(dentry->d_parent);
176 const char * name = dentry->d_name.name;
177 int namelen = dentry->d_name.len;
178 struct page *page = NULL;
179 struct sysv_dir_entry * de;
180 unsigned long npages = dir_pages(dir);
181 unsigned long n;
182 char *kaddr;
183 loff_t pos;
184 int err;
186 /* We take care of directory expansion in the same loop */
187 for (n = 0; n <= npages; n++) {
188 page = dir_get_page(dir, n);
189 err = PTR_ERR(page);
190 if (IS_ERR(page))
191 goto out;
192 kaddr = (char*)page_address(page);
193 de = (struct sysv_dir_entry *)kaddr;
194 kaddr += PAGE_SIZE - SYSV_DIRSIZE;
195 while ((char *)de <= kaddr) {
196 if (!de->inode)
197 goto got_it;
198 err = -EEXIST;
199 if (namecompare(namelen, SYSV_NAMELEN, name, de->name))
200 goto out_page;
201 de++;
203 dir_put_page(page);
205 BUG();
206 return -EINVAL;
208 got_it:
209 pos = page_offset(page) +
210 (char*)de - (char*)page_address(page);
211 lock_page(page);
212 err = sysv_prepare_chunk(page, pos, SYSV_DIRSIZE);
213 if (err)
214 goto out_unlock;
215 memcpy (de->name, name, namelen);
216 memset (de->name + namelen, 0, SYSV_DIRSIZE - namelen - 2);
217 de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), inode->i_ino);
218 err = dir_commit_chunk(page, pos, SYSV_DIRSIZE);
219 dir->i_mtime = dir->i_ctime = current_time(dir);
220 mark_inode_dirty(dir);
221 out_page:
222 dir_put_page(page);
223 out:
224 return err;
225 out_unlock:
226 unlock_page(page);
227 goto out_page;
230 int sysv_delete_entry(struct sysv_dir_entry *de, struct page *page)
232 struct inode *inode = page->mapping->host;
233 char *kaddr = (char*)page_address(page);
234 loff_t pos = page_offset(page) + (char *)de - kaddr;
235 int err;
237 lock_page(page);
238 err = sysv_prepare_chunk(page, pos, SYSV_DIRSIZE);
239 BUG_ON(err);
240 de->inode = 0;
241 err = dir_commit_chunk(page, pos, SYSV_DIRSIZE);
242 dir_put_page(page);
243 inode->i_ctime = inode->i_mtime = current_time(inode);
244 mark_inode_dirty(inode);
245 return err;
248 int sysv_make_empty(struct inode *inode, struct inode *dir)
250 struct page *page = grab_cache_page(inode->i_mapping, 0);
251 struct sysv_dir_entry * de;
252 char *base;
253 int err;
255 if (!page)
256 return -ENOMEM;
257 err = sysv_prepare_chunk(page, 0, 2 * SYSV_DIRSIZE);
258 if (err) {
259 unlock_page(page);
260 goto fail;
262 kmap(page);
264 base = (char*)page_address(page);
265 memset(base, 0, PAGE_SIZE);
267 de = (struct sysv_dir_entry *) base;
268 de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), inode->i_ino);
269 strcpy(de->name,".");
270 de++;
271 de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), dir->i_ino);
272 strcpy(de->name,"..");
274 kunmap(page);
275 err = dir_commit_chunk(page, 0, 2 * SYSV_DIRSIZE);
276 fail:
277 put_page(page);
278 return err;
282 * routine to check that the specified directory is empty (for rmdir)
284 int sysv_empty_dir(struct inode * inode)
286 struct super_block *sb = inode->i_sb;
287 struct page *page = NULL;
288 unsigned long i, npages = dir_pages(inode);
290 for (i = 0; i < npages; i++) {
291 char *kaddr;
292 struct sysv_dir_entry * de;
293 page = dir_get_page(inode, i);
295 if (IS_ERR(page))
296 continue;
298 kaddr = (char *)page_address(page);
299 de = (struct sysv_dir_entry *)kaddr;
300 kaddr += PAGE_SIZE-SYSV_DIRSIZE;
302 for ( ;(char *)de <= kaddr; de++) {
303 if (!de->inode)
304 continue;
305 /* check for . and .. */
306 if (de->name[0] != '.')
307 goto not_empty;
308 if (!de->name[1]) {
309 if (de->inode == cpu_to_fs16(SYSV_SB(sb),
310 inode->i_ino))
311 continue;
312 goto not_empty;
314 if (de->name[1] != '.' || de->name[2])
315 goto not_empty;
317 dir_put_page(page);
319 return 1;
321 not_empty:
322 dir_put_page(page);
323 return 0;
326 /* Releases the page */
327 void sysv_set_link(struct sysv_dir_entry *de, struct page *page,
328 struct inode *inode)
330 struct inode *dir = page->mapping->host;
331 loff_t pos = page_offset(page) +
332 (char *)de-(char*)page_address(page);
333 int err;
335 lock_page(page);
336 err = sysv_prepare_chunk(page, pos, SYSV_DIRSIZE);
337 BUG_ON(err);
338 de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), inode->i_ino);
339 err = dir_commit_chunk(page, pos, SYSV_DIRSIZE);
340 dir_put_page(page);
341 dir->i_mtime = dir->i_ctime = current_time(dir);
342 mark_inode_dirty(dir);
345 struct sysv_dir_entry * sysv_dotdot (struct inode *dir, struct page **p)
347 struct page *page = dir_get_page(dir, 0);
348 struct sysv_dir_entry *de = NULL;
350 if (!IS_ERR(page)) {
351 de = (struct sysv_dir_entry*) page_address(page) + 1;
352 *p = page;
354 return de;
357 ino_t sysv_inode_by_name(struct dentry *dentry)
359 struct page *page;
360 struct sysv_dir_entry *de = sysv_find_entry (dentry, &page);
361 ino_t res = 0;
363 if (de) {
364 res = fs16_to_cpu(SYSV_SB(dentry->d_sb), de->inode);
365 dir_put_page(page);
367 return res;