^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * OMFS (as used by RIO Karma) directory operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2005 Bob Copeland <me@bobcopeland.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/buffer_head.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "omfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) static int omfs_hash(const char *name, int namelen, int mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) int i, hash = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) for (i = 0; i < namelen; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) hash ^= tolower(name[i]) << (i % 24);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) return hash % mod;
^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * Finds the bucket for a given name and reads the containing block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * *ofs is set to the offset of the first list entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static struct buffer_head *omfs_get_bucket(struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) const char *name, int namelen, int *ofs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) int nbuckets = (dir->i_size - OMFS_DIR_START)/8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) int bucket = omfs_hash(name, namelen, nbuckets);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) *ofs = OMFS_DIR_START + bucket * 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) return omfs_bread(dir->i_sb, dir->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static struct buffer_head *omfs_scan_list(struct inode *dir, u64 block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) const char *name, int namelen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) u64 *prev_block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct omfs_inode *oi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) int err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) *prev_block = ~0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) while (block != ~0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) bh = omfs_bread(dir->i_sb, block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) if (!bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) oi = (struct omfs_inode *) bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) if (omfs_is_bad(OMFS_SB(dir->i_sb), &oi->i_head, block)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (strncmp(oi->i_name, name, namelen) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) *prev_block = block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) block = be64_to_cpu(oi->i_sibling);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static struct buffer_head *omfs_find_entry(struct inode *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) const char *name, int namelen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) int ofs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) u64 block, dummy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) bh = omfs_get_bucket(dir, name, namelen, &ofs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (!bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) return ERR_PTR(-EIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) block = be64_to_cpu(*((__be64 *) &bh->b_data[ofs]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return omfs_scan_list(dir, block, name, namelen, &dummy);
^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) int omfs_make_empty(struct inode *inode, struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) struct omfs_sb_info *sbi = OMFS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) struct omfs_inode *oi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) bh = omfs_bread(sb, inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (!bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) memset(bh->b_data, 0, sizeof(struct omfs_inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (S_ISDIR(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) memset(&bh->b_data[OMFS_DIR_START], 0xff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) sbi->s_sys_blocksize - OMFS_DIR_START);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) omfs_make_empty_table(bh, OMFS_EXTENT_START);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) oi = (struct omfs_inode *) bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) oi->i_head.h_self = cpu_to_be64(inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) oi->i_sibling = ~cpu_to_be64(0ULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) mark_buffer_dirty(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) static int omfs_add_link(struct dentry *dentry, struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) struct inode *dir = d_inode(dentry->d_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) const char *name = dentry->d_name.name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) int namelen = dentry->d_name.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) struct omfs_inode *oi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) u64 block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) __be64 *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) int ofs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) /* just prepend to head of queue in proper bucket */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) bh = omfs_get_bucket(dir, name, namelen, &ofs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (!bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) entry = (__be64 *) &bh->b_data[ofs];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) block = be64_to_cpu(*entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) *entry = cpu_to_be64(inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) mark_buffer_dirty(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) /* now set the sibling and parent pointers on the new inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) bh = omfs_bread(dir->i_sb, inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (!bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) oi = (struct omfs_inode *) bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) memcpy(oi->i_name, name, namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) memset(oi->i_name + namelen, 0, OMFS_NAMELEN - namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) oi->i_sibling = cpu_to_be64(block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) oi->i_parent = cpu_to_be64(dir->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) mark_buffer_dirty(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) dir->i_ctime = current_time(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) /* mark affected inodes dirty to rebuild checksums */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) mark_inode_dirty(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) static int omfs_delete_entry(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) struct inode *dir = d_inode(dentry->d_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) struct inode *dirty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) const char *name = dentry->d_name.name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) int namelen = dentry->d_name.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) struct omfs_inode *oi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) struct buffer_head *bh, *bh2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) __be64 *entry, next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) u64 block, prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) int ofs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) int err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) /* delete the proper node in the bucket's linked list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) bh = omfs_get_bucket(dir, name, namelen, &ofs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (!bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) entry = (__be64 *) &bh->b_data[ofs];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) block = be64_to_cpu(*entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) bh2 = omfs_scan_list(dir, block, name, namelen, &prev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (IS_ERR(bh2)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) err = PTR_ERR(bh2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) goto out_free_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) oi = (struct omfs_inode *) bh2->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) next = oi->i_sibling;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) brelse(bh2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (prev != ~0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) /* found in middle of list, get list ptr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) bh = omfs_bread(dir->i_sb, prev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) if (!bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) oi = (struct omfs_inode *) bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) entry = &oi->i_sibling;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) *entry = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) mark_buffer_dirty(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) if (prev != ~0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) dirty = omfs_iget(dir->i_sb, prev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) if (!IS_ERR(dirty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) mark_inode_dirty(dirty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) iput(dirty);
^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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) out_free_bh:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) static int omfs_dir_is_empty(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) int nbuckets = (inode->i_size - OMFS_DIR_START) / 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) u64 *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) bh = omfs_bread(inode->i_sb, inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) if (!bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) ptr = (u64 *) &bh->b_data[OMFS_DIR_START];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) for (i = 0; i < nbuckets; i++, ptr++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) if (*ptr != ~0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) return *ptr != ~0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) static int omfs_remove(struct inode *dir, struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) struct inode *inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) if (S_ISDIR(inode->i_mode) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) !omfs_dir_is_empty(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) return -ENOTEMPTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) ret = omfs_delete_entry(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) clear_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) mark_inode_dirty(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) static int omfs_add_node(struct inode *dir, struct dentry *dentry, umode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) struct inode *inode = omfs_new_inode(dir, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) if (IS_ERR(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) return PTR_ERR(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) err = omfs_make_empty(inode, dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) goto out_free_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) err = omfs_add_link(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) goto out_free_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) d_instantiate(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) out_free_inode:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) return err;
^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) static int omfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) return omfs_add_node(dir, dentry, mode | S_IFDIR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) static int omfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) bool excl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) return omfs_add_node(dir, dentry, mode | S_IFREG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) static struct dentry *omfs_lookup(struct inode *dir, struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) struct inode *inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) if (dentry->d_name.len > OMFS_NAMELEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) return ERR_PTR(-ENAMETOOLONG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) bh = omfs_find_entry(dir, dentry->d_name.name, dentry->d_name.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) if (!IS_ERR(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) struct omfs_inode *oi = (struct omfs_inode *)bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) ino_t ino = be64_to_cpu(oi->i_head.h_self);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) inode = omfs_iget(dir->i_sb, ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) } else if (bh != ERR_PTR(-ENOENT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) inode = ERR_CAST(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) return d_splice_alias(inode, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) /* sanity check block's self pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) int omfs_is_bad(struct omfs_sb_info *sbi, struct omfs_header *header,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) u64 fsblock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) int is_bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) u64 ino = be64_to_cpu(header->h_self);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) is_bad = ((ino != fsblock) || (ino < sbi->s_root_ino) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) (ino > sbi->s_num_blocks));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) if (is_bad)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) printk(KERN_WARNING "omfs: bad hash chain detected\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) return is_bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) static bool omfs_fill_chain(struct inode *dir, struct dir_context *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) u64 fsblock, int hindex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) /* follow chain in this bucket */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) while (fsblock != ~0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) struct buffer_head *bh = omfs_bread(dir->i_sb, fsblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) struct omfs_inode *oi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) u64 self;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) unsigned char d_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) if (!bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) oi = (struct omfs_inode *) bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) if (omfs_is_bad(OMFS_SB(dir->i_sb), &oi->i_head, fsblock)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) self = fsblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) fsblock = be64_to_cpu(oi->i_sibling);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) /* skip visited nodes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) if (hindex) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) hindex--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) d_type = (oi->i_type == OMFS_DIR) ? DT_DIR : DT_REG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) if (!dir_emit(ctx, oi->i_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) strnlen(oi->i_name, OMFS_NAMELEN),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) self, d_type)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) ctx->pos++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) static int omfs_rename(struct inode *old_dir, struct dentry *old_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) struct inode *new_dir, struct dentry *new_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) struct inode *new_inode = d_inode(new_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) struct inode *old_inode = d_inode(old_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) if (flags & ~RENAME_NOREPLACE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) if (new_inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) /* overwriting existing file/dir */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) err = omfs_remove(new_dir, new_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) /* since omfs locates files by name, we need to unlink _before_
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) * adding the new link or we won't find the old one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) err = omfs_delete_entry(old_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) mark_inode_dirty(old_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) err = omfs_add_link(new_dentry, old_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) old_inode->i_ctime = current_time(old_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) mark_inode_dirty(old_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) return err;
^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 int omfs_readdir(struct file *file, struct dir_context *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) struct inode *dir = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) __be64 *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) unsigned int hchain, hindex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) int nbuckets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) if (ctx->pos >> 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) if (ctx->pos < 1 << 20) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) if (!dir_emit_dots(file, ctx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) ctx->pos = 1 << 20;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) nbuckets = (dir->i_size - OMFS_DIR_START) / 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) /* high 12 bits store bucket + 1 and low 20 bits store hash index */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) hchain = (ctx->pos >> 20) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) hindex = ctx->pos & 0xfffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) bh = omfs_bread(dir->i_sb, dir->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) if (!bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) p = (__be64 *)(bh->b_data + OMFS_DIR_START) + hchain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) for (; hchain < nbuckets; hchain++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) __u64 fsblock = be64_to_cpu(*p++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) if (!omfs_fill_chain(dir, ctx, fsblock, hindex))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) hindex = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) ctx->pos = (hchain+2) << 20;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) const struct inode_operations omfs_dir_inops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) .lookup = omfs_lookup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) .mkdir = omfs_mkdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) .rename = omfs_rename,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) .create = omfs_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) .unlink = omfs_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) .rmdir = omfs_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) const struct file_operations omfs_dir_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) .read = generic_read_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) .iterate_shared = omfs_readdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) .llseek = generic_file_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) };