4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * minix directory handling functions
8 * Updated to filesystem version 3 by Daniel Aragones
12 #include <linux/highmem.h>
13 #include <linux/smp_lock.h>
15 typedef struct minix_dir_entry minix_dirent;
16 typedef struct minix3_dir_entry minix3_dirent;
18 static int minix_readdir(struct file *, void *, filldir_t);
20 const struct file_operations minix_dir_operations = {
21 .read = generic_read_dir,
22 .readdir = minix_readdir,
23 .fsync = minix_sync_file,
26 static inline void dir_put_page(struct page *page)
29 page_cache_release(page);
33 * Return the offset into page `page_nr' of the last valid
34 * byte in that page, plus one.
37 minix_last_byte(struct inode *inode, unsigned long page_nr)
39 unsigned last_byte = PAGE_CACHE_SIZE;
41 if (page_nr == (inode->i_size >> PAGE_CACHE_SHIFT))
42 last_byte = inode->i_size & (PAGE_CACHE_SIZE - 1);
46 static inline unsigned long dir_pages(struct inode *inode)
48 return (inode->i_size+PAGE_CACHE_SIZE-1)>>PAGE_CACHE_SHIFT;
51 static int dir_commit_chunk(struct page *page, unsigned from, unsigned to)
53 struct inode *dir = (struct inode *)page->mapping->host;
55 page->mapping->a_ops->commit_write(NULL, page, from, to);
57 err = write_one_page(page, 1);
63 static struct page * dir_get_page(struct inode *dir, unsigned long n)
65 struct address_space *mapping = dir->i_mapping;
66 struct page *page = read_mapping_page(mapping, n, NULL);
69 if (!PageUptodate(page))
79 static inline void *minix_next_entry(void *de, struct minix_sb_info *sbi)
81 return (void*)((char*)de + sbi->s_dirsize);
84 static int minix_readdir(struct file * filp, void * dirent, filldir_t filldir)
86 unsigned long pos = filp->f_pos;
87 struct inode *inode = filp->f_path.dentry->d_inode;
88 struct super_block *sb = inode->i_sb;
89 unsigned offset = pos & ~PAGE_CACHE_MASK;
90 unsigned long n = pos >> PAGE_CACHE_SHIFT;
91 unsigned long npages = dir_pages(inode);
92 struct minix_sb_info *sbi = minix_sb(sb);
93 unsigned chunk_size = sbi->s_dirsize;
99 pos = (pos + chunk_size-1) & ~(chunk_size-1);
100 if (pos >= inode->i_size)
103 for ( ; n < npages; n++, offset = 0) {
104 char *p, *kaddr, *limit;
105 struct page *page = dir_get_page(inode, n);
109 kaddr = (char *)page_address(page);
111 limit = kaddr + minix_last_byte(inode, n) - chunk_size;
112 for ( ; p <= limit; p = minix_next_entry(p, sbi)) {
113 if (sbi->s_version == MINIX_V3) {
114 minix3_dirent *de3 = (minix3_dirent *)p;
116 inumber = de3->inode;
118 minix_dirent *de = (minix_dirent *)p;
125 unsigned l = strnlen(name, sbi->s_namelen);
127 over = filldir(dirent, name, l,
128 (n << PAGE_CACHE_SHIFT) | offset,
129 inumber, DT_UNKNOWN);
140 filp->f_pos = (n << PAGE_CACHE_SHIFT) | offset;
145 static inline int namecompare(int len, int maxlen,
146 const char * name, const char * buffer)
148 if (len < maxlen && buffer[len])
150 return !memcmp(name, buffer, len);
156 * finds an entry in the specified directory with the wanted name. It
157 * returns the cache buffer in which the entry was found, and the entry
158 * itself (as a parameter - res_dir). It does NOT read the inode of the
159 * entry - you'll have to do that yourself if you want to.
161 minix_dirent *minix_find_entry(struct dentry *dentry, struct page **res_page)
163 const char * name = dentry->d_name.name;
164 int namelen = dentry->d_name.len;
165 struct inode * dir = dentry->d_parent->d_inode;
166 struct super_block * sb = dir->i_sb;
167 struct minix_sb_info * sbi = minix_sb(sb);
169 unsigned long npages = dir_pages(dir);
170 struct page *page = NULL;
177 for (n = 0; n < npages; n++) {
180 page = dir_get_page(dir, n);
184 kaddr = (char*)page_address(page);
185 limit = kaddr + minix_last_byte(dir, n) - sbi->s_dirsize;
186 for (p = kaddr; p <= limit; p = minix_next_entry(p, sbi)) {
187 if (sbi->s_version == MINIX_V3) {
188 minix3_dirent *de3 = (minix3_dirent *)p;
190 inumber = de3->inode;
192 minix_dirent *de = (minix_dirent *)p;
198 if (namecompare(namelen, sbi->s_namelen, name, namx))
207 return (minix_dirent *)p;
210 int minix_add_link(struct dentry *dentry, struct inode *inode)
212 struct inode *dir = dentry->d_parent->d_inode;
213 const char * name = dentry->d_name.name;
214 int namelen = dentry->d_name.len;
215 struct super_block * sb = dir->i_sb;
216 struct minix_sb_info * sbi = minix_sb(sb);
217 struct page *page = NULL;
218 unsigned long npages = dir_pages(dir);
229 * We take care of directory expansion in the same loop
230 * This code plays outside i_size, so it locks the page
231 * to protect that region.
233 for (n = 0; n <= npages; n++) {
234 char *limit, *dir_end;
236 page = dir_get_page(dir, n);
241 kaddr = (char*)page_address(page);
242 dir_end = kaddr + minix_last_byte(dir, n);
243 limit = kaddr + PAGE_CACHE_SIZE - sbi->s_dirsize;
244 for (p = kaddr; p <= limit; p = minix_next_entry(p, sbi)) {
245 de = (minix_dirent *)p;
246 de3 = (minix3_dirent *)p;
247 if (sbi->s_version == MINIX_V3) {
249 inumber = de3->inode;
256 if (sbi->s_version == MINIX_V3)
265 if (namecompare(namelen, sbi->s_namelen, name, namx))
275 from = p - (char*)page_address(page);
276 to = from + sbi->s_dirsize;
277 err = page->mapping->a_ops->prepare_write(NULL, page, from, to);
280 memcpy (namx, name, namelen);
281 if (sbi->s_version == MINIX_V3) {
282 memset (namx + namelen, 0, sbi->s_dirsize - namelen - 4);
283 de3->inode = inode->i_ino;
285 memset (namx + namelen, 0, sbi->s_dirsize - namelen - 2);
286 de->inode = inode->i_ino;
288 err = dir_commit_chunk(page, from, to);
289 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
290 mark_inode_dirty(dir);
300 int minix_delete_entry(struct minix_dir_entry *de, struct page *page)
302 struct address_space *mapping = page->mapping;
303 struct inode *inode = (struct inode*)mapping->host;
304 char *kaddr = page_address(page);
305 unsigned from = (char*)de - kaddr;
306 unsigned to = from + minix_sb(inode->i_sb)->s_dirsize;
310 err = mapping->a_ops->prepare_write(NULL, page, from, to);
313 err = dir_commit_chunk(page, from, to);
318 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
319 mark_inode_dirty(inode);
323 int minix_make_empty(struct inode *inode, struct inode *dir)
325 struct address_space *mapping = inode->i_mapping;
326 struct page *page = grab_cache_page(mapping, 0);
327 struct minix_sb_info *sbi = minix_sb(inode->i_sb);
333 err = mapping->a_ops->prepare_write(NULL, page, 0, 2 * sbi->s_dirsize);
339 kaddr = kmap_atomic(page, KM_USER0);
340 memset(kaddr, 0, PAGE_CACHE_SIZE);
342 if (sbi->s_version == MINIX_V3) {
343 minix3_dirent *de3 = (minix3_dirent *)kaddr;
345 de3->inode = inode->i_ino;
346 strcpy(de3->name, ".");
347 de3 = minix_next_entry(de3, sbi);
348 de3->inode = dir->i_ino;
349 strcpy(de3->name, "..");
351 minix_dirent *de = (minix_dirent *)kaddr;
353 de->inode = inode->i_ino;
354 strcpy(de->name, ".");
355 de = minix_next_entry(de, sbi);
356 de->inode = dir->i_ino;
357 strcpy(de->name, "..");
359 kunmap_atomic(kaddr, KM_USER0);
361 err = dir_commit_chunk(page, 0, 2 * sbi->s_dirsize);
363 page_cache_release(page);
368 * routine to check that the specified directory is empty (for rmdir)
370 int minix_empty_dir(struct inode * inode)
372 struct page *page = NULL;
373 unsigned long i, npages = dir_pages(inode);
374 struct minix_sb_info *sbi = minix_sb(inode->i_sb);
378 for (i = 0; i < npages; i++) {
379 char *p, *kaddr, *limit;
381 page = dir_get_page(inode, i);
385 kaddr = (char *)page_address(page);
386 limit = kaddr + minix_last_byte(inode, i) - sbi->s_dirsize;
387 for (p = kaddr; p <= limit; p = minix_next_entry(p, sbi)) {
388 if (sbi->s_version == MINIX_V3) {
389 minix3_dirent *de3 = (minix3_dirent *)p;
391 inumber = de3->inode;
393 minix_dirent *de = (minix_dirent *)p;
399 /* check for . and .. */
403 if (inumber != inode->i_ino)
405 } else if (name[1] != '.')
420 /* Releases the page */
421 void minix_set_link(struct minix_dir_entry *de, struct page *page,
424 struct inode *dir = (struct inode*)page->mapping->host;
425 struct minix_sb_info *sbi = minix_sb(dir->i_sb);
426 unsigned from = (char *)de-(char*)page_address(page);
427 unsigned to = from + sbi->s_dirsize;
431 err = page->mapping->a_ops->prepare_write(NULL, page, from, to);
433 de->inode = inode->i_ino;
434 err = dir_commit_chunk(page, from, to);
439 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
440 mark_inode_dirty(dir);
443 struct minix_dir_entry * minix_dotdot (struct inode *dir, struct page **p)
445 struct page *page = dir_get_page(dir, 0);
446 struct minix_sb_info *sbi = minix_sb(dir->i_sb);
447 struct minix_dir_entry *de = NULL;
450 de = minix_next_entry(page_address(page), sbi);
456 ino_t minix_inode_by_name(struct dentry *dentry)
459 struct minix_dir_entry *de = minix_find_entry(dentry, &page);