1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 1991, 1992 Linus Torvalds
7 * minix directory handling functions
9 * Updated to filesystem version 3 by Daniel Aragones
13 #include <linux/buffer_head.h>
14 #include <linux/highmem.h>
15 #include <linux/swap.h>
17 typedef struct minix_dir_entry minix_dirent;
18 typedef struct minix3_dir_entry minix3_dirent;
20 static int minix_readdir(struct file *, struct dir_context *);
22 const struct file_operations minix_dir_operations = {
23 .llseek = generic_file_llseek,
24 .read = generic_read_dir,
25 .iterate_shared = minix_readdir,
26 .fsync = generic_file_fsync,
29 static inline void dir_put_page(struct page *page)
36 * Return the offset into page `page_nr' of the last valid
37 * byte in that page, plus one.
40 minix_last_byte(struct inode *inode, unsigned long page_nr)
42 unsigned last_byte = PAGE_SIZE;
44 if (page_nr == (inode->i_size >> PAGE_SHIFT))
45 last_byte = inode->i_size & (PAGE_SIZE - 1);
49 static void dir_commit_chunk(struct page *page, loff_t pos, unsigned len)
51 struct address_space *mapping = page->mapping;
52 struct inode *dir = mapping->host;
54 block_write_end(NULL, mapping, pos, len, len, page, NULL);
56 if (pos+len > dir->i_size) {
57 i_size_write(dir, pos+len);
58 mark_inode_dirty(dir);
63 static int minix_handle_dirsync(struct inode *dir)
67 err = filemap_write_and_wait(dir->i_mapping);
69 err = sync_inode_metadata(dir, 1);
73 static struct page * dir_get_page(struct inode *dir, unsigned long n)
75 struct address_space *mapping = dir->i_mapping;
76 struct page *page = read_mapping_page(mapping, n, NULL);
82 static inline void *minix_next_entry(void *de, struct minix_sb_info *sbi)
84 return (void*)((char*)de + sbi->s_dirsize);
87 static int minix_readdir(struct file *file, struct dir_context *ctx)
89 struct inode *inode = file_inode(file);
90 struct super_block *sb = inode->i_sb;
91 struct minix_sb_info *sbi = minix_sb(sb);
92 unsigned chunk_size = sbi->s_dirsize;
93 unsigned long npages = dir_pages(inode);
94 unsigned long pos = ctx->pos;
98 ctx->pos = pos = ALIGN(pos, chunk_size);
99 if (pos >= inode->i_size)
102 offset = pos & ~PAGE_MASK;
103 n = pos >> PAGE_SHIFT;
105 for ( ; n < npages; n++, offset = 0) {
106 char *p, *kaddr, *limit;
107 struct page *page = dir_get_page(inode, n);
111 kaddr = (char *)page_address(page);
113 limit = kaddr + minix_last_byte(inode, n) - chunk_size;
114 for ( ; p <= limit; p = minix_next_entry(p, sbi)) {
117 if (sbi->s_version == MINIX_V3) {
118 minix3_dirent *de3 = (minix3_dirent *)p;
120 inumber = de3->inode;
122 minix_dirent *de = (minix_dirent *)p;
127 unsigned l = strnlen(name, sbi->s_namelen);
128 if (!dir_emit(ctx, name, l,
129 inumber, DT_UNKNOWN)) {
134 ctx->pos += chunk_size;
141 static inline int namecompare(int len, int maxlen,
142 const char * name, const char * buffer)
144 if (len < maxlen && buffer[len])
146 return !memcmp(name, buffer, len);
152 * finds an entry in the specified directory with the wanted name. It
153 * returns the cache buffer in which the entry was found, and the entry
154 * itself (as a parameter - res_dir). It does NOT read the inode of the
155 * entry - you'll have to do that yourself if you want to.
157 minix_dirent *minix_find_entry(struct dentry *dentry, struct page **res_page)
159 const char * name = dentry->d_name.name;
160 int namelen = dentry->d_name.len;
161 struct inode * dir = d_inode(dentry->d_parent);
162 struct super_block * sb = dir->i_sb;
163 struct minix_sb_info * sbi = minix_sb(sb);
165 unsigned long npages = dir_pages(dir);
166 struct page *page = NULL;
173 for (n = 0; n < npages; n++) {
176 page = dir_get_page(dir, n);
180 kaddr = (char*)page_address(page);
181 limit = kaddr + minix_last_byte(dir, n) - sbi->s_dirsize;
182 for (p = kaddr; p <= limit; p = minix_next_entry(p, sbi)) {
183 if (sbi->s_version == MINIX_V3) {
184 minix3_dirent *de3 = (minix3_dirent *)p;
186 inumber = de3->inode;
188 minix_dirent *de = (minix_dirent *)p;
194 if (namecompare(namelen, sbi->s_namelen, name, namx))
203 return (minix_dirent *)p;
206 int minix_add_link(struct dentry *dentry, struct inode *inode)
208 struct inode *dir = d_inode(dentry->d_parent);
209 const char * name = dentry->d_name.name;
210 int namelen = dentry->d_name.len;
211 struct super_block * sb = dir->i_sb;
212 struct minix_sb_info * sbi = minix_sb(sb);
213 struct page *page = NULL;
214 unsigned long npages = dir_pages(dir);
225 * We take care of directory expansion in the same loop
226 * This code plays outside i_size, so it locks the page
227 * to protect that region.
229 for (n = 0; n <= npages; n++) {
230 char *limit, *dir_end;
232 page = dir_get_page(dir, n);
237 kaddr = (char*)page_address(page);
238 dir_end = kaddr + minix_last_byte(dir, n);
239 limit = kaddr + PAGE_SIZE - sbi->s_dirsize;
240 for (p = kaddr; p <= limit; p = minix_next_entry(p, sbi)) {
241 de = (minix_dirent *)p;
242 de3 = (minix3_dirent *)p;
243 if (sbi->s_version == MINIX_V3) {
245 inumber = de3->inode;
252 if (sbi->s_version == MINIX_V3)
261 if (namecompare(namelen, sbi->s_namelen, name, namx))
271 pos = page_offset(page) + p - (char *)page_address(page);
272 err = minix_prepare_chunk(page, pos, sbi->s_dirsize);
275 memcpy (namx, name, namelen);
276 if (sbi->s_version == MINIX_V3) {
277 memset (namx + namelen, 0, sbi->s_dirsize - namelen - 4);
278 de3->inode = inode->i_ino;
280 memset (namx + namelen, 0, sbi->s_dirsize - namelen - 2);
281 de->inode = inode->i_ino;
283 dir_commit_chunk(page, pos, sbi->s_dirsize);
284 dir->i_mtime = inode_set_ctime_current(dir);
285 mark_inode_dirty(dir);
286 err = minix_handle_dirsync(dir);
296 int minix_delete_entry(struct minix_dir_entry *de, struct page *page)
298 struct inode *inode = page->mapping->host;
299 char *kaddr = page_address(page);
300 loff_t pos = page_offset(page) + (char*)de - kaddr;
301 struct minix_sb_info *sbi = minix_sb(inode->i_sb);
302 unsigned len = sbi->s_dirsize;
306 err = minix_prepare_chunk(page, pos, len);
311 if (sbi->s_version == MINIX_V3)
312 ((minix3_dirent *)de)->inode = 0;
315 dir_commit_chunk(page, pos, len);
316 inode->i_mtime = inode_set_ctime_current(inode);
317 mark_inode_dirty(inode);
318 return minix_handle_dirsync(inode);
321 int minix_make_empty(struct inode *inode, struct inode *dir)
323 struct page *page = grab_cache_page(inode->i_mapping, 0);
324 struct minix_sb_info *sbi = minix_sb(inode->i_sb);
330 err = minix_prepare_chunk(page, 0, 2 * sbi->s_dirsize);
336 kaddr = kmap_atomic(page);
337 memset(kaddr, 0, PAGE_SIZE);
339 if (sbi->s_version == MINIX_V3) {
340 minix3_dirent *de3 = (minix3_dirent *)kaddr;
342 de3->inode = inode->i_ino;
343 strcpy(de3->name, ".");
344 de3 = minix_next_entry(de3, sbi);
345 de3->inode = dir->i_ino;
346 strcpy(de3->name, "..");
348 minix_dirent *de = (minix_dirent *)kaddr;
350 de->inode = inode->i_ino;
351 strcpy(de->name, ".");
352 de = minix_next_entry(de, sbi);
353 de->inode = dir->i_ino;
354 strcpy(de->name, "..");
356 kunmap_atomic(kaddr);
358 dir_commit_chunk(page, 0, 2 * sbi->s_dirsize);
359 err = minix_handle_dirsync(inode);
366 * routine to check that the specified directory is empty (for rmdir)
368 int minix_empty_dir(struct inode * inode)
370 struct page *page = NULL;
371 unsigned long i, npages = dir_pages(inode);
372 struct minix_sb_info *sbi = minix_sb(inode->i_sb);
376 for (i = 0; i < npages; i++) {
377 char *p, *kaddr, *limit;
379 page = dir_get_page(inode, i);
383 kaddr = (char *)page_address(page);
384 limit = kaddr + minix_last_byte(inode, i) - sbi->s_dirsize;
385 for (p = kaddr; p <= limit; p = minix_next_entry(p, sbi)) {
386 if (sbi->s_version == MINIX_V3) {
387 minix3_dirent *de3 = (minix3_dirent *)p;
389 inumber = de3->inode;
391 minix_dirent *de = (minix_dirent *)p;
397 /* check for . and .. */
401 if (inumber != inode->i_ino)
403 } else if (name[1] != '.')
418 /* Releases the page */
419 int minix_set_link(struct minix_dir_entry *de, struct page *page,
422 struct inode *dir = page->mapping->host;
423 struct minix_sb_info *sbi = minix_sb(dir->i_sb);
424 loff_t pos = page_offset(page) +
425 (char *)de-(char*)page_address(page);
429 err = minix_prepare_chunk(page, pos, sbi->s_dirsize);
434 if (sbi->s_version == MINIX_V3)
435 ((minix3_dirent *)de)->inode = inode->i_ino;
437 de->inode = inode->i_ino;
438 dir_commit_chunk(page, pos, sbi->s_dirsize);
439 dir->i_mtime = inode_set_ctime_current(dir);
440 mark_inode_dirty(dir);
441 return minix_handle_dirsync(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);
464 struct address_space *mapping = page->mapping;
465 struct inode *inode = mapping->host;
466 struct minix_sb_info *sbi = minix_sb(inode->i_sb);
468 if (sbi->s_version == MINIX_V3)
469 res = ((minix3_dirent *) de)->inode;