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