1 // SPDX-License-Identifier: GPL-2.0
6 * Copyright (C) 1991, 1992 Linus Torvalds
9 * Copyright (C) 1993 Pascal Haible, Bruno Haible
12 * Copyright (C) 1993 Bruno Haible
14 * SystemV/Coherent directory handling functions
17 #include <linux/pagemap.h>
18 #include <linux/highmem.h>
19 #include <linux/swap.h>
22 static int sysv_readdir(struct file *, struct dir_context *);
24 const struct file_operations sysv_dir_operations = {
25 .llseek = generic_file_llseek,
26 .read = generic_read_dir,
27 .iterate_shared = sysv_readdir,
28 .fsync = generic_file_fsync,
31 static inline void dir_put_page(struct page *page)
37 static int dir_commit_chunk(struct page *page, loff_t pos, unsigned len)
39 struct address_space *mapping = page->mapping;
40 struct inode *dir = mapping->host;
43 block_write_end(NULL, mapping, pos, len, len, page, NULL);
44 if (pos+len > dir->i_size) {
45 i_size_write(dir, pos+len);
46 mark_inode_dirty(dir);
49 err = write_one_page(page);
55 static struct page * dir_get_page(struct inode *dir, unsigned long n)
57 struct address_space *mapping = dir->i_mapping;
58 struct page *page = read_mapping_page(mapping, n, NULL);
64 static int sysv_readdir(struct file *file, struct dir_context *ctx)
66 unsigned long pos = ctx->pos;
67 struct inode *inode = file_inode(file);
68 struct super_block *sb = inode->i_sb;
69 unsigned long npages = dir_pages(inode);
73 ctx->pos = pos = (pos + SYSV_DIRSIZE-1) & ~(SYSV_DIRSIZE-1);
74 if (pos >= inode->i_size)
77 offset = pos & ~PAGE_MASK;
78 n = pos >> PAGE_SHIFT;
80 for ( ; n < npages; n++, offset = 0) {
82 struct sysv_dir_entry *de;
83 struct page *page = dir_get_page(inode, n);
87 kaddr = (char *)page_address(page);
88 de = (struct sysv_dir_entry *)(kaddr+offset);
89 limit = kaddr + PAGE_SIZE - SYSV_DIRSIZE;
90 for ( ;(char*)de <= limit; de++, ctx->pos += sizeof(*de)) {
91 char *name = de->name;
96 if (!dir_emit(ctx, name, strnlen(name,SYSV_NAMELEN),
97 fs16_to_cpu(SYSV_SB(sb), de->inode),
108 /* compare strings: name[0..len-1] (not zero-terminated) and
109 * buffer[0..] (filled with zeroes up to buffer[0..maxlen-1])
111 static inline int namecompare(int len, int maxlen,
112 const char * name, const char * buffer)
114 if (len < maxlen && buffer[len])
116 return !memcmp(name, buffer, len);
122 * finds an entry in the specified directory with the wanted name. It
123 * returns the cache buffer in which the entry was found, and the entry
124 * itself (as a parameter - res_dir). It does NOT read the inode of the
125 * entry - you'll have to do that yourself if you want to.
127 struct sysv_dir_entry *sysv_find_entry(struct dentry *dentry, struct page **res_page)
129 const char * name = dentry->d_name.name;
130 int namelen = dentry->d_name.len;
131 struct inode * dir = d_inode(dentry->d_parent);
132 unsigned long start, n;
133 unsigned long npages = dir_pages(dir);
134 struct page *page = NULL;
135 struct sysv_dir_entry *de;
139 start = SYSV_I(dir)->i_dir_start_lookup;
146 page = dir_get_page(dir, n);
148 kaddr = (char*)page_address(page);
149 de = (struct sysv_dir_entry *) kaddr;
150 kaddr += PAGE_SIZE - SYSV_DIRSIZE;
151 for ( ; (char *) de <= kaddr ; de++) {
154 if (namecompare(namelen, SYSV_NAMELEN,
163 } while (n != start);
168 SYSV_I(dir)->i_dir_start_lookup = n;
173 int sysv_add_link(struct dentry *dentry, struct inode *inode)
175 struct inode *dir = d_inode(dentry->d_parent);
176 const char * name = dentry->d_name.name;
177 int namelen = dentry->d_name.len;
178 struct page *page = NULL;
179 struct sysv_dir_entry * de;
180 unsigned long npages = dir_pages(dir);
186 /* We take care of directory expansion in the same loop */
187 for (n = 0; n <= npages; n++) {
188 page = dir_get_page(dir, n);
192 kaddr = (char*)page_address(page);
193 de = (struct sysv_dir_entry *)kaddr;
194 kaddr += PAGE_SIZE - SYSV_DIRSIZE;
195 while ((char *)de <= kaddr) {
199 if (namecompare(namelen, SYSV_NAMELEN, name, de->name))
209 pos = page_offset(page) +
210 (char*)de - (char*)page_address(page);
212 err = sysv_prepare_chunk(page, pos, SYSV_DIRSIZE);
215 memcpy (de->name, name, namelen);
216 memset (de->name + namelen, 0, SYSV_DIRSIZE - namelen - 2);
217 de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), inode->i_ino);
218 err = dir_commit_chunk(page, pos, SYSV_DIRSIZE);
219 dir->i_mtime = dir->i_ctime = current_time(dir);
220 mark_inode_dirty(dir);
230 int sysv_delete_entry(struct sysv_dir_entry *de, struct page *page)
232 struct inode *inode = page->mapping->host;
233 char *kaddr = (char*)page_address(page);
234 loff_t pos = page_offset(page) + (char *)de - kaddr;
238 err = sysv_prepare_chunk(page, pos, SYSV_DIRSIZE);
241 err = dir_commit_chunk(page, pos, SYSV_DIRSIZE);
243 inode->i_ctime = inode->i_mtime = current_time(inode);
244 mark_inode_dirty(inode);
248 int sysv_make_empty(struct inode *inode, struct inode *dir)
250 struct page *page = grab_cache_page(inode->i_mapping, 0);
251 struct sysv_dir_entry * de;
257 err = sysv_prepare_chunk(page, 0, 2 * SYSV_DIRSIZE);
264 base = (char*)page_address(page);
265 memset(base, 0, PAGE_SIZE);
267 de = (struct sysv_dir_entry *) base;
268 de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), inode->i_ino);
269 strcpy(de->name,".");
271 de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), dir->i_ino);
272 strcpy(de->name,"..");
275 err = dir_commit_chunk(page, 0, 2 * SYSV_DIRSIZE);
282 * routine to check that the specified directory is empty (for rmdir)
284 int sysv_empty_dir(struct inode * inode)
286 struct super_block *sb = inode->i_sb;
287 struct page *page = NULL;
288 unsigned long i, npages = dir_pages(inode);
290 for (i = 0; i < npages; i++) {
292 struct sysv_dir_entry * de;
293 page = dir_get_page(inode, i);
298 kaddr = (char *)page_address(page);
299 de = (struct sysv_dir_entry *)kaddr;
300 kaddr += PAGE_SIZE-SYSV_DIRSIZE;
302 for ( ;(char *)de <= kaddr; de++) {
305 /* check for . and .. */
306 if (de->name[0] != '.')
309 if (de->inode == cpu_to_fs16(SYSV_SB(sb),
314 if (de->name[1] != '.' || de->name[2])
326 /* Releases the page */
327 void sysv_set_link(struct sysv_dir_entry *de, struct page *page,
330 struct inode *dir = page->mapping->host;
331 loff_t pos = page_offset(page) +
332 (char *)de-(char*)page_address(page);
336 err = sysv_prepare_chunk(page, pos, SYSV_DIRSIZE);
338 de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), inode->i_ino);
339 err = dir_commit_chunk(page, pos, SYSV_DIRSIZE);
341 dir->i_mtime = dir->i_ctime = current_time(dir);
342 mark_inode_dirty(dir);
345 struct sysv_dir_entry * sysv_dotdot (struct inode *dir, struct page **p)
347 struct page *page = dir_get_page(dir, 0);
348 struct sysv_dir_entry *de = NULL;
351 de = (struct sysv_dir_entry*) page_address(page) + 1;
357 ino_t sysv_inode_by_name(struct dentry *dentry)
360 struct sysv_dir_entry *de = sysv_find_entry (dentry, &page);
364 res = fs16_to_cpu(SYSV_SB(dentry->d_sb), de->inode);