xref: /linux/fs/ufs/dir.c (revision ba199dc909a20fe62270ae4e93f263987bb9d119)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  linux/fs/ufs/ufs_dir.c
4  *
5  * Copyright (C) 1996
6  * Adrian Rodriguez (adrian@franklins-tower.rutgers.edu)
7  * Laboratory for Computer Science Research Computing Facility
8  * Rutgers, The State University of New Jersey
9  *
10  * swab support by Francois-Rene Rideau <fare@tunes.org> 19970406
11  *
12  * 4.4BSD (FreeBSD) support added on February 1st 1998 by
13  * Niels Kristian Bech Jensen <nkbj@image.dk> partially based
14  * on code by Martin von Loewis <martin@mira.isdn.cs.tu-berlin.de>.
15  *
16  * Migration to usage of "page cache" on May 2006 by
17  * Evgeniy Dushistov <dushistov@mail.ru> based on ext2 code base.
18  */
19 
20 #include <linux/time.h>
21 #include <linux/fs.h>
22 #include <linux/swap.h>
23 #include <linux/iversion.h>
24 
25 #include "ufs_fs.h"
26 #include "ufs.h"
27 #include "swab.h"
28 #include "util.h"
29 
30 /*
31  * NOTE! unlike strncmp, ufs_match returns 1 for success, 0 for failure.
32  *
33  * len <= UFS_MAXNAMLEN and de != NULL are guaranteed by caller.
34  */
35 static inline int ufs_match(struct super_block *sb, int len,
36 		const unsigned char *name, struct ufs_dir_entry *de)
37 {
38 	if (len != ufs_get_de_namlen(sb, de))
39 		return 0;
40 	if (!de->d_ino)
41 		return 0;
42 	return !memcmp(name, de->d_name, len);
43 }
44 
45 static void ufs_commit_chunk(struct page *page, loff_t pos, unsigned len)
46 {
47 	struct address_space *mapping = page->mapping;
48 	struct inode *dir = mapping->host;
49 
50 	inode_inc_iversion(dir);
51 	block_write_end(NULL, mapping, pos, len, len, page, NULL);
52 	if (pos+len > dir->i_size) {
53 		i_size_write(dir, pos+len);
54 		mark_inode_dirty(dir);
55 	}
56 	unlock_page(page);
57 }
58 
59 static int ufs_handle_dirsync(struct inode *dir)
60 {
61 	int err;
62 
63 	err = filemap_write_and_wait(dir->i_mapping);
64 	if (!err)
65 		err = sync_inode_metadata(dir, 1);
66 	return err;
67 }
68 
69 static inline void ufs_put_page(struct page *page)
70 {
71 	kunmap(page);
72 	put_page(page);
73 }
74 
75 ino_t ufs_inode_by_name(struct inode *dir, const struct qstr *qstr)
76 {
77 	ino_t res = 0;
78 	struct ufs_dir_entry *de;
79 	struct page *page;
80 
81 	de = ufs_find_entry(dir, qstr, &page);
82 	if (de) {
83 		res = fs32_to_cpu(dir->i_sb, de->d_ino);
84 		ufs_put_page(page);
85 	}
86 	return res;
87 }
88 
89 
90 /* Releases the page */
91 void ufs_set_link(struct inode *dir, struct ufs_dir_entry *de,
92 		  struct page *page, struct inode *inode,
93 		  bool update_times)
94 {
95 	loff_t pos = page_offset(page) +
96 			(char *) de - (char *) page_address(page);
97 	unsigned len = fs16_to_cpu(dir->i_sb, de->d_reclen);
98 	int err;
99 
100 	lock_page(page);
101 	err = ufs_prepare_chunk(page, pos, len);
102 	BUG_ON(err);
103 
104 	de->d_ino = cpu_to_fs32(dir->i_sb, inode->i_ino);
105 	ufs_set_de_type(dir->i_sb, de, inode->i_mode);
106 
107 	ufs_commit_chunk(page, pos, len);
108 	ufs_put_page(page);
109 	if (update_times)
110 		inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
111 	mark_inode_dirty(dir);
112 	ufs_handle_dirsync(dir);
113 }
114 
115 
116 static bool ufs_check_page(struct page *page)
117 {
118 	struct inode *dir = page->mapping->host;
119 	struct super_block *sb = dir->i_sb;
120 	char *kaddr = page_address(page);
121 	unsigned offs, rec_len;
122 	unsigned limit = PAGE_SIZE;
123 	const unsigned chunk_mask = UFS_SB(sb)->s_uspi->s_dirblksize - 1;
124 	struct ufs_dir_entry *p;
125 	char *error;
126 
127 	if ((dir->i_size >> PAGE_SHIFT) == page->index) {
128 		limit = dir->i_size & ~PAGE_MASK;
129 		if (limit & chunk_mask)
130 			goto Ebadsize;
131 		if (!limit)
132 			goto out;
133 	}
134 	for (offs = 0; offs <= limit - UFS_DIR_REC_LEN(1); offs += rec_len) {
135 		p = (struct ufs_dir_entry *)(kaddr + offs);
136 		rec_len = fs16_to_cpu(sb, p->d_reclen);
137 
138 		if (rec_len < UFS_DIR_REC_LEN(1))
139 			goto Eshort;
140 		if (rec_len & 3)
141 			goto Ealign;
142 		if (rec_len < UFS_DIR_REC_LEN(ufs_get_de_namlen(sb, p)))
143 			goto Enamelen;
144 		if (((offs + rec_len - 1) ^ offs) & ~chunk_mask)
145 			goto Espan;
146 		if (fs32_to_cpu(sb, p->d_ino) > (UFS_SB(sb)->s_uspi->s_ipg *
147 						  UFS_SB(sb)->s_uspi->s_ncg))
148 			goto Einumber;
149 	}
150 	if (offs != limit)
151 		goto Eend;
152 out:
153 	SetPageChecked(page);
154 	return true;
155 
156 	/* Too bad, we had an error */
157 
158 Ebadsize:
159 	ufs_error(sb, "ufs_check_page",
160 		  "size of directory #%lu is not a multiple of chunk size",
161 		  dir->i_ino
162 	);
163 	goto fail;
164 Eshort:
165 	error = "rec_len is smaller than minimal";
166 	goto bad_entry;
167 Ealign:
168 	error = "unaligned directory entry";
169 	goto bad_entry;
170 Enamelen:
171 	error = "rec_len is too small for name_len";
172 	goto bad_entry;
173 Espan:
174 	error = "directory entry across blocks";
175 	goto bad_entry;
176 Einumber:
177 	error = "inode out of bounds";
178 bad_entry:
179 	ufs_error (sb, "ufs_check_page", "bad entry in directory #%lu: %s - "
180 		   "offset=%lu, rec_len=%d, name_len=%d",
181 		   dir->i_ino, error, (page->index<<PAGE_SHIFT)+offs,
182 		   rec_len, ufs_get_de_namlen(sb, p));
183 	goto fail;
184 Eend:
185 	p = (struct ufs_dir_entry *)(kaddr + offs);
186 	ufs_error(sb, __func__,
187 		   "entry in directory #%lu spans the page boundary"
188 		   "offset=%lu",
189 		   dir->i_ino, (page->index<<PAGE_SHIFT)+offs);
190 fail:
191 	return false;
192 }
193 
194 static struct page *ufs_get_page(struct inode *dir, unsigned long n)
195 {
196 	struct address_space *mapping = dir->i_mapping;
197 	struct page *page = read_mapping_page(mapping, n, NULL);
198 	if (!IS_ERR(page)) {
199 		kmap(page);
200 		if (unlikely(!PageChecked(page))) {
201 			if (!ufs_check_page(page))
202 				goto fail;
203 		}
204 	}
205 	return page;
206 
207 fail:
208 	ufs_put_page(page);
209 	return ERR_PTR(-EIO);
210 }
211 
212 /*
213  * Return the offset into page `page_nr' of the last valid
214  * byte in that page, plus one.
215  */
216 static unsigned
217 ufs_last_byte(struct inode *inode, unsigned long page_nr)
218 {
219 	unsigned last_byte = inode->i_size;
220 
221 	last_byte -= page_nr << PAGE_SHIFT;
222 	if (last_byte > PAGE_SIZE)
223 		last_byte = PAGE_SIZE;
224 	return last_byte;
225 }
226 
227 static inline struct ufs_dir_entry *
228 ufs_next_entry(struct super_block *sb, struct ufs_dir_entry *p)
229 {
230 	return (struct ufs_dir_entry *)((char *)p +
231 					fs16_to_cpu(sb, p->d_reclen));
232 }
233 
234 struct ufs_dir_entry *ufs_dotdot(struct inode *dir, struct page **p)
235 {
236 	struct page *page = ufs_get_page(dir, 0);
237 	struct ufs_dir_entry *de = NULL;
238 
239 	if (!IS_ERR(page)) {
240 		de = ufs_next_entry(dir->i_sb,
241 				    (struct ufs_dir_entry *)page_address(page));
242 		*p = page;
243 	}
244 	return de;
245 }
246 
247 /*
248  *	ufs_find_entry()
249  *
250  * finds an entry in the specified directory with the wanted name. It
251  * returns the page in which the entry was found, and the entry itself
252  * (as a parameter - res_dir). Page is returned mapped and unlocked.
253  * Entry is guaranteed to be valid.
254  */
255 struct ufs_dir_entry *ufs_find_entry(struct inode *dir, const struct qstr *qstr,
256 				     struct page **res_page)
257 {
258 	struct super_block *sb = dir->i_sb;
259 	const unsigned char *name = qstr->name;
260 	int namelen = qstr->len;
261 	unsigned reclen = UFS_DIR_REC_LEN(namelen);
262 	unsigned long start, n;
263 	unsigned long npages = dir_pages(dir);
264 	struct page *page = NULL;
265 	struct ufs_inode_info *ui = UFS_I(dir);
266 	struct ufs_dir_entry *de;
267 
268 	UFSD("ENTER, dir_ino %lu, name %s, namlen %u\n", dir->i_ino, name, namelen);
269 
270 	if (npages == 0 || namelen > UFS_MAXNAMLEN)
271 		goto out;
272 
273 	/* OFFSET_CACHE */
274 	*res_page = NULL;
275 
276 	start = ui->i_dir_start_lookup;
277 
278 	if (start >= npages)
279 		start = 0;
280 	n = start;
281 	do {
282 		char *kaddr;
283 		page = ufs_get_page(dir, n);
284 		if (!IS_ERR(page)) {
285 			kaddr = page_address(page);
286 			de = (struct ufs_dir_entry *) kaddr;
287 			kaddr += ufs_last_byte(dir, n) - reclen;
288 			while ((char *) de <= kaddr) {
289 				if (ufs_match(sb, namelen, name, de))
290 					goto found;
291 				de = ufs_next_entry(sb, de);
292 			}
293 			ufs_put_page(page);
294 		}
295 		if (++n >= npages)
296 			n = 0;
297 	} while (n != start);
298 out:
299 	return NULL;
300 
301 found:
302 	*res_page = page;
303 	ui->i_dir_start_lookup = n;
304 	return de;
305 }
306 
307 /*
308  *	Parent is locked.
309  */
310 int ufs_add_link(struct dentry *dentry, struct inode *inode)
311 {
312 	struct inode *dir = d_inode(dentry->d_parent);
313 	const unsigned char *name = dentry->d_name.name;
314 	int namelen = dentry->d_name.len;
315 	struct super_block *sb = dir->i_sb;
316 	unsigned reclen = UFS_DIR_REC_LEN(namelen);
317 	const unsigned int chunk_size = UFS_SB(sb)->s_uspi->s_dirblksize;
318 	unsigned short rec_len, name_len;
319 	struct page *page = NULL;
320 	struct ufs_dir_entry *de;
321 	unsigned long npages = dir_pages(dir);
322 	unsigned long n;
323 	char *kaddr;
324 	loff_t pos;
325 	int err;
326 
327 	UFSD("ENTER, name %s, namelen %u\n", name, namelen);
328 
329 	/*
330 	 * We take care of directory expansion in the same loop.
331 	 * This code plays outside i_size, so it locks the page
332 	 * to protect that region.
333 	 */
334 	for (n = 0; n <= npages; n++) {
335 		char *dir_end;
336 
337 		page = ufs_get_page(dir, n);
338 		err = PTR_ERR(page);
339 		if (IS_ERR(page))
340 			goto out;
341 		lock_page(page);
342 		kaddr = page_address(page);
343 		dir_end = kaddr + ufs_last_byte(dir, n);
344 		de = (struct ufs_dir_entry *)kaddr;
345 		kaddr += PAGE_SIZE - reclen;
346 		while ((char *)de <= kaddr) {
347 			if ((char *)de == dir_end) {
348 				/* We hit i_size */
349 				name_len = 0;
350 				rec_len = chunk_size;
351 				de->d_reclen = cpu_to_fs16(sb, chunk_size);
352 				de->d_ino = 0;
353 				goto got_it;
354 			}
355 			if (de->d_reclen == 0) {
356 				ufs_error(dir->i_sb, __func__,
357 					  "zero-length directory entry");
358 				err = -EIO;
359 				goto out_unlock;
360 			}
361 			err = -EEXIST;
362 			if (ufs_match(sb, namelen, name, de))
363 				goto out_unlock;
364 			name_len = UFS_DIR_REC_LEN(ufs_get_de_namlen(sb, de));
365 			rec_len = fs16_to_cpu(sb, de->d_reclen);
366 			if (!de->d_ino && rec_len >= reclen)
367 				goto got_it;
368 			if (rec_len >= name_len + reclen)
369 				goto got_it;
370 			de = (struct ufs_dir_entry *) ((char *) de + rec_len);
371 		}
372 		unlock_page(page);
373 		ufs_put_page(page);
374 	}
375 	BUG();
376 	return -EINVAL;
377 
378 got_it:
379 	pos = page_offset(page) +
380 			(char*)de - (char*)page_address(page);
381 	err = ufs_prepare_chunk(page, pos, rec_len);
382 	if (err)
383 		goto out_unlock;
384 	if (de->d_ino) {
385 		struct ufs_dir_entry *de1 =
386 			(struct ufs_dir_entry *) ((char *) de + name_len);
387 		de1->d_reclen = cpu_to_fs16(sb, rec_len - name_len);
388 		de->d_reclen = cpu_to_fs16(sb, name_len);
389 
390 		de = de1;
391 	}
392 
393 	ufs_set_de_namlen(sb, de, namelen);
394 	memcpy(de->d_name, name, namelen + 1);
395 	de->d_ino = cpu_to_fs32(sb, inode->i_ino);
396 	ufs_set_de_type(sb, de, inode->i_mode);
397 
398 	ufs_commit_chunk(page, pos, rec_len);
399 	inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
400 
401 	mark_inode_dirty(dir);
402 	err = ufs_handle_dirsync(dir);
403 	/* OFFSET_CACHE */
404 out_put:
405 	ufs_put_page(page);
406 out:
407 	return err;
408 out_unlock:
409 	unlock_page(page);
410 	goto out_put;
411 }
412 
413 static inline unsigned
414 ufs_validate_entry(struct super_block *sb, char *base,
415 		   unsigned offset, unsigned mask)
416 {
417 	struct ufs_dir_entry *de = (struct ufs_dir_entry*)(base + offset);
418 	struct ufs_dir_entry *p = (struct ufs_dir_entry*)(base + (offset&mask));
419 	while ((char*)p < (char*)de)
420 		p = ufs_next_entry(sb, p);
421 	return (char *)p - base;
422 }
423 
424 
425 /*
426  * This is blatantly stolen from ext2fs
427  */
428 static int
429 ufs_readdir(struct file *file, struct dir_context *ctx)
430 {
431 	loff_t pos = ctx->pos;
432 	struct inode *inode = file_inode(file);
433 	struct super_block *sb = inode->i_sb;
434 	unsigned int offset = pos & ~PAGE_MASK;
435 	unsigned long n = pos >> PAGE_SHIFT;
436 	unsigned long npages = dir_pages(inode);
437 	unsigned chunk_mask = ~(UFS_SB(sb)->s_uspi->s_dirblksize - 1);
438 	bool need_revalidate = !inode_eq_iversion(inode, file->f_version);
439 	unsigned flags = UFS_SB(sb)->s_flags;
440 
441 	UFSD("BEGIN\n");
442 
443 	if (pos > inode->i_size - UFS_DIR_REC_LEN(1))
444 		return 0;
445 
446 	for ( ; n < npages; n++, offset = 0) {
447 		char *kaddr, *limit;
448 		struct ufs_dir_entry *de;
449 
450 		struct page *page = ufs_get_page(inode, n);
451 
452 		if (IS_ERR(page)) {
453 			ufs_error(sb, __func__,
454 				  "bad page in #%lu",
455 				  inode->i_ino);
456 			ctx->pos += PAGE_SIZE - offset;
457 			return -EIO;
458 		}
459 		kaddr = page_address(page);
460 		if (unlikely(need_revalidate)) {
461 			if (offset) {
462 				offset = ufs_validate_entry(sb, kaddr, offset, chunk_mask);
463 				ctx->pos = (n<<PAGE_SHIFT) + offset;
464 			}
465 			file->f_version = inode_query_iversion(inode);
466 			need_revalidate = false;
467 		}
468 		de = (struct ufs_dir_entry *)(kaddr+offset);
469 		limit = kaddr + ufs_last_byte(inode, n) - UFS_DIR_REC_LEN(1);
470 		for ( ;(char*)de <= limit; de = ufs_next_entry(sb, de)) {
471 			if (de->d_ino) {
472 				unsigned char d_type = DT_UNKNOWN;
473 
474 				UFSD("filldir(%s,%u)\n", de->d_name,
475 				      fs32_to_cpu(sb, de->d_ino));
476 				UFSD("namlen %u\n", ufs_get_de_namlen(sb, de));
477 
478 				if ((flags & UFS_DE_MASK) == UFS_DE_44BSD)
479 					d_type = de->d_u.d_44.d_type;
480 
481 				if (!dir_emit(ctx, de->d_name,
482 					       ufs_get_de_namlen(sb, de),
483 					       fs32_to_cpu(sb, de->d_ino),
484 					       d_type)) {
485 					ufs_put_page(page);
486 					return 0;
487 				}
488 			}
489 			ctx->pos += fs16_to_cpu(sb, de->d_reclen);
490 		}
491 		ufs_put_page(page);
492 	}
493 	return 0;
494 }
495 
496 
497 /*
498  * ufs_delete_entry deletes a directory entry by merging it with the
499  * previous entry.
500  */
501 int ufs_delete_entry(struct inode *inode, struct ufs_dir_entry *dir,
502 		     struct page * page)
503 {
504 	struct super_block *sb = inode->i_sb;
505 	char *kaddr = page_address(page);
506 	unsigned from = ((char*)dir - kaddr) & ~(UFS_SB(sb)->s_uspi->s_dirblksize - 1);
507 	unsigned to = ((char*)dir - kaddr) + fs16_to_cpu(sb, dir->d_reclen);
508 	loff_t pos;
509 	struct ufs_dir_entry *pde = NULL;
510 	struct ufs_dir_entry *de = (struct ufs_dir_entry *) (kaddr + from);
511 	int err;
512 
513 	UFSD("ENTER\n");
514 
515 	UFSD("ino %u, reclen %u, namlen %u, name %s\n",
516 	      fs32_to_cpu(sb, de->d_ino),
517 	      fs16_to_cpu(sb, de->d_reclen),
518 	      ufs_get_de_namlen(sb, de), de->d_name);
519 
520 	while ((char*)de < (char*)dir) {
521 		if (de->d_reclen == 0) {
522 			ufs_error(inode->i_sb, __func__,
523 				  "zero-length directory entry");
524 			err = -EIO;
525 			goto out;
526 		}
527 		pde = de;
528 		de = ufs_next_entry(sb, de);
529 	}
530 	if (pde)
531 		from = (char*)pde - (char*)page_address(page);
532 
533 	pos = page_offset(page) + from;
534 	lock_page(page);
535 	err = ufs_prepare_chunk(page, pos, to - from);
536 	BUG_ON(err);
537 	if (pde)
538 		pde->d_reclen = cpu_to_fs16(sb, to - from);
539 	dir->d_ino = 0;
540 	ufs_commit_chunk(page, pos, to - from);
541 	inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
542 	mark_inode_dirty(inode);
543 	err = ufs_handle_dirsync(inode);
544 out:
545 	ufs_put_page(page);
546 	UFSD("EXIT\n");
547 	return err;
548 }
549 
550 int ufs_make_empty(struct inode * inode, struct inode *dir)
551 {
552 	struct super_block * sb = dir->i_sb;
553 	struct address_space *mapping = inode->i_mapping;
554 	struct page *page = grab_cache_page(mapping, 0);
555 	const unsigned int chunk_size = UFS_SB(sb)->s_uspi->s_dirblksize;
556 	struct ufs_dir_entry * de;
557 	char *base;
558 	int err;
559 
560 	if (!page)
561 		return -ENOMEM;
562 
563 	err = ufs_prepare_chunk(page, 0, chunk_size);
564 	if (err) {
565 		unlock_page(page);
566 		goto fail;
567 	}
568 
569 	kmap(page);
570 	base = (char*)page_address(page);
571 	memset(base, 0, PAGE_SIZE);
572 
573 	de = (struct ufs_dir_entry *) base;
574 
575 	de->d_ino = cpu_to_fs32(sb, inode->i_ino);
576 	ufs_set_de_type(sb, de, inode->i_mode);
577 	ufs_set_de_namlen(sb, de, 1);
578 	de->d_reclen = cpu_to_fs16(sb, UFS_DIR_REC_LEN(1));
579 	strcpy (de->d_name, ".");
580 	de = (struct ufs_dir_entry *)
581 		((char *)de + fs16_to_cpu(sb, de->d_reclen));
582 	de->d_ino = cpu_to_fs32(sb, dir->i_ino);
583 	ufs_set_de_type(sb, de, dir->i_mode);
584 	de->d_reclen = cpu_to_fs16(sb, chunk_size - UFS_DIR_REC_LEN(1));
585 	ufs_set_de_namlen(sb, de, 2);
586 	strcpy (de->d_name, "..");
587 	kunmap(page);
588 
589 	ufs_commit_chunk(page, 0, chunk_size);
590 	err = ufs_handle_dirsync(inode);
591 fail:
592 	put_page(page);
593 	return err;
594 }
595 
596 /*
597  * routine to check that the specified directory is empty (for rmdir)
598  */
599 int ufs_empty_dir(struct inode * inode)
600 {
601 	struct super_block *sb = inode->i_sb;
602 	struct page *page = NULL;
603 	unsigned long i, npages = dir_pages(inode);
604 
605 	for (i = 0; i < npages; i++) {
606 		char *kaddr;
607 		struct ufs_dir_entry *de;
608 		page = ufs_get_page(inode, i);
609 
610 		if (IS_ERR(page))
611 			continue;
612 
613 		kaddr = page_address(page);
614 		de = (struct ufs_dir_entry *)kaddr;
615 		kaddr += ufs_last_byte(inode, i) - UFS_DIR_REC_LEN(1);
616 
617 		while ((char *)de <= kaddr) {
618 			if (de->d_reclen == 0) {
619 				ufs_error(inode->i_sb, __func__,
620 					"zero-length directory entry: "
621 					"kaddr=%p, de=%p\n", kaddr, de);
622 				goto not_empty;
623 			}
624 			if (de->d_ino) {
625 				u16 namelen=ufs_get_de_namlen(sb, de);
626 				/* check for . and .. */
627 				if (de->d_name[0] != '.')
628 					goto not_empty;
629 				if (namelen > 2)
630 					goto not_empty;
631 				if (namelen < 2) {
632 					if (inode->i_ino !=
633 					    fs32_to_cpu(sb, de->d_ino))
634 						goto not_empty;
635 				} else if (de->d_name[1] != '.')
636 					goto not_empty;
637 			}
638 			de = ufs_next_entry(sb, de);
639 		}
640 		ufs_put_page(page);
641 	}
642 	return 1;
643 
644 not_empty:
645 	ufs_put_page(page);
646 	return 0;
647 }
648 
649 const struct file_operations ufs_dir_operations = {
650 	.read		= generic_read_dir,
651 	.iterate_shared	= ufs_readdir,
652 	.fsync		= generic_file_fsync,
653 	.llseek		= generic_file_llseek,
654 };
655