xref: /linux/fs/sysv/dir.c (revision bf80eef2212a1e8451df13b52533f4bc31bb4f8e)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  linux/fs/sysv/dir.c
4  *
5  *  minix/dir.c
6  *  Copyright (C) 1991, 1992  Linus Torvalds
7  *
8  *  coh/dir.c
9  *  Copyright (C) 1993  Pascal Haible, Bruno Haible
10  *
11  *  sysv/dir.c
12  *  Copyright (C) 1993  Bruno Haible
13  *
14  *  SystemV/Coherent directory handling functions
15  */
16 
17 #include <linux/pagemap.h>
18 #include <linux/highmem.h>
19 #include <linux/swap.h>
20 #include "sysv.h"
21 
22 static int sysv_readdir(struct file *, struct dir_context *);
23 
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,
29 };
30 
31 static inline void dir_put_page(struct page *page)
32 {
33 	kunmap(page);
34 	put_page(page);
35 }
36 
37 static int dir_commit_chunk(struct page *page, loff_t pos, unsigned len)
38 {
39 	struct address_space *mapping = page->mapping;
40 	struct inode *dir = mapping->host;
41 	int err = 0;
42 
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);
47 	}
48 	if (IS_DIRSYNC(dir))
49 		err = write_one_page(page);
50 	else
51 		unlock_page(page);
52 	return err;
53 }
54 
55 static struct page * dir_get_page(struct inode *dir, unsigned long n)
56 {
57 	struct address_space *mapping = dir->i_mapping;
58 	struct page *page = read_mapping_page(mapping, n, NULL);
59 	if (!IS_ERR(page))
60 		kmap(page);
61 	return page;
62 }
63 
64 static int sysv_readdir(struct file *file, struct dir_context *ctx)
65 {
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);
70 	unsigned offset;
71 	unsigned long n;
72 
73 	ctx->pos = pos = (pos + SYSV_DIRSIZE-1) & ~(SYSV_DIRSIZE-1);
74 	if (pos >= inode->i_size)
75 		return 0;
76 
77 	offset = pos & ~PAGE_MASK;
78 	n = pos >> PAGE_SHIFT;
79 
80 	for ( ; n < npages; n++, offset = 0) {
81 		char *kaddr, *limit;
82 		struct sysv_dir_entry *de;
83 		struct page *page = dir_get_page(inode, n);
84 
85 		if (IS_ERR(page))
86 			continue;
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;
92 
93 			if (!de->inode)
94 				continue;
95 
96 			if (!dir_emit(ctx, name, strnlen(name,SYSV_NAMELEN),
97 					fs16_to_cpu(SYSV_SB(sb), de->inode),
98 					DT_UNKNOWN)) {
99 				dir_put_page(page);
100 				return 0;
101 			}
102 		}
103 		dir_put_page(page);
104 	}
105 	return 0;
106 }
107 
108 /* compare strings: name[0..len-1] (not zero-terminated) and
109  * buffer[0..] (filled with zeroes up to buffer[0..maxlen-1])
110  */
111 static inline int namecompare(int len, int maxlen,
112 	const char * name, const char * buffer)
113 {
114 	if (len < maxlen && buffer[len])
115 		return 0;
116 	return !memcmp(name, buffer, len);
117 }
118 
119 /*
120  *	sysv_find_entry()
121  *
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.
126  */
127 struct sysv_dir_entry *sysv_find_entry(struct dentry *dentry, struct page **res_page)
128 {
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;
136 
137 	*res_page = NULL;
138 
139 	start = SYSV_I(dir)->i_dir_start_lookup;
140 	if (start >= npages)
141 		start = 0;
142 	n = start;
143 
144 	do {
145 		char *kaddr;
146 		page = dir_get_page(dir, n);
147 		if (!IS_ERR(page)) {
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++) {
152 				if (!de->inode)
153 					continue;
154 				if (namecompare(namelen, SYSV_NAMELEN,
155 							name, de->name))
156 					goto found;
157 			}
158 			dir_put_page(page);
159 		}
160 
161 		if (++n >= npages)
162 			n = 0;
163 	} while (n != start);
164 
165 	return NULL;
166 
167 found:
168 	SYSV_I(dir)->i_dir_start_lookup = n;
169 	*res_page = page;
170 	return de;
171 }
172 
173 int sysv_add_link(struct dentry *dentry, struct inode *inode)
174 {
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);
181 	unsigned long n;
182 	char *kaddr;
183 	loff_t pos;
184 	int err;
185 
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);
189 		err = PTR_ERR(page);
190 		if (IS_ERR(page))
191 			goto out;
192 		kaddr = (char*)page_address(page);
193 		de = (struct sysv_dir_entry *)kaddr;
194 		kaddr += PAGE_SIZE - SYSV_DIRSIZE;
195 		while ((char *)de <= kaddr) {
196 			if (!de->inode)
197 				goto got_it;
198 			err = -EEXIST;
199 			if (namecompare(namelen, SYSV_NAMELEN, name, de->name))
200 				goto out_page;
201 			de++;
202 		}
203 		dir_put_page(page);
204 	}
205 	BUG();
206 	return -EINVAL;
207 
208 got_it:
209 	pos = page_offset(page) +
210 			(char*)de - (char*)page_address(page);
211 	lock_page(page);
212 	err = sysv_prepare_chunk(page, pos, SYSV_DIRSIZE);
213 	if (err)
214 		goto out_unlock;
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);
221 out_page:
222 	dir_put_page(page);
223 out:
224 	return err;
225 out_unlock:
226 	unlock_page(page);
227 	goto out_page;
228 }
229 
230 int sysv_delete_entry(struct sysv_dir_entry *de, struct page *page)
231 {
232 	struct inode *inode = page->mapping->host;
233 	char *kaddr = (char*)page_address(page);
234 	loff_t pos = page_offset(page) + (char *)de - kaddr;
235 	int err;
236 
237 	lock_page(page);
238 	err = sysv_prepare_chunk(page, pos, SYSV_DIRSIZE);
239 	BUG_ON(err);
240 	de->inode = 0;
241 	err = dir_commit_chunk(page, pos, SYSV_DIRSIZE);
242 	dir_put_page(page);
243 	inode->i_ctime = inode->i_mtime = current_time(inode);
244 	mark_inode_dirty(inode);
245 	return err;
246 }
247 
248 int sysv_make_empty(struct inode *inode, struct inode *dir)
249 {
250 	struct page *page = grab_cache_page(inode->i_mapping, 0);
251 	struct sysv_dir_entry * de;
252 	char *base;
253 	int err;
254 
255 	if (!page)
256 		return -ENOMEM;
257 	err = sysv_prepare_chunk(page, 0, 2 * SYSV_DIRSIZE);
258 	if (err) {
259 		unlock_page(page);
260 		goto fail;
261 	}
262 	kmap(page);
263 
264 	base = (char*)page_address(page);
265 	memset(base, 0, PAGE_SIZE);
266 
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,".");
270 	de++;
271 	de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), dir->i_ino);
272 	strcpy(de->name,"..");
273 
274 	kunmap(page);
275 	err = dir_commit_chunk(page, 0, 2 * SYSV_DIRSIZE);
276 fail:
277 	put_page(page);
278 	return err;
279 }
280 
281 /*
282  * routine to check that the specified directory is empty (for rmdir)
283  */
284 int sysv_empty_dir(struct inode * inode)
285 {
286 	struct super_block *sb = inode->i_sb;
287 	struct page *page = NULL;
288 	unsigned long i, npages = dir_pages(inode);
289 
290 	for (i = 0; i < npages; i++) {
291 		char *kaddr;
292 		struct sysv_dir_entry * de;
293 		page = dir_get_page(inode, i);
294 
295 		if (IS_ERR(page))
296 			continue;
297 
298 		kaddr = (char *)page_address(page);
299 		de = (struct sysv_dir_entry *)kaddr;
300 		kaddr += PAGE_SIZE-SYSV_DIRSIZE;
301 
302 		for ( ;(char *)de <= kaddr; de++) {
303 			if (!de->inode)
304 				continue;
305 			/* check for . and .. */
306 			if (de->name[0] != '.')
307 				goto not_empty;
308 			if (!de->name[1]) {
309 				if (de->inode == cpu_to_fs16(SYSV_SB(sb),
310 							inode->i_ino))
311 					continue;
312 				goto not_empty;
313 			}
314 			if (de->name[1] != '.' || de->name[2])
315 				goto not_empty;
316 		}
317 		dir_put_page(page);
318 	}
319 	return 1;
320 
321 not_empty:
322 	dir_put_page(page);
323 	return 0;
324 }
325 
326 /* Releases the page */
327 void sysv_set_link(struct sysv_dir_entry *de, struct page *page,
328 	struct inode *inode)
329 {
330 	struct inode *dir = page->mapping->host;
331 	loff_t pos = page_offset(page) +
332 			(char *)de-(char*)page_address(page);
333 	int err;
334 
335 	lock_page(page);
336 	err = sysv_prepare_chunk(page, pos, SYSV_DIRSIZE);
337 	BUG_ON(err);
338 	de->inode = cpu_to_fs16(SYSV_SB(inode->i_sb), inode->i_ino);
339 	err = dir_commit_chunk(page, pos, SYSV_DIRSIZE);
340 	dir_put_page(page);
341 	dir->i_mtime = dir->i_ctime = current_time(dir);
342 	mark_inode_dirty(dir);
343 }
344 
345 struct sysv_dir_entry * sysv_dotdot (struct inode *dir, struct page **p)
346 {
347 	struct page *page = dir_get_page(dir, 0);
348 	struct sysv_dir_entry *de = NULL;
349 
350 	if (!IS_ERR(page)) {
351 		de = (struct sysv_dir_entry*) page_address(page) + 1;
352 		*p = page;
353 	}
354 	return de;
355 }
356 
357 ino_t sysv_inode_by_name(struct dentry *dentry)
358 {
359 	struct page *page;
360 	struct sysv_dir_entry *de = sysv_find_entry (dentry, &page);
361 	ino_t res = 0;
362 
363 	if (de) {
364 		res = fs16_to_cpu(SYSV_SB(dentry->d_sb), de->inode);
365 		dir_put_page(page);
366 	}
367 	return res;
368 }
369