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 folio *folio, loff_t pos, unsigned len)
33 struct address_space *mapping = folio->mapping;
34 struct inode *dir = mapping->host;
36 block_write_end(NULL, mapping, pos, len, len, folio, 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_folio()/folio_release_kmap() 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_folio()
59 * and must be treated accordingly for nesting purposes.
61 static void *dir_get_folio(struct inode *dir, unsigned long n,
62 struct folio **foliop)
64 struct folio *folio = read_mapping_folio(dir->i_mapping, n, NULL);
67 return ERR_CAST(folio);
69 return kmap_local_folio(folio, 0);
72 static int sysv_readdir(struct file *file, struct dir_context *ctx)
74 unsigned long pos = ctx->pos;
75 struct inode *inode = file_inode(file);
76 struct super_block *sb = inode->i_sb;
77 unsigned long npages = dir_pages(inode);
81 ctx->pos = pos = (pos + SYSV_DIRSIZE-1) & ~(SYSV_DIRSIZE-1);
82 if (pos >= inode->i_size)
85 offset = pos & ~PAGE_MASK;
86 n = pos >> PAGE_SHIFT;
88 for ( ; n < npages; n++, offset = 0) {
90 struct sysv_dir_entry *de;
93 kaddr = dir_get_folio(inode, n, &folio);
96 de = (struct sysv_dir_entry *)(kaddr+offset);
97 limit = kaddr + PAGE_SIZE - SYSV_DIRSIZE;
98 for ( ;(char*)de <= limit; de++, ctx->pos += sizeof(*de)) {
99 char *name = de->name;
104 if (!dir_emit(ctx, name, strnlen(name,SYSV_NAMELEN),
105 fs16_to_cpu(SYSV_SB(sb), de->inode),
107 folio_release_kmap(folio, kaddr);
111 folio_release_kmap(folio, kaddr);
116 /* compare strings: name[0..len-1] (not zero-terminated) and
117 * buffer[0..] (filled with zeroes up to buffer[0..maxlen-1])
119 static inline int namecompare(int len, int maxlen,
120 const char * name, const char * buffer)
122 if (len < maxlen && buffer[len])
124 return !memcmp(name, buffer, len);
130 * finds an entry in the specified directory with the wanted name.
131 * It does NOT read the inode of the
132 * entry - you'll have to do that yourself if you want to.
134 * On Success folio_release_kmap() should be called on *foliop.
136 * sysv_find_entry() acts as a call to dir_get_folio() and must be treated
137 * accordingly for nesting purposes.
139 struct sysv_dir_entry *sysv_find_entry(struct dentry *dentry, struct folio **foliop)
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 sysv_dir_entry *de;
148 start = SYSV_I(dir)->i_dir_start_lookup;
154 char *kaddr = dir_get_folio(dir, n, foliop);
156 if (!IS_ERR(kaddr)) {
157 de = (struct sysv_dir_entry *)kaddr;
158 kaddr += folio_size(*foliop) - SYSV_DIRSIZE;
159 for ( ; (char *) de <= kaddr ; de++) {
162 if (namecompare(namelen, SYSV_NAMELEN,
166 folio_release_kmap(*foliop, kaddr);
171 } while (n != start);
176 SYSV_I(dir)->i_dir_start_lookup = n;
180 int sysv_add_link(struct dentry *dentry, struct inode *inode)
182 struct inode *dir = d_inode(dentry->d_parent);
183 const char * name = dentry->d_name.name;
184 int namelen = dentry->d_name.len;
185 struct folio *folio = NULL;
186 struct sysv_dir_entry * de;
187 unsigned long npages = dir_pages(dir);
193 /* We take care of directory expansion in the same loop */
194 for (n = 0; n <= npages; n++) {
195 kaddr = dir_get_folio(dir, n, &folio);
197 return PTR_ERR(kaddr);
198 de = (struct sysv_dir_entry *)kaddr;
199 kaddr += PAGE_SIZE - SYSV_DIRSIZE;
200 while ((char *)de <= kaddr) {
204 if (namecompare(namelen, SYSV_NAMELEN, name, de->name))
208 folio_release_kmap(folio, kaddr);
214 pos = folio_pos(folio) + offset_in_folio(folio, de);
216 err = sysv_prepare_chunk(folio, pos, SYSV_DIRSIZE);
219 memcpy (de->name, name, namelen);
220 memset (de->name + namelen, 0, SYSV_DIRSIZE - namelen - 2);
221 de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), inode->i_ino);
222 dir_commit_chunk(folio, pos, SYSV_DIRSIZE);
223 inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
224 mark_inode_dirty(dir);
225 err = sysv_handle_dirsync(dir);
227 folio_release_kmap(folio, kaddr);
234 int sysv_delete_entry(struct sysv_dir_entry *de, struct folio *folio)
236 struct inode *inode = folio->mapping->host;
237 loff_t pos = folio_pos(folio) + offset_in_folio(folio, de);
241 err = sysv_prepare_chunk(folio, pos, SYSV_DIRSIZE);
247 dir_commit_chunk(folio, pos, SYSV_DIRSIZE);
248 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
249 mark_inode_dirty(inode);
250 return sysv_handle_dirsync(inode);
253 int sysv_make_empty(struct inode *inode, struct inode *dir)
255 struct folio *folio = filemap_grab_folio(inode->i_mapping, 0);
256 struct sysv_dir_entry * de;
261 return PTR_ERR(folio);
262 err = sysv_prepare_chunk(folio, 0, 2 * SYSV_DIRSIZE);
267 kaddr = kmap_local_folio(folio, 0);
268 memset(kaddr, 0, folio_size(folio));
270 de = (struct sysv_dir_entry *)kaddr;
271 de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), inode->i_ino);
272 strcpy(de->name,".");
274 de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), dir->i_ino);
275 strcpy(de->name,"..");
278 dir_commit_chunk(folio, 0, 2 * SYSV_DIRSIZE);
279 err = sysv_handle_dirsync(inode);
286 * routine to check that the specified directory is empty (for rmdir)
288 int sysv_empty_dir(struct inode * inode)
290 struct super_block *sb = inode->i_sb;
291 struct folio *folio = NULL;
292 unsigned long i, npages = dir_pages(inode);
295 for (i = 0; i < npages; i++) {
296 struct sysv_dir_entry *de;
298 kaddr = dir_get_folio(inode, i, &folio);
302 de = (struct sysv_dir_entry *)kaddr;
303 kaddr += folio_size(folio) - SYSV_DIRSIZE;
305 for ( ;(char *)de <= kaddr; de++) {
308 /* check for . and .. */
309 if (de->name[0] != '.')
312 if (de->inode == cpu_to_fs16(SYSV_SB(sb),
317 if (de->name[1] != '.' || de->name[2])
320 folio_release_kmap(folio, kaddr);
325 folio_release_kmap(folio, kaddr);
329 /* Releases the page */
330 int sysv_set_link(struct sysv_dir_entry *de, struct folio *folio,
333 struct inode *dir = folio->mapping->host;
334 loff_t pos = folio_pos(folio) + offset_in_folio(folio, de);
338 err = sysv_prepare_chunk(folio, pos, SYSV_DIRSIZE);
343 de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), inode->i_ino);
344 dir_commit_chunk(folio, pos, SYSV_DIRSIZE);
345 inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
346 mark_inode_dirty(dir);
347 return sysv_handle_dirsync(inode);
351 * Calls to dir_get_folio()/folio_release_kmap() must be nested according to the
352 * rules documented in mm/highmem.rst.
354 * sysv_dotdot() acts as a call to dir_get_folio() and must be treated
355 * accordingly for nesting purposes.
357 struct sysv_dir_entry *sysv_dotdot(struct inode *dir, struct folio **foliop)
359 struct sysv_dir_entry *de = dir_get_folio(dir, 0, foliop);
363 /* ".." is the second directory entry */
367 ino_t sysv_inode_by_name(struct dentry *dentry)
370 struct sysv_dir_entry *de = sysv_find_entry (dentry, &folio);
374 res = fs16_to_cpu(SYSV_SB(dentry->d_sb), de->inode);
375 folio_release_kmap(folio, de);