diff options
author | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
commit | 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch) | |
tree | 0bba044c4ce775e45a88a51686b5d9f90697ea9d /fs/sysv/dir.c |
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history,
even though we have it. We can create a separate "historical" git
archive of that later if we want to, and in the meantime it's about
3.2GB when imported into git - space that would just make the early
git days unnecessarily complicated, when we don't have a lot of good
infrastructure for it.
Let it rip!
Diffstat (limited to 'fs/sysv/dir.c')
-rw-r--r-- | fs/sysv/dir.c | 388 |
1 files changed, 388 insertions, 0 deletions
diff --git a/fs/sysv/dir.c b/fs/sysv/dir.c new file mode 100644 index 000000000000..69a085abad6f --- /dev/null +++ b/fs/sysv/dir.c | |||
@@ -0,0 +1,388 @@ | |||
1 | /* | ||
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 | |||
21 | static int sysv_readdir(struct file *, void *, filldir_t); | ||
22 | |||
23 | struct file_operations sysv_dir_operations = { | ||
24 | .read = generic_read_dir, | ||
25 | .readdir = sysv_readdir, | ||
26 | .fsync = sysv_sync_file, | ||
27 | }; | ||
28 | |||
29 | static inline void dir_put_page(struct page *page) | ||
30 | { | ||
31 | kunmap(page); | ||
32 | page_cache_release(page); | ||
33 | } | ||
34 | |||
35 | static inline unsigned long dir_pages(struct inode *inode) | ||
36 | { | ||
37 | return (inode->i_size+PAGE_CACHE_SIZE-1)>>PAGE_CACHE_SHIFT; | ||
38 | } | ||
39 | |||
40 | static 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 | |||
53 | static 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 | |||
66 | fail: | ||
67 | dir_put_page(page); | ||
68 | return ERR_PTR(-EIO); | ||
69 | } | ||
70 | |||
71 | static 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), | ||
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; | ||
112 | } | ||
113 | } | ||
114 | dir_put_page(page); | ||
115 | } | ||
116 | |||
117 | done: | ||
118 | filp->f_pos = (n << PAGE_CACHE_SHIFT) | offset; | ||
119 | 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 | */ | ||
126 | static 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 | */ | ||
142 | struct 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 | |||
182 | found: | ||
183 | SYSV_I(dir)->i_dir_start_lookup = n; | ||
184 | *res_page = page; | ||
185 | return de; | ||
186 | } | ||
187 | |||
188 | int 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 | |||
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; | ||
243 | } | ||
244 | |||
245 | int 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); | ||
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; | ||
264 | } | ||
265 | |||
266 | int sysv_make_empty(struct inode *inode, struct inode *dir) | ||
267 | { | ||
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; | ||
273 | |||
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; | ||
281 | } | ||
282 | |||
283 | base = (char*)page_address(page); | ||
284 | memset(base, 0, PAGE_CACHE_SIZE); | ||
285 | |||
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,".."); | ||
292 | |||
293 | err = dir_commit_chunk(page, 0, 2 * SYSV_DIRSIZE); | ||
294 | fail: | ||
295 | kunmap(page); | ||
296 | page_cache_release(page); | ||
297 | return err; | ||
298 | } | ||
299 | |||
300 | /* | ||
301 | * routine to check that the specified directory is empty (for rmdir) | ||
302 | */ | ||
303 | int sysv_empty_dir(struct inode * inode) | ||
304 | { | ||
305 | struct super_block *sb = inode->i_sb; | ||
306 | struct page *page = NULL; | ||
307 | unsigned long i, npages = dir_pages(inode); | ||
308 | |||
309 | for (i = 0; i < npages; i++) { | ||
310 | char *kaddr; | ||
311 | struct sysv_dir_entry * de; | ||
312 | page = dir_get_page(inode, i); | ||
313 | |||
314 | if (IS_ERR(page)) | ||
315 | continue; | ||
316 | |||
317 | kaddr = (char *)page_address(page); | ||
318 | de = (struct sysv_dir_entry *)kaddr; | ||
319 | kaddr += PAGE_CACHE_SIZE-SYSV_DIRSIZE; | ||
320 | |||
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; | ||
332 | } | ||
333 | if (de->name[1] != '.' || de->name[2]) | ||
334 | goto not_empty; | ||
335 | } | ||
336 | dir_put_page(page); | ||
337 | } | ||
338 | return 1; | ||
339 | |||
340 | not_empty: | ||
341 | dir_put_page(page); | ||
342 | return 0; | ||
343 | } | ||
344 | |||
345 | /* Releases the page */ | ||
346 | void sysv_set_link(struct sysv_dir_entry *de, struct page *page, | ||
347 | struct inode *inode) | ||
348 | { | ||
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; | ||
353 | |||
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); | ||
363 | } | ||
364 | |||
365 | struct sysv_dir_entry * sysv_dotdot (struct inode *dir, struct page **p) | ||
366 | { | ||
367 | struct page *page = dir_get_page(dir, 0); | ||
368 | struct sysv_dir_entry *de = NULL; | ||
369 | |||
370 | if (!IS_ERR(page)) { | ||
371 | de = (struct sysv_dir_entry*) page_address(page) + 1; | ||
372 | *p = page; | ||
373 | } | ||
374 | return de; | ||
375 | } | ||
376 | |||
377 | ino_t sysv_inode_by_name(struct dentry *dentry) | ||
378 | { | ||
379 | struct page *page; | ||
380 | struct sysv_dir_entry *de = sysv_find_entry (dentry, &page); | ||
381 | ino_t res = 0; | ||
382 | |||
383 | if (de) { | ||
384 | res = fs16_to_cpu(SYSV_SB(dentry->d_sb), de->inode); | ||
385 | dir_put_page(page); | ||
386 | } | ||
387 | return res; | ||
388 | } | ||