blob: cb4cb571fddfc507f20a26b19b341efd8fdb2465 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/minix/dir.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * minix directory handling functions
Andries Brouwer939b00d2007-02-12 00:52:49 -08007 *
8 * Updated to filesystem version 3 by Daniel Aragones
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11#include "minix.h"
12#include <linux/highmem.h>
13#include <linux/smp_lock.h>
14
15typedef struct minix_dir_entry minix_dirent;
Andries Brouwer939b00d2007-02-12 00:52:49 -080016typedef struct minix3_dir_entry minix3_dirent;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18static int minix_readdir(struct file *, void *, filldir_t);
19
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080020const struct file_operations minix_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 .read = generic_read_dir,
22 .readdir = minix_readdir,
23 .fsync = minix_sync_file,
24};
25
26static inline void dir_put_page(struct page *page)
27{
28 kunmap(page);
29 page_cache_release(page);
30}
31
32/*
33 * Return the offset into page `page_nr' of the last valid
34 * byte in that page, plus one.
35 */
36static unsigned
37minix_last_byte(struct inode *inode, unsigned long page_nr)
38{
39 unsigned last_byte = PAGE_CACHE_SIZE;
40
41 if (page_nr == (inode->i_size >> PAGE_CACHE_SHIFT))
42 last_byte = inode->i_size & (PAGE_CACHE_SIZE - 1);
43 return last_byte;
44}
45
46static inline unsigned long dir_pages(struct inode *inode)
47{
48 return (inode->i_size+PAGE_CACHE_SIZE-1)>>PAGE_CACHE_SHIFT;
49}
50
51static int dir_commit_chunk(struct page *page, unsigned from, unsigned to)
52{
53 struct inode *dir = (struct inode *)page->mapping->host;
54 int err = 0;
55 page->mapping->a_ops->commit_write(NULL, page, from, to);
56 if (IS_DIRSYNC(dir))
57 err = write_one_page(page, 1);
58 else
59 unlock_page(page);
60 return err;
61}
62
63static struct page * dir_get_page(struct inode *dir, unsigned long n)
64{
65 struct address_space *mapping = dir->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -070066 struct page *page = read_mapping_page(mapping, n, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 if (!IS_ERR(page)) {
68 wait_on_page_locked(page);
69 kmap(page);
70 if (!PageUptodate(page))
71 goto fail;
72 }
73 return page;
74
75fail:
76 dir_put_page(page);
77 return ERR_PTR(-EIO);
78}
79
80static inline void *minix_next_entry(void *de, struct minix_sb_info *sbi)
81{
82 return (void*)((char*)de + sbi->s_dirsize);
83}
84
85static int minix_readdir(struct file * filp, void * dirent, filldir_t filldir)
86{
87 unsigned long pos = filp->f_pos;
Josef Sipekdcf258a2006-12-08 02:37:19 -080088 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 struct super_block *sb = inode->i_sb;
90 unsigned offset = pos & ~PAGE_CACHE_MASK;
91 unsigned long n = pos >> PAGE_CACHE_SHIFT;
92 unsigned long npages = dir_pages(inode);
93 struct minix_sb_info *sbi = minix_sb(sb);
94 unsigned chunk_size = sbi->s_dirsize;
Andries Brouwer939b00d2007-02-12 00:52:49 -080095 char *name;
96 __u32 inumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 lock_kernel();
99
100 pos = (pos + chunk_size-1) & ~(chunk_size-1);
101 if (pos >= inode->i_size)
102 goto done;
103
104 for ( ; n < npages; n++, offset = 0) {
105 char *p, *kaddr, *limit;
106 struct page *page = dir_get_page(inode, n);
107
108 if (IS_ERR(page))
109 continue;
110 kaddr = (char *)page_address(page);
111 p = kaddr+offset;
112 limit = kaddr + minix_last_byte(inode, n) - chunk_size;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800113 for ( ; p <= limit; p = minix_next_entry(p, sbi)) {
114 if (sbi->s_version == MINIX_V3) {
115 minix3_dirent *de3 = (minix3_dirent *)p;
116 name = de3->name;
117 inumber = de3->inode;
118 } else {
119 minix_dirent *de = (minix_dirent *)p;
120 name = de->name;
121 inumber = de->inode;
122 }
123 if (inumber) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 int over;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Andries Brouwer939b00d2007-02-12 00:52:49 -0800126 unsigned l = strnlen(name, sbi->s_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 offset = p - kaddr;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800128 over = filldir(dirent, name, l,
129 (n << PAGE_CACHE_SHIFT) | offset,
130 inumber, DT_UNKNOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 if (over) {
132 dir_put_page(page);
133 goto done;
134 }
135 }
136 }
137 dir_put_page(page);
138 }
139
140done:
141 filp->f_pos = (n << PAGE_CACHE_SHIFT) | offset;
142 unlock_kernel();
143 return 0;
144}
145
146static inline int namecompare(int len, int maxlen,
147 const char * name, const char * buffer)
148{
149 if (len < maxlen && buffer[len])
150 return 0;
151 return !memcmp(name, buffer, len);
152}
153
154/*
155 * minix_find_entry()
156 *
157 * finds an entry in the specified directory with the wanted name. It
158 * returns the cache buffer in which the entry was found, and the entry
159 * itself (as a parameter - res_dir). It does NOT read the inode of the
160 * entry - you'll have to do that yourself if you want to.
161 */
162minix_dirent *minix_find_entry(struct dentry *dentry, struct page **res_page)
163{
164 const char * name = dentry->d_name.name;
165 int namelen = dentry->d_name.len;
166 struct inode * dir = dentry->d_parent->d_inode;
167 struct super_block * sb = dir->i_sb;
168 struct minix_sb_info * sbi = minix_sb(sb);
169 unsigned long n;
170 unsigned long npages = dir_pages(dir);
171 struct page *page = NULL;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800172 char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Andries Brouwer939b00d2007-02-12 00:52:49 -0800174 char *namx;
175 __u32 inumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 *res_page = NULL;
177
178 for (n = 0; n < npages; n++) {
Andries Brouwer939b00d2007-02-12 00:52:49 -0800179 char *kaddr, *limit;
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 page = dir_get_page(dir, n);
182 if (IS_ERR(page))
183 continue;
184
185 kaddr = (char*)page_address(page);
Andries Brouwer939b00d2007-02-12 00:52:49 -0800186 limit = kaddr + minix_last_byte(dir, n) - sbi->s_dirsize;
187 for (p = kaddr; p <= limit; p = minix_next_entry(p, sbi)) {
188 if (sbi->s_version == MINIX_V3) {
189 minix3_dirent *de3 = (minix3_dirent *)p;
190 namx = de3->name;
191 inumber = de3->inode;
192 } else {
193 minix_dirent *de = (minix_dirent *)p;
194 namx = de->name;
195 inumber = de->inode;
196 }
197 if (!inumber)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 continue;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800199 if (namecompare(namelen, sbi->s_namelen, name, namx))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 goto found;
201 }
202 dir_put_page(page);
203 }
204 return NULL;
205
206found:
207 *res_page = page;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800208 return (minix_dirent *)p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
211int minix_add_link(struct dentry *dentry, struct inode *inode)
212{
213 struct inode *dir = dentry->d_parent->d_inode;
214 const char * name = dentry->d_name.name;
215 int namelen = dentry->d_name.len;
216 struct super_block * sb = dir->i_sb;
217 struct minix_sb_info * sbi = minix_sb(sb);
218 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 unsigned long npages = dir_pages(dir);
220 unsigned long n;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800221 char *kaddr, *p;
222 minix_dirent *de;
223 minix3_dirent *de3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 unsigned from, to;
225 int err;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800226 char *namx = NULL;
227 __u32 inumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 /*
230 * We take care of directory expansion in the same loop
231 * This code plays outside i_size, so it locks the page
232 * to protect that region.
233 */
234 for (n = 0; n <= npages; n++) {
Andries Brouwer939b00d2007-02-12 00:52:49 -0800235 char *limit, *dir_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 page = dir_get_page(dir, n);
238 err = PTR_ERR(page);
239 if (IS_ERR(page))
240 goto out;
241 lock_page(page);
242 kaddr = (char*)page_address(page);
243 dir_end = kaddr + minix_last_byte(dir, n);
Andries Brouwer939b00d2007-02-12 00:52:49 -0800244 limit = kaddr + PAGE_CACHE_SIZE - sbi->s_dirsize;
245 for (p = kaddr; p <= limit; p = minix_next_entry(p, sbi)) {
246 de = (minix_dirent *)p;
247 de3 = (minix3_dirent *)p;
248 if (sbi->s_version == MINIX_V3) {
249 namx = de3->name;
250 inumber = de3->inode;
251 } else {
252 namx = de->name;
253 inumber = de->inode;
254 }
255 if (p == dir_end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 /* We hit i_size */
Andries Brouwer939b00d2007-02-12 00:52:49 -0800257 if (sbi->s_version == MINIX_V3)
258 de3->inode = 0;
259 else
260 de->inode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 goto got_it;
262 }
Andries Brouwer939b00d2007-02-12 00:52:49 -0800263 if (!inumber)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 goto got_it;
265 err = -EEXIST;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800266 if (namecompare(namelen, sbi->s_namelen, name, namx))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
269 unlock_page(page);
270 dir_put_page(page);
271 }
272 BUG();
273 return -EINVAL;
274
275got_it:
Andries Brouwer939b00d2007-02-12 00:52:49 -0800276 from = p - (char*)page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 to = from + sbi->s_dirsize;
278 err = page->mapping->a_ops->prepare_write(NULL, page, from, to);
279 if (err)
280 goto out_unlock;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800281 memcpy (namx, name, namelen);
282 if (sbi->s_version == MINIX_V3) {
283 memset (namx + namelen, 0, sbi->s_dirsize - namelen - 4);
284 de3->inode = inode->i_ino;
285 } else {
286 memset (namx + namelen, 0, sbi->s_dirsize - namelen - 2);
287 de->inode = inode->i_ino;
288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 err = dir_commit_chunk(page, from, to);
290 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
291 mark_inode_dirty(dir);
292out_put:
293 dir_put_page(page);
294out:
295 return err;
296out_unlock:
297 unlock_page(page);
298 goto out_put;
299}
300
301int minix_delete_entry(struct minix_dir_entry *de, struct page *page)
302{
303 struct address_space *mapping = page->mapping;
304 struct inode *inode = (struct inode*)mapping->host;
305 char *kaddr = page_address(page);
306 unsigned from = (char*)de - kaddr;
307 unsigned to = from + minix_sb(inode->i_sb)->s_dirsize;
308 int err;
309
310 lock_page(page);
311 err = mapping->a_ops->prepare_write(NULL, page, from, to);
312 if (err == 0) {
313 de->inode = 0;
314 err = dir_commit_chunk(page, from, to);
315 } else {
316 unlock_page(page);
317 }
318 dir_put_page(page);
319 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
320 mark_inode_dirty(inode);
321 return err;
322}
323
324int minix_make_empty(struct inode *inode, struct inode *dir)
325{
326 struct address_space *mapping = inode->i_mapping;
327 struct page *page = grab_cache_page(mapping, 0);
Andries Brouwer939b00d2007-02-12 00:52:49 -0800328 struct minix_sb_info *sbi = minix_sb(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 char *kaddr;
330 int err;
331
332 if (!page)
333 return -ENOMEM;
334 err = mapping->a_ops->prepare_write(NULL, page, 0, 2 * sbi->s_dirsize);
335 if (err) {
336 unlock_page(page);
337 goto fail;
338 }
339
340 kaddr = kmap_atomic(page, KM_USER0);
341 memset(kaddr, 0, PAGE_CACHE_SIZE);
342
Andries Brouwer939b00d2007-02-12 00:52:49 -0800343 if (sbi->s_version == MINIX_V3) {
344 minix3_dirent *de3 = (minix3_dirent *)kaddr;
345
346 de3->inode = inode->i_ino;
347 strcpy(de3->name, ".");
348 de3 = minix_next_entry(de3, sbi);
349 de3->inode = dir->i_ino;
350 strcpy(de3->name, "..");
351 } else {
352 minix_dirent *de = (minix_dirent *)kaddr;
353
354 de->inode = inode->i_ino;
355 strcpy(de->name, ".");
356 de = minix_next_entry(de, sbi);
357 de->inode = dir->i_ino;
358 strcpy(de->name, "..");
359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 kunmap_atomic(kaddr, KM_USER0);
361
362 err = dir_commit_chunk(page, 0, 2 * sbi->s_dirsize);
363fail:
364 page_cache_release(page);
365 return err;
366}
367
368/*
369 * routine to check that the specified directory is empty (for rmdir)
370 */
371int minix_empty_dir(struct inode * inode)
372{
373 struct page *page = NULL;
374 unsigned long i, npages = dir_pages(inode);
375 struct minix_sb_info *sbi = minix_sb(inode->i_sb);
Andries Brouwer939b00d2007-02-12 00:52:49 -0800376 char *name;
377 __u32 inumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 for (i = 0; i < npages; i++) {
Andries Brouwer939b00d2007-02-12 00:52:49 -0800380 char *p, *kaddr, *limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Andries Brouwer939b00d2007-02-12 00:52:49 -0800382 page = dir_get_page(inode, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (IS_ERR(page))
384 continue;
385
386 kaddr = (char *)page_address(page);
Andries Brouwer939b00d2007-02-12 00:52:49 -0800387 limit = kaddr + minix_last_byte(inode, i) - sbi->s_dirsize;
388 for (p = kaddr; p <= limit; p = minix_next_entry(p, sbi)) {
389 if (sbi->s_version == MINIX_V3) {
390 minix3_dirent *de3 = (minix3_dirent *)p;
391 name = de3->name;
392 inumber = de3->inode;
393 } else {
394 minix_dirent *de = (minix_dirent *)p;
395 name = de->name;
396 inumber = de->inode;
397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Andries Brouwer939b00d2007-02-12 00:52:49 -0800399 if (inumber != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 /* check for . and .. */
Andries Brouwer939b00d2007-02-12 00:52:49 -0800401 if (name[0] != '.')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 goto not_empty;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800403 if (!name[1]) {
404 if (inumber != inode->i_ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 goto not_empty;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800406 } else if (name[1] != '.')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 goto not_empty;
Andries Brouwer939b00d2007-02-12 00:52:49 -0800408 else if (name[2])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 goto not_empty;
410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
412 dir_put_page(page);
413 }
414 return 1;
415
416not_empty:
417 dir_put_page(page);
418 return 0;
419}
420
421/* Releases the page */
422void minix_set_link(struct minix_dir_entry *de, struct page *page,
423 struct inode *inode)
424{
425 struct inode *dir = (struct inode*)page->mapping->host;
426 struct minix_sb_info *sbi = minix_sb(dir->i_sb);
427 unsigned from = (char *)de-(char*)page_address(page);
428 unsigned to = from + sbi->s_dirsize;
429 int err;
430
431 lock_page(page);
432 err = page->mapping->a_ops->prepare_write(NULL, page, from, to);
433 if (err == 0) {
434 de->inode = inode->i_ino;
435 err = dir_commit_chunk(page, from, to);
436 } else {
437 unlock_page(page);
438 }
439 dir_put_page(page);
440 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
441 mark_inode_dirty(dir);
442}
443
444struct minix_dir_entry * minix_dotdot (struct inode *dir, struct page **p)
445{
446 struct page *page = dir_get_page(dir, 0);
447 struct minix_sb_info *sbi = minix_sb(dir->i_sb);
448 struct minix_dir_entry *de = NULL;
449
450 if (!IS_ERR(page)) {
451 de = minix_next_entry(page_address(page), sbi);
452 *p = page;
453 }
454 return de;
455}
456
457ino_t minix_inode_by_name(struct dentry *dentry)
458{
459 struct page *page;
460 struct minix_dir_entry *de = minix_find_entry(dentry, &page);
461 ino_t res = 0;
462
463 if (de) {
464 res = de->inode;
465 dir_put_page(page);
466 }
467 return res;
468}