Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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