^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/hpfs/namei.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * adding & removing files & directories
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "hpfs_fn.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) static void hpfs_update_directory_times(struct inode *dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) time64_t t = local_to_gmt(dir->i_sb, local_get_seconds(dir->i_sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) if (t == dir->i_mtime.tv_sec &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) t == dir->i_ctime.tv_sec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) dir->i_mtime.tv_sec = dir->i_ctime.tv_sec = t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) dir->i_mtime.tv_nsec = dir->i_ctime.tv_nsec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) hpfs_write_inode_nolock(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static int hpfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) const unsigned char *name = dentry->d_name.name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) unsigned len = dentry->d_name.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct quad_buffer_head qbh0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct hpfs_dirent *de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct fnode *fnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct dnode *dnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct inode *result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) fnode_secno fno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) dnode_secno dno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct hpfs_dirent dee;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if ((err = hpfs_chk_name(name, &len))) return err==-ENOENT ? -EINVAL : err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) hpfs_lock(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) err = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) fnode = hpfs_alloc_fnode(dir->i_sb, hpfs_i(dir)->i_dno, &fno, &bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (!fnode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) dnode = hpfs_alloc_dnode(dir->i_sb, fno, &dno, &qbh0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) if (!dnode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) goto bail1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) memset(&dee, 0, sizeof dee);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) dee.directory = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) if (!(mode & 0222)) dee.read_only = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) /*dee.archive = 0;*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) dee.hidden = name[0] == '.';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) dee.fnode = cpu_to_le32(fno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) dee.creation_date = dee.write_date = dee.read_date = cpu_to_le32(local_get_seconds(dir->i_sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) result = new_inode(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (!result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) goto bail2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) hpfs_init_inode(result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) result->i_ino = fno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) hpfs_i(result)->i_parent_dir = dir->i_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) hpfs_i(result)->i_dno = dno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) result->i_ctime.tv_sec = result->i_mtime.tv_sec = result->i_atime.tv_sec = local_to_gmt(dir->i_sb, le32_to_cpu(dee.creation_date));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) result->i_ctime.tv_nsec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) result->i_mtime.tv_nsec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) result->i_atime.tv_nsec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) hpfs_i(result)->i_ea_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) result->i_mode |= S_IFDIR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) result->i_op = &hpfs_dir_iops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) result->i_fop = &hpfs_dir_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) result->i_blocks = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) result->i_size = 2048;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) set_nlink(result, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (dee.read_only)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) result->i_mode &= ~0222;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) r = hpfs_add_dirent(dir, name, len, &dee);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (r == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) goto bail3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (r == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) err = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) goto bail3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) fnode->len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) memcpy(fnode->name, name, len > 15 ? 15 : len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) fnode->up = cpu_to_le32(dir->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) fnode->flags |= FNODE_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) fnode->btree.n_free_nodes = 7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) fnode->btree.n_used_nodes = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) fnode->btree.first_free = cpu_to_le16(0x14);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) fnode->u.external[0].disk_secno = cpu_to_le32(dno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) fnode->u.external[0].file_secno = cpu_to_le32(-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) dnode->root_dnode = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) dnode->up = cpu_to_le32(fno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) de = hpfs_add_de(dir->i_sb, dnode, "\001\001", 2, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) de->creation_date = de->write_date = de->read_date = cpu_to_le32(local_get_seconds(dir->i_sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (!(mode & 0222)) de->read_only = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) de->first = de->directory = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) /*de->hidden = de->system = 0;*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) de->fnode = cpu_to_le32(fno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) mark_buffer_dirty(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) hpfs_mark_4buffers_dirty(&qbh0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) hpfs_brelse4(&qbh0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) inc_nlink(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) insert_inode_hash(result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (!uid_eq(result->i_uid, current_fsuid()) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) !gid_eq(result->i_gid, current_fsgid()) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) result->i_mode != (mode | S_IFDIR)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) result->i_uid = current_fsuid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) result->i_gid = current_fsgid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) result->i_mode = mode | S_IFDIR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) hpfs_write_inode_nolock(result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) hpfs_update_directory_times(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) d_instantiate(dentry, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) hpfs_unlock(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) bail3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) iput(result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) bail2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) hpfs_brelse4(&qbh0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) hpfs_free_dnode(dir->i_sb, dno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) bail1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) hpfs_free_sectors(dir->i_sb, fno, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) bail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) hpfs_unlock(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) static int hpfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, bool excl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) const unsigned char *name = dentry->d_name.name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) unsigned len = dentry->d_name.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) struct inode *result = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) struct fnode *fnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) fnode_secno fno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) struct hpfs_dirent dee;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if ((err = hpfs_chk_name(name, &len)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) return err==-ENOENT ? -EINVAL : err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) hpfs_lock(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) err = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) fnode = hpfs_alloc_fnode(dir->i_sb, hpfs_i(dir)->i_dno, &fno, &bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (!fnode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) memset(&dee, 0, sizeof dee);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) if (!(mode & 0222)) dee.read_only = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) dee.archive = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) dee.hidden = name[0] == '.';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) dee.fnode = cpu_to_le32(fno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) dee.creation_date = dee.write_date = dee.read_date = cpu_to_le32(local_get_seconds(dir->i_sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) result = new_inode(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) if (!result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) goto bail1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) hpfs_init_inode(result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) result->i_ino = fno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) result->i_mode |= S_IFREG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) result->i_mode &= ~0111;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) result->i_op = &hpfs_file_iops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) result->i_fop = &hpfs_file_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) set_nlink(result, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) hpfs_i(result)->i_parent_dir = dir->i_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) result->i_ctime.tv_sec = result->i_mtime.tv_sec = result->i_atime.tv_sec = local_to_gmt(dir->i_sb, le32_to_cpu(dee.creation_date));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) result->i_ctime.tv_nsec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) result->i_mtime.tv_nsec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) result->i_atime.tv_nsec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) hpfs_i(result)->i_ea_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) if (dee.read_only)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) result->i_mode &= ~0222;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) result->i_blocks = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) result->i_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) result->i_data.a_ops = &hpfs_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) hpfs_i(result)->mmu_private = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) r = hpfs_add_dirent(dir, name, len, &dee);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (r == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) goto bail2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if (r == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) err = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) goto bail2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) fnode->len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) memcpy(fnode->name, name, len > 15 ? 15 : len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) fnode->up = cpu_to_le32(dir->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) mark_buffer_dirty(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) insert_inode_hash(result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if (!uid_eq(result->i_uid, current_fsuid()) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) !gid_eq(result->i_gid, current_fsgid()) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) result->i_mode != (mode | S_IFREG)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) result->i_uid = current_fsuid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) result->i_gid = current_fsgid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) result->i_mode = mode | S_IFREG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) hpfs_write_inode_nolock(result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) hpfs_update_directory_times(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) d_instantiate(dentry, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) hpfs_unlock(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) bail2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) iput(result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) bail1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) hpfs_free_sectors(dir->i_sb, fno, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) bail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) hpfs_unlock(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) static int hpfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) const unsigned char *name = dentry->d_name.name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) unsigned len = dentry->d_name.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) struct fnode *fnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) fnode_secno fno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) struct hpfs_dirent dee;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) struct inode *result = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) if ((err = hpfs_chk_name(name, &len))) return err==-ENOENT ? -EINVAL : err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) if (hpfs_sb(dir->i_sb)->sb_eas < 2) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) hpfs_lock(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) err = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) fnode = hpfs_alloc_fnode(dir->i_sb, hpfs_i(dir)->i_dno, &fno, &bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) if (!fnode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) memset(&dee, 0, sizeof dee);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) if (!(mode & 0222)) dee.read_only = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) dee.archive = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) dee.hidden = name[0] == '.';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) dee.fnode = cpu_to_le32(fno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) dee.creation_date = dee.write_date = dee.read_date = cpu_to_le32(local_get_seconds(dir->i_sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) result = new_inode(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) if (!result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) goto bail1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) hpfs_init_inode(result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) result->i_ino = fno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) hpfs_i(result)->i_parent_dir = dir->i_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) result->i_ctime.tv_sec = result->i_mtime.tv_sec = result->i_atime.tv_sec = local_to_gmt(dir->i_sb, le32_to_cpu(dee.creation_date));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) result->i_ctime.tv_nsec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) result->i_mtime.tv_nsec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) result->i_atime.tv_nsec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) hpfs_i(result)->i_ea_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) result->i_uid = current_fsuid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) result->i_gid = current_fsgid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) set_nlink(result, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) result->i_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) result->i_blocks = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) init_special_inode(result, mode, rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) r = hpfs_add_dirent(dir, name, len, &dee);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) if (r == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) goto bail2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) if (r == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) err = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) goto bail2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) fnode->len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) memcpy(fnode->name, name, len > 15 ? 15 : len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) fnode->up = cpu_to_le32(dir->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) mark_buffer_dirty(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) insert_inode_hash(result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) hpfs_write_inode_nolock(result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) hpfs_update_directory_times(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) d_instantiate(dentry, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) hpfs_unlock(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) bail2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) iput(result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) bail1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) hpfs_free_sectors(dir->i_sb, fno, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) bail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) hpfs_unlock(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) static int hpfs_symlink(struct inode *dir, struct dentry *dentry, const char *symlink)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) const unsigned char *name = dentry->d_name.name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) unsigned len = dentry->d_name.len;
^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 fnode *fnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) fnode_secno fno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) struct hpfs_dirent dee;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) struct inode *result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) if ((err = hpfs_chk_name(name, &len))) return err==-ENOENT ? -EINVAL : err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) hpfs_lock(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) if (hpfs_sb(dir->i_sb)->sb_eas < 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) hpfs_unlock(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) err = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) fnode = hpfs_alloc_fnode(dir->i_sb, hpfs_i(dir)->i_dno, &fno, &bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) if (!fnode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) goto bail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) memset(&dee, 0, sizeof dee);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) dee.archive = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) dee.hidden = name[0] == '.';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) dee.fnode = cpu_to_le32(fno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) dee.creation_date = dee.write_date = dee.read_date = cpu_to_le32(local_get_seconds(dir->i_sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) result = new_inode(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) if (!result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) goto bail1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) result->i_ino = fno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) hpfs_init_inode(result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) hpfs_i(result)->i_parent_dir = dir->i_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) result->i_ctime.tv_sec = result->i_mtime.tv_sec = result->i_atime.tv_sec = local_to_gmt(dir->i_sb, le32_to_cpu(dee.creation_date));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) result->i_ctime.tv_nsec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) result->i_mtime.tv_nsec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) result->i_atime.tv_nsec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) hpfs_i(result)->i_ea_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) result->i_mode = S_IFLNK | 0777;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) result->i_uid = current_fsuid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) result->i_gid = current_fsgid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) result->i_blocks = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) set_nlink(result, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) result->i_size = strlen(symlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) inode_nohighmem(result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) result->i_op = &page_symlink_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) result->i_data.a_ops = &hpfs_symlink_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) r = hpfs_add_dirent(dir, name, len, &dee);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) if (r == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) goto bail2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) if (r == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) err = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) goto bail2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) fnode->len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) memcpy(fnode->name, name, len > 15 ? 15 : len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) fnode->up = cpu_to_le32(dir->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) hpfs_set_ea(result, fnode, "SYMLINK", symlink, strlen(symlink));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) mark_buffer_dirty(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) insert_inode_hash(result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) hpfs_write_inode_nolock(result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) hpfs_update_directory_times(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) d_instantiate(dentry, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) hpfs_unlock(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) bail2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) iput(result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) bail1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) hpfs_free_sectors(dir->i_sb, fno, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) bail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) hpfs_unlock(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) static int hpfs_unlink(struct inode *dir, struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) const unsigned char *name = dentry->d_name.name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) unsigned len = dentry->d_name.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) struct quad_buffer_head qbh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) struct hpfs_dirent *de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) struct inode *inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) dnode_secno dno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) hpfs_lock(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) hpfs_adjust_length(name, &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) de = map_dirent(dir, hpfs_i(dir)->i_dno, name, len, &dno, &qbh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) if (!de)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) err = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) if (de->first)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) goto out1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) err = -EISDIR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) if (de->directory)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) goto out1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) r = hpfs_remove_dirent(dir, dno, de, &qbh, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) switch (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) hpfs_error(dir->i_sb, "there was error when removing dirent");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) err = -EFSERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) case 2: /* no space for deleting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) err = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) drop_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) out1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) hpfs_brelse4(&qbh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) hpfs_update_directory_times(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) hpfs_unlock(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) static int hpfs_rmdir(struct inode *dir, struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) const unsigned char *name = dentry->d_name.name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) unsigned len = dentry->d_name.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) struct quad_buffer_head qbh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) struct hpfs_dirent *de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) struct inode *inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) dnode_secno dno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) int n_items = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) hpfs_adjust_length(name, &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) hpfs_lock(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) de = map_dirent(dir, hpfs_i(dir)->i_dno, name, len, &dno, &qbh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) if (!de)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) err = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) if (de->first)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) goto out1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) err = -ENOTDIR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) if (!de->directory)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) goto out1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) hpfs_count_dnodes(dir->i_sb, hpfs_i(inode)->i_dno, NULL, NULL, &n_items);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) err = -ENOTEMPTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) if (n_items)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) goto out1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) r = hpfs_remove_dirent(dir, dno, de, &qbh, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) switch (r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) hpfs_error(dir->i_sb, "there was error when removing dirent");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) err = -EFSERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) case 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) err = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) drop_nlink(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) clear_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) out1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) hpfs_brelse4(&qbh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) hpfs_update_directory_times(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) hpfs_unlock(dir->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) static int hpfs_symlink_readpage(struct file *file, struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) char *link = page_address(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) struct inode *i = page->mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) struct fnode *fnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) hpfs_lock(i->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) if (!(fnode = hpfs_map_fnode(i->i_sb, i->i_ino, &bh)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) err = hpfs_read_ea(i->i_sb, fnode, "SYMLINK", link, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) hpfs_unlock(i->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) SetPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) hpfs_unlock(i->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) SetPageError(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) const struct address_space_operations hpfs_symlink_aops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) .readpage = hpfs_symlink_readpage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) static int hpfs_rename(struct inode *old_dir, struct dentry *old_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) struct inode *new_dir, struct dentry *new_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) const unsigned char *old_name = old_dentry->d_name.name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) unsigned old_len = old_dentry->d_name.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) const unsigned char *new_name = new_dentry->d_name.name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) unsigned new_len = new_dentry->d_name.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) struct inode *i = d_inode(old_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) struct inode *new_inode = d_inode(new_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) struct quad_buffer_head qbh, qbh1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) struct hpfs_dirent *dep, *nde;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) struct hpfs_dirent de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) dnode_secno dno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) struct fnode *fnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) if (flags & ~RENAME_NOREPLACE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) if ((err = hpfs_chk_name(new_name, &new_len))) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) hpfs_adjust_length(old_name, &old_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) hpfs_lock(i->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) /* order doesn't matter, due to VFS exclusion */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) /* Erm? Moving over the empty non-busy directory is perfectly legal */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) if (new_inode && S_ISDIR(new_inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) goto end1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) if (!(dep = map_dirent(old_dir, hpfs_i(old_dir)->i_dno, old_name, old_len, &dno, &qbh))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) hpfs_error(i->i_sb, "lookup succeeded but map dirent failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) goto end1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) copy_de(&de, dep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) de.hidden = new_name[0] == '.';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) if (new_inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) if ((r = hpfs_remove_dirent(old_dir, dno, dep, &qbh, 1)) != 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) if ((nde = map_dirent(new_dir, hpfs_i(new_dir)->i_dno, new_name, new_len, NULL, &qbh1))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) clear_nlink(new_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) copy_de(nde, &de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) memcpy(nde->name, new_name, new_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) hpfs_mark_4buffers_dirty(&qbh1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) hpfs_brelse4(&qbh1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) hpfs_error(new_dir->i_sb, "hpfs_rename: could not find dirent");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) err = -EFSERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) goto end1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) err = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) goto end1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) if (new_dir == old_dir) hpfs_brelse4(&qbh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) if ((r = hpfs_add_dirent(new_dir, new_name, new_len, &de))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) if (r == -1) hpfs_error(new_dir->i_sb, "hpfs_rename: dirent already exists!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) err = r == 1 ? -ENOSPC : -EFSERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) if (new_dir != old_dir) hpfs_brelse4(&qbh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) goto end1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) if (new_dir == old_dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) if (!(dep = map_dirent(old_dir, hpfs_i(old_dir)->i_dno, old_name, old_len, &dno, &qbh))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) hpfs_error(i->i_sb, "lookup succeeded but map dirent failed at #2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) goto end1;
^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) if ((r = hpfs_remove_dirent(old_dir, dno, dep, &qbh, 0))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) hpfs_error(i->i_sb, "hpfs_rename: could not remove dirent");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) err = r == 2 ? -ENOSPC : -EFSERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) goto end1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) hpfs_i(i)->i_parent_dir = new_dir->i_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) if (S_ISDIR(i->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) inc_nlink(new_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) drop_nlink(old_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) if ((fnode = hpfs_map_fnode(i->i_sb, i->i_ino, &bh))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) fnode->up = cpu_to_le32(new_dir->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) fnode->len = new_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) memcpy(fnode->name, new_name, new_len>15?15:new_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) if (new_len < 15) memset(&fnode->name[new_len], 0, 15 - new_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) mark_buffer_dirty(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) end1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) hpfs_update_directory_times(old_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) hpfs_update_directory_times(new_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) hpfs_unlock(i->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) const struct inode_operations hpfs_dir_iops =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) .create = hpfs_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) .lookup = hpfs_lookup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) .unlink = hpfs_unlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) .symlink = hpfs_symlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) .mkdir = hpfs_mkdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) .rmdir = hpfs_rmdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) .mknod = hpfs_mknod,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) .rename = hpfs_rename,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) .setattr = hpfs_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) };