blob: d7074341ee87e4996715bfbca1bbe2210c62d9e8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/sysv/dir.c
3 *
4 * minix/dir.c
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 *
7 * coh/dir.c
8 * Copyright (C) 1993 Pascal Haible, Bruno Haible
9 *
10 * sysv/dir.c
11 * Copyright (C) 1993 Bruno Haible
12 *
13 * SystemV/Coherent directory handling functions
14 */
15
16#include <linux/pagemap.h>
17#include <linux/highmem.h>
18#include <linux/smp_lock.h>
19#include "sysv.h"
20
21static int sysv_readdir(struct file *, void *, filldir_t);
22
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080023const struct file_operations sysv_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 .read = generic_read_dir,
25 .readdir = sysv_readdir,
26 .fsync = sysv_sync_file,
27};
28
29static inline void dir_put_page(struct page *page)
30{
31 kunmap(page);
32 page_cache_release(page);
33}
34
35static inline unsigned long dir_pages(struct inode *inode)
36{
37 return (inode->i_size+PAGE_CACHE_SIZE-1)>>PAGE_CACHE_SHIFT;
38}
39
40static int dir_commit_chunk(struct page *page, unsigned from, unsigned to)
41{
42 struct inode *dir = (struct inode *)page->mapping->host;
43 int err = 0;
44
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;
51}
52
53static struct page * dir_get_page(struct inode *dir, unsigned long n)
54{
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;
63 }
64 return page;
65
66fail:
67 dir_put_page(page);
68 return ERR_PTR(-EIO);
69}
70
71static int sysv_readdir(struct file * filp, void * dirent, filldir_t filldir)
72{
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);
79
80 lock_kernel();
81
82 pos = (pos + SYSV_DIRSIZE-1) & ~(SYSV_DIRSIZE-1);
83 if (pos >= inode->i_size)
84 goto done;
85
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);
90
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;
99
100 if (!de->inode)
101 continue;
102
103 offset = (char *)de - kaddr;
104
105 over = filldir(dirent, name, strnlen(name,SYSV_NAMELEN),
Andrew Morton54b21a72006-01-08 01:03:05 -0800106 ((loff_t)n<<PAGE_CACHE_SHIFT) | offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 fs16_to_cpu(SYSV_SB(sb), de->inode),
108 DT_UNKNOWN);
109 if (over) {
110 dir_put_page(page);
111 goto done;
112 }
113 }
114 dir_put_page(page);
115 }
116
117done:
Andrew Morton54b21a72006-01-08 01:03:05 -0800118 filp->f_pos = ((loff_t)n << PAGE_CACHE_SHIFT) | offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 unlock_kernel();
120 return 0;
121}
122
123/* compare strings: name[0..len-1] (not zero-terminated) and
124 * buffer[0..] (filled with zeroes up to buffer[0..maxlen-1])
125 */
126static inline int namecompare(int len, int maxlen,
127 const char * name, const char * buffer)
128{
129 if (len < maxlen && buffer[len])
130 return 0;
131 return !memcmp(name, buffer, len);
132}
133
134/*
135 * sysv_find_entry()
136 *
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.
141 */
142struct sysv_dir_entry *sysv_find_entry(struct dentry *dentry, struct page **res_page)
143{
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;
151
152 *res_page = NULL;
153
154 start = SYSV_I(dir)->i_dir_start_lookup;
155 if (start >= npages)
156 start = 0;
157 n = start;
158
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;
172 }
173 }
174 dir_put_page(page);
175
176 if (++n >= npages)
177 n = 0;
178 } while (n != start);
179
180 return NULL;
181
182found:
183 SYSV_I(dir)->i_dir_start_lookup = n;
184 *res_page = page;
185 return de;
186}
187
188int sysv_add_link(struct dentry *dentry, struct inode *inode)
189{
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;
200
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++;
217 }
218 dir_put_page(page);
219 }
220 BUG();
221 return -EINVAL;
222
223got_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);
236out_page:
237 dir_put_page(page);
238out:
239 return err;
240out_unlock:
241 unlock_page(page);
242 goto out_page;
243}
244
245int sysv_delete_entry(struct sysv_dir_entry *de, struct page *page)
246{
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;
253
254 lock_page(page);
255 err = mapping->a_ops->prepare_write(NULL, page, from, to);
Eric Sesterhennd6735bf2006-04-02 13:39:21 +0200256 BUG_ON(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 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;
263}
264
265int sysv_make_empty(struct inode *inode, struct inode *dir)
266{
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;
272
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;
280 }
281
282 base = (char*)page_address(page);
283 memset(base, 0, PAGE_CACHE_SIZE);
284
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,"..");
291
292 err = dir_commit_chunk(page, 0, 2 * SYSV_DIRSIZE);
293fail:
294 kunmap(page);
295 page_cache_release(page);
296 return err;
297}
298
299/*
300 * routine to check that the specified directory is empty (for rmdir)
301 */
302int sysv_empty_dir(struct inode * inode)
303{
304 struct super_block *sb = inode->i_sb;
305 struct page *page = NULL;
306 unsigned long i, npages = dir_pages(inode);
307
308 for (i = 0; i < npages; i++) {
309 char *kaddr;
310 struct sysv_dir_entry * de;
311 page = dir_get_page(inode, i);
312
313 if (IS_ERR(page))
314 continue;
315
316 kaddr = (char *)page_address(page);
317 de = (struct sysv_dir_entry *)kaddr;
318 kaddr += PAGE_CACHE_SIZE-SYSV_DIRSIZE;
319
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;
331 }
332 if (de->name[1] != '.' || de->name[2])
333 goto not_empty;
334 }
335 dir_put_page(page);
336 }
337 return 1;
338
339not_empty:
340 dir_put_page(page);
341 return 0;
342}
343
344/* Releases the page */
345void sysv_set_link(struct sysv_dir_entry *de, struct page *page,
346 struct inode *inode)
347{
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;
352
353 lock_page(page);
354 err = page->mapping->a_ops->prepare_write(NULL, page, from, to);
Eric Sesterhennd6735bf2006-04-02 13:39:21 +0200355 BUG_ON(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 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);
361}
362
363struct sysv_dir_entry * sysv_dotdot (struct inode *dir, struct page **p)
364{
365 struct page *page = dir_get_page(dir, 0);
366 struct sysv_dir_entry *de = NULL;
367
368 if (!IS_ERR(page)) {
369 de = (struct sysv_dir_entry*) page_address(page) + 1;
370 *p = page;
371 }
372 return de;
373}
374
375ino_t sysv_inode_by_name(struct dentry *dentry)
376{
377 struct page *page;
378 struct sysv_dir_entry *de = sysv_find_entry (dentry, &page);
379 ino_t res = 0;
380
381 if (de) {
382 res = fs16_to_cpu(SYSV_SB(dentry->d_sb), de->inode);
383 dir_put_page(page);
384 }
385 return res;
386}