4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * minix directory handling functions
10 #include <linux/highmem.h>
11 #include <linux/smp_lock.h>
13 typedef struct minix_dir_entry minix_dirent;
15 static int minix_readdir(struct file *, void *, filldir_t);
17 const struct file_operations minix_dir_operations = {
18 .read = generic_read_dir,
19 .readdir = minix_readdir,
20 .fsync = minix_sync_file,
23 static inline void dir_put_page(struct page *page)
26 page_cache_release(page);
30 * Return the offset into page `page_nr' of the last valid
31 * byte in that page, plus one.
34 minix_last_byte(struct inode *inode, unsigned long page_nr)
36 unsigned last_byte = PAGE_CACHE_SIZE;
38 if (page_nr == (inode->i_size >> PAGE_CACHE_SHIFT))
39 last_byte = inode->i_size & (PAGE_CACHE_SIZE - 1);
43 static inline unsigned long dir_pages(struct inode *inode)
45 return (inode->i_size+PAGE_CACHE_SIZE-1)>>PAGE_CACHE_SHIFT;
48 static int dir_commit_chunk(struct page *page, unsigned from, unsigned to)
50 struct inode *dir = (struct inode *)page->mapping->host;
52 page->mapping->a_ops->commit_write(NULL, page, from, to);
54 err = write_one_page(page, 1);
60 static struct page * dir_get_page(struct inode *dir, unsigned long n)
62 struct address_space *mapping = dir->i_mapping;
63 struct page *page = read_mapping_page(mapping, n, NULL);
65 wait_on_page_locked(page);
67 if (!PageUptodate(page))
77 static inline void *minix_next_entry(void *de, struct minix_sb_info *sbi)
79 return (void*)((char*)de + sbi->s_dirsize);
82 static int minix_readdir(struct file * filp, void * dirent, filldir_t filldir)
84 unsigned long pos = filp->f_pos;
85 struct inode *inode = filp->f_path.dentry->d_inode;
86 struct super_block *sb = inode->i_sb;
87 unsigned offset = pos & ~PAGE_CACHE_MASK;
88 unsigned long n = pos >> PAGE_CACHE_SHIFT;
89 unsigned long npages = dir_pages(inode);
90 struct minix_sb_info *sbi = minix_sb(sb);
91 unsigned chunk_size = sbi->s_dirsize;
95 pos = (pos + chunk_size-1) & ~(chunk_size-1);
96 if (pos >= inode->i_size)
99 for ( ; n < npages; n++, offset = 0) {
100 char *p, *kaddr, *limit;
101 struct page *page = dir_get_page(inode, n);
105 kaddr = (char *)page_address(page);
107 limit = kaddr + minix_last_byte(inode, n) - chunk_size;
108 for ( ; p <= limit ; p = minix_next_entry(p, sbi)) {
109 minix_dirent *de = (minix_dirent *)p;
112 unsigned l = strnlen(de->name,sbi->s_namelen);
115 over = filldir(dirent, de->name, l,
116 (n<<PAGE_CACHE_SHIFT) | offset,
117 de->inode, DT_UNKNOWN);
128 filp->f_pos = (n << PAGE_CACHE_SHIFT) | offset;
133 static inline int namecompare(int len, int maxlen,
134 const char * name, const char * buffer)
136 if (len < maxlen && buffer[len])
138 return !memcmp(name, buffer, len);
144 * finds an entry in the specified directory with the wanted name. It
145 * returns the cache buffer in which the entry was found, and the entry
146 * itself (as a parameter - res_dir). It does NOT read the inode of the
147 * entry - you'll have to do that yourself if you want to.
149 minix_dirent *minix_find_entry(struct dentry *dentry, struct page **res_page)
151 const char * name = dentry->d_name.name;
152 int namelen = dentry->d_name.len;
153 struct inode * dir = dentry->d_parent->d_inode;
154 struct super_block * sb = dir->i_sb;
155 struct minix_sb_info * sbi = minix_sb(sb);
157 unsigned long npages = dir_pages(dir);
158 struct page *page = NULL;
159 struct minix_dir_entry *de;
163 for (n = 0; n < npages; n++) {
165 page = dir_get_page(dir, n);
169 kaddr = (char*)page_address(page);
170 de = (struct minix_dir_entry *) kaddr;
171 kaddr += minix_last_byte(dir, n) - sbi->s_dirsize;
172 for ( ; (char *) de <= kaddr ; de = minix_next_entry(de,sbi)) {
175 if (namecompare(namelen,sbi->s_namelen,name,de->name))
187 int minix_add_link(struct dentry *dentry, struct inode *inode)
189 struct inode *dir = dentry->d_parent->d_inode;
190 const char * name = dentry->d_name.name;
191 int namelen = dentry->d_name.len;
192 struct super_block * sb = dir->i_sb;
193 struct minix_sb_info * sbi = minix_sb(sb);
194 struct page *page = NULL;
195 struct minix_dir_entry * de;
196 unsigned long npages = dir_pages(dir);
203 * We take care of directory expansion in the same loop
204 * This code plays outside i_size, so it locks the page
205 * to protect that region.
207 for (n = 0; n <= npages; n++) {
210 page = dir_get_page(dir, n);
215 kaddr = (char*)page_address(page);
216 dir_end = kaddr + minix_last_byte(dir, n);
217 de = (minix_dirent *)kaddr;
218 kaddr += PAGE_CACHE_SIZE - sbi->s_dirsize;
219 while ((char *)de <= kaddr) {
220 if ((char *)de == dir_end) {
228 if (namecompare(namelen,sbi->s_namelen,name,de->name))
230 de = minix_next_entry(de, sbi);
239 from = (char*)de - (char*)page_address(page);
240 to = from + sbi->s_dirsize;
241 err = page->mapping->a_ops->prepare_write(NULL, page, from, to);
244 memcpy (de->name, name, namelen);
245 memset (de->name + namelen, 0, sbi->s_dirsize - namelen - 2);
246 de->inode = inode->i_ino;
247 err = dir_commit_chunk(page, from, to);
248 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
249 mark_inode_dirty(dir);
259 int minix_delete_entry(struct minix_dir_entry *de, struct page *page)
261 struct address_space *mapping = page->mapping;
262 struct inode *inode = (struct inode*)mapping->host;
263 char *kaddr = page_address(page);
264 unsigned from = (char*)de - kaddr;
265 unsigned to = from + minix_sb(inode->i_sb)->s_dirsize;
269 err = mapping->a_ops->prepare_write(NULL, page, from, to);
272 err = dir_commit_chunk(page, from, to);
277 inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC;
278 mark_inode_dirty(inode);
282 int minix_make_empty(struct inode *inode, struct inode *dir)
284 struct address_space *mapping = inode->i_mapping;
285 struct page *page = grab_cache_page(mapping, 0);
286 struct minix_sb_info * sbi = minix_sb(inode->i_sb);
287 struct minix_dir_entry * de;
293 err = mapping->a_ops->prepare_write(NULL, page, 0, 2 * sbi->s_dirsize);
299 kaddr = kmap_atomic(page, KM_USER0);
300 memset(kaddr, 0, PAGE_CACHE_SIZE);
302 de = (struct minix_dir_entry *)kaddr;
303 de->inode = inode->i_ino;
304 strcpy(de->name,".");
305 de = minix_next_entry(de, sbi);
306 de->inode = dir->i_ino;
307 strcpy(de->name,"..");
308 kunmap_atomic(kaddr, KM_USER0);
310 err = dir_commit_chunk(page, 0, 2 * sbi->s_dirsize);
312 page_cache_release(page);
317 * routine to check that the specified directory is empty (for rmdir)
319 int minix_empty_dir(struct inode * inode)
321 struct page *page = NULL;
322 unsigned long i, npages = dir_pages(inode);
323 struct minix_sb_info *sbi = minix_sb(inode->i_sb);
325 for (i = 0; i < npages; i++) {
328 page = dir_get_page(inode, i);
333 kaddr = (char *)page_address(page);
334 de = (minix_dirent *)kaddr;
335 kaddr += minix_last_byte(inode, i) - sbi->s_dirsize;
337 while ((char *)de <= kaddr) {
338 if (de->inode != 0) {
339 /* check for . and .. */
340 if (de->name[0] != '.')
343 if (de->inode != inode->i_ino)
345 } else if (de->name[1] != '.')
347 else if (de->name[2])
350 de = minix_next_entry(de, sbi);
361 /* Releases the page */
362 void minix_set_link(struct minix_dir_entry *de, struct page *page,
365 struct inode *dir = (struct inode*)page->mapping->host;
366 struct minix_sb_info *sbi = minix_sb(dir->i_sb);
367 unsigned from = (char *)de-(char*)page_address(page);
368 unsigned to = from + sbi->s_dirsize;
372 err = page->mapping->a_ops->prepare_write(NULL, page, from, to);
374 de->inode = inode->i_ino;
375 err = dir_commit_chunk(page, from, to);
380 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
381 mark_inode_dirty(dir);
384 struct minix_dir_entry * minix_dotdot (struct inode *dir, struct page **p)
386 struct page *page = dir_get_page(dir, 0);
387 struct minix_sb_info *sbi = minix_sb(dir->i_sb);
388 struct minix_dir_entry *de = NULL;
391 de = minix_next_entry(page_address(page), sbi);
397 ino_t minix_inode_by_name(struct dentry *dentry)
400 struct minix_dir_entry *de = minix_find_entry(dentry, &page);