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 void dir_commit_chunk(struct page *page, loff_t pos, unsigned len)
33 struct address_space *mapping = page->mapping;
34 struct inode *dir = mapping->host;
36 block_write_end(NULL, mapping, pos, len, len, page, NULL);
37 if (pos+len > dir->i_size) {
38 i_size_write(dir, pos+len);
39 mark_inode_dirty(dir);
44 static int sysv_handle_dirsync(struct inode *dir)
48 err = filemap_write_and_wait(dir->i_mapping);
50 err = sync_inode_metadata(dir, 1);
55 * Calls to dir_get_page()/unmap_and_put_page() must be nested according to the
56 * rules documented in mm/highmem.rst.
58 * NOTE: sysv_find_entry() and sysv_dotdot() act as calls to dir_get_page()
59 * and must be treated accordingly for nesting purposes.
61 static void *dir_get_page(struct inode *dir, unsigned long n, struct page **p)
63 struct address_space *mapping = dir->i_mapping;
64 struct page *page = read_mapping_page(mapping, n, NULL);
66 return ERR_CAST(page);
68 return kmap_local_page(page);
71 static int sysv_readdir(struct file *file, struct dir_context *ctx)
73 unsigned long pos = ctx->pos;
74 struct inode *inode = file_inode(file);
75 struct super_block *sb = inode->i_sb;
76 unsigned long npages = dir_pages(inode);
80 ctx->pos = pos = (pos + SYSV_DIRSIZE-1) & ~(SYSV_DIRSIZE-1);
81 if (pos >= inode->i_size)
84 offset = pos & ~PAGE_MASK;
85 n = pos >> PAGE_SHIFT;
87 for ( ; n < npages; n++, offset = 0) {
89 struct sysv_dir_entry *de;
92 kaddr = dir_get_page(inode, n, &page);
95 de = (struct sysv_dir_entry *)(kaddr+offset);
96 limit = kaddr + PAGE_SIZE - SYSV_DIRSIZE;
97 for ( ;(char*)de <= limit; de++, ctx->pos += sizeof(*de)) {
98 char *name = de->name;
103 if (!dir_emit(ctx, name, strnlen(name,SYSV_NAMELEN),
104 fs16_to_cpu(SYSV_SB(sb), de->inode),
106 unmap_and_put_page(page, kaddr);
110 unmap_and_put_page(page, kaddr);
115 /* compare strings: name[0..len-1] (not zero-terminated) and
116 * buffer[0..] (filled with zeroes up to buffer[0..maxlen-1])
118 static inline int namecompare(int len, int maxlen,
119 const char * name, const char * buffer)
121 if (len < maxlen && buffer[len])
123 return !memcmp(name, buffer, len);
129 * finds an entry in the specified directory with the wanted name. It
130 * returns the cache buffer in which the entry was found, and the entry
131 * itself (as a parameter - res_dir). It does NOT read the inode of the
132 * entry - you'll have to do that yourself if you want to.
134 * On Success unmap_and_put_page() should be called on *res_page.
136 * sysv_find_entry() acts as a call to dir_get_page() and must be treated
137 * accordingly for nesting purposes.
139 struct sysv_dir_entry *sysv_find_entry(struct dentry *dentry, struct page **res_page)
141 const char * name = dentry->d_name.name;
142 int namelen = dentry->d_name.len;
143 struct inode * dir = d_inode(dentry->d_parent);
144 unsigned long start, n;
145 unsigned long npages = dir_pages(dir);
146 struct page *page = NULL;
147 struct sysv_dir_entry *de;
151 start = SYSV_I(dir)->i_dir_start_lookup;
157 char *kaddr = dir_get_page(dir, n, &page);
159 if (!IS_ERR(kaddr)) {
160 de = (struct sysv_dir_entry *)kaddr;
161 kaddr += PAGE_SIZE - SYSV_DIRSIZE;
162 for ( ; (char *) de <= kaddr ; de++) {
165 if (namecompare(namelen, SYSV_NAMELEN,
169 unmap_and_put_page(page, kaddr);
174 } while (n != start);
179 SYSV_I(dir)->i_dir_start_lookup = n;
184 int sysv_add_link(struct dentry *dentry, struct inode *inode)
186 struct inode *dir = d_inode(dentry->d_parent);
187 const char * name = dentry->d_name.name;
188 int namelen = dentry->d_name.len;
189 struct page *page = NULL;
190 struct sysv_dir_entry * de;
191 unsigned long npages = dir_pages(dir);
197 /* We take care of directory expansion in the same loop */
198 for (n = 0; n <= npages; n++) {
199 kaddr = dir_get_page(dir, n, &page);
201 return PTR_ERR(kaddr);
202 de = (struct sysv_dir_entry *)kaddr;
203 kaddr += PAGE_SIZE - SYSV_DIRSIZE;
204 while ((char *)de <= kaddr) {
208 if (namecompare(namelen, SYSV_NAMELEN, name, de->name))
212 unmap_and_put_page(page, kaddr);
218 pos = page_offset(page) + offset_in_page(de);
220 err = sysv_prepare_chunk(page, pos, SYSV_DIRSIZE);
223 memcpy (de->name, name, namelen);
224 memset (de->name + namelen, 0, SYSV_DIRSIZE - namelen - 2);
225 de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), inode->i_ino);
226 dir_commit_chunk(page, pos, SYSV_DIRSIZE);
227 dir->i_mtime = inode_set_ctime_current(dir);
228 mark_inode_dirty(dir);
229 err = sysv_handle_dirsync(dir);
231 unmap_and_put_page(page, kaddr);
238 int sysv_delete_entry(struct sysv_dir_entry *de, struct page *page)
240 struct inode *inode = page->mapping->host;
241 loff_t pos = page_offset(page) + offset_in_page(de);
245 err = sysv_prepare_chunk(page, pos, SYSV_DIRSIZE);
251 dir_commit_chunk(page, pos, SYSV_DIRSIZE);
252 inode->i_mtime = inode_set_ctime_current(inode);
253 mark_inode_dirty(inode);
254 return sysv_handle_dirsync(inode);
257 int sysv_make_empty(struct inode *inode, struct inode *dir)
259 struct page *page = grab_cache_page(inode->i_mapping, 0);
260 struct sysv_dir_entry * de;
266 err = sysv_prepare_chunk(page, 0, 2 * SYSV_DIRSIZE);
271 base = kmap_local_page(page);
272 memset(base, 0, PAGE_SIZE);
274 de = (struct sysv_dir_entry *) base;
275 de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), inode->i_ino);
276 strcpy(de->name,".");
278 de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), dir->i_ino);
279 strcpy(de->name,"..");
282 dir_commit_chunk(page, 0, 2 * SYSV_DIRSIZE);
283 err = sysv_handle_dirsync(inode);
290 * routine to check that the specified directory is empty (for rmdir)
292 int sysv_empty_dir(struct inode * inode)
294 struct super_block *sb = inode->i_sb;
295 struct page *page = NULL;
296 unsigned long i, npages = dir_pages(inode);
299 for (i = 0; i < npages; i++) {
300 struct sysv_dir_entry *de;
302 kaddr = dir_get_page(inode, i, &page);
306 de = (struct sysv_dir_entry *)kaddr;
307 kaddr += PAGE_SIZE-SYSV_DIRSIZE;
309 for ( ;(char *)de <= kaddr; de++) {
312 /* check for . and .. */
313 if (de->name[0] != '.')
316 if (de->inode == cpu_to_fs16(SYSV_SB(sb),
321 if (de->name[1] != '.' || de->name[2])
324 unmap_and_put_page(page, kaddr);
329 unmap_and_put_page(page, kaddr);
333 /* Releases the page */
334 int sysv_set_link(struct sysv_dir_entry *de, struct page *page,
337 struct inode *dir = page->mapping->host;
338 loff_t pos = page_offset(page) + offset_in_page(de);
342 err = sysv_prepare_chunk(page, pos, SYSV_DIRSIZE);
347 de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), inode->i_ino);
348 dir_commit_chunk(page, pos, SYSV_DIRSIZE);
349 dir->i_mtime = inode_set_ctime_current(dir);
350 mark_inode_dirty(dir);
351 return sysv_handle_dirsync(inode);
355 * Calls to dir_get_page()/unmap_and_put_page() must be nested according to the
356 * rules documented in mm/highmem.rst.
358 * sysv_dotdot() acts as a call to dir_get_page() and must be treated
359 * accordingly for nesting purposes.
361 struct sysv_dir_entry *sysv_dotdot(struct inode *dir, struct page **p)
363 struct sysv_dir_entry *de = dir_get_page(dir, 0, p);
367 /* ".." is the second directory entry */
371 ino_t sysv_inode_by_name(struct dentry *dentry)
374 struct sysv_dir_entry *de = sysv_find_entry (dentry, &page);
378 res = fs16_to_cpu(SYSV_SB(dentry->d_sb), de->inode);
379 unmap_and_put_page(page, de);