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);
68 wait_on_page_locked(page);
70 if (!PageUptodate(page))
80 static inline void *minix_next_entry(void *de, struct minix_sb_info *sbi)
82 return (void*)((char*)de + sbi->s_dirsize);
85 static int minix_readdir(struct file * filp, void * dirent, filldir_t filldir)
87 unsigned long pos = filp->f_pos;
88 struct inode *inode = filp->f_path.dentry->d_inode;
89 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;
100 pos = (pos + chunk_size-1) & ~(chunk_size-1);
101 if (pos >= inode->i_size)
104 for ( ; n < npages; n++, offset = 0) {
105 char *p, *kaddr, *limit;
106 struct page *page = dir_get_page(inode, n);
110 kaddr = (char *)page_address(page);
112 limit = kaddr + minix_last_byte(inode, n) - chunk_size;
113 for ( ; p <= limit; p = minix_next_entry(p, sbi)) {
114 if (sbi->s_version == MINIX_V3) {
115 minix3_dirent *de3 = (minix3_dirent *)p;
117 inumber = de3->inode;
119 minix_dirent *de = (minix_dirent *)p;
126 unsigned l = strnlen(name, sbi->s_namelen);
128 over = filldir(dirent, name, l,
129 (n << PAGE_CACHE_SHIFT) | offset,
130 inumber, DT_UNKNOWN);
141 filp->f_pos = (n << PAGE_CACHE_SHIFT) | offset;
146 static inline int namecompare(int len, int maxlen,
147 const char * name, const char * buffer)
149 if (len < maxlen && buffer[len])
151 return !memcmp(name, buffer, len);
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.
162 minix_dirent *minix_find_entry(struct dentry *dentry, struct page **res_page)
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);
170 unsigned long npages = dir_pages(dir);
171 struct page *page = NULL;
178 for (n = 0; n < npages; n++) {
181 page = dir_get_page(dir, n);
185 kaddr = (char*)page_address(page);
186 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;
191 inumber = de3->inode;
193 minix_dirent *de = (minix_dirent *)p;
199 if (namecompare(namelen, sbi->s_namelen, name, namx))
208 return (minix_dirent *)p;
211 int minix_add_link(struct dentry *dentry, struct inode *inode)
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;
219 unsigned long npages = dir_pages(dir);
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.
234 for (n = 0; n <= npages; n++) {
235 char *limit, *dir_end;
237 page = dir_get_page(dir, n);
242 kaddr = (char*)page_address(page);
243 dir_end = kaddr + minix_last_byte(dir, n);
244 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) {
250 inumber = de3->inode;
257 if (sbi->s_version == MINIX_V3)
266 if (namecompare(namelen, sbi->s_namelen, name, namx))
276 from = p - (char*)page_address(page);
277 to = from + sbi->s_dirsize;
278 err = page->mapping->a_ops->prepare_write(NULL, page, from, to);
281 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;
286 memset (namx + namelen, 0, sbi->s_dirsize - namelen - 2);
287 de->inode = inode->i_ino;
289 err = dir_commit_chunk(page, from, to);
290 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
291 mark_inode_dirty(dir);
301 int minix_delete_entry(struct minix_dir_entry *de, struct page *page)
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;
311 err = mapping->a_ops->prepare_write(NULL, page, from, to);
314 err = dir_commit_chunk(page, from, to);
319 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
320 mark_inode_dirty(inode);
324 int minix_make_empty(struct inode *inode, struct inode *dir)
326 struct address_space *mapping = inode->i_mapping;
327 struct page *page = grab_cache_page(mapping, 0);
328 struct minix_sb_info *sbi = minix_sb(inode->i_sb);
334 err = mapping->a_ops->prepare_write(NULL, page, 0, 2 * sbi->s_dirsize);
340 kaddr = kmap_atomic(page, KM_USER0);
341 memset(kaddr, 0, PAGE_CACHE_SIZE);
343 if (sbi->s_version == MINIX_V3) {
344 minix3_dirent *de3 = (minix3_dirent *)kaddr;
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, "..");
352 minix_dirent *de = (minix_dirent *)kaddr;
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, "..");
360 kunmap_atomic(kaddr, KM_USER0);
362 err = dir_commit_chunk(page, 0, 2 * sbi->s_dirsize);
364 page_cache_release(page);
369 * routine to check that the specified directory is empty (for rmdir)
371 int minix_empty_dir(struct inode * inode)
373 struct page *page = NULL;
374 unsigned long i, npages = dir_pages(inode);
375 struct minix_sb_info *sbi = minix_sb(inode->i_sb);
379 for (i = 0; i < npages; i++) {
380 char *p, *kaddr, *limit;
382 page = dir_get_page(inode, i);
386 kaddr = (char *)page_address(page);
387 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;
392 inumber = de3->inode;
394 minix_dirent *de = (minix_dirent *)p;
400 /* check for . and .. */
404 if (inumber != inode->i_ino)
406 } else if (name[1] != '.')
421 /* Releases the page */
422 void minix_set_link(struct minix_dir_entry *de, struct page *page,
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;
432 err = page->mapping->a_ops->prepare_write(NULL, page, from, to);
434 de->inode = inode->i_ino;
435 err = dir_commit_chunk(page, from, to);
440 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
441 mark_inode_dirty(dir);
444 struct minix_dir_entry * minix_dotdot (struct inode *dir, struct page **p)
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;
451 de = minix_next_entry(page_address(page), sbi);
457 ino_t minix_inode_by_name(struct dentry *dentry)
460 struct minix_dir_entry *de = minix_find_entry(dentry, &page);