Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  *  linux/fs/hpfs/inode.c
0004  *
0005  *  Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
0006  *
0007  *  inode VFS functions
0008  */
0009 
0010 #include <linux/slab.h>
0011 #include <linux/user_namespace.h>
0012 #include "hpfs_fn.h"
0013 
0014 void hpfs_init_inode(struct inode *i)
0015 {
0016     struct super_block *sb = i->i_sb;
0017     struct hpfs_inode_info *hpfs_inode = hpfs_i(i);
0018 
0019     i->i_uid = hpfs_sb(sb)->sb_uid;
0020     i->i_gid = hpfs_sb(sb)->sb_gid;
0021     i->i_mode = hpfs_sb(sb)->sb_mode;
0022     i->i_size = -1;
0023     i->i_blocks = -1;
0024     
0025     hpfs_inode->i_dno = 0;
0026     hpfs_inode->i_n_secs = 0;
0027     hpfs_inode->i_file_sec = 0;
0028     hpfs_inode->i_disk_sec = 0;
0029     hpfs_inode->i_dpos = 0;
0030     hpfs_inode->i_dsubdno = 0;
0031     hpfs_inode->i_ea_mode = 0;
0032     hpfs_inode->i_ea_uid = 0;
0033     hpfs_inode->i_ea_gid = 0;
0034     hpfs_inode->i_ea_size = 0;
0035 
0036     hpfs_inode->i_rddir_off = NULL;
0037     hpfs_inode->i_dirty = 0;
0038 
0039     i->i_ctime.tv_sec = i->i_ctime.tv_nsec = 0;
0040     i->i_mtime.tv_sec = i->i_mtime.tv_nsec = 0;
0041     i->i_atime.tv_sec = i->i_atime.tv_nsec = 0;
0042 }
0043 
0044 void hpfs_read_inode(struct inode *i)
0045 {
0046     struct buffer_head *bh;
0047     struct fnode *fnode;
0048     struct super_block *sb = i->i_sb;
0049     struct hpfs_inode_info *hpfs_inode = hpfs_i(i);
0050     void *ea;
0051     int ea_size;
0052 
0053     if (!(fnode = hpfs_map_fnode(sb, i->i_ino, &bh))) {
0054         /*i->i_mode |= S_IFREG;
0055         i->i_mode &= ~0111;
0056         i->i_op = &hpfs_file_iops;
0057         i->i_fop = &hpfs_file_ops;
0058         clear_nlink(i);*/
0059         make_bad_inode(i);
0060         return;
0061     }
0062     if (hpfs_sb(i->i_sb)->sb_eas) {
0063         if ((ea = hpfs_get_ea(i->i_sb, fnode, "UID", &ea_size))) {
0064             if (ea_size == 2) {
0065                 i_uid_write(i, le16_to_cpu(*(__le16*)ea));
0066                 hpfs_inode->i_ea_uid = 1;
0067             }
0068             kfree(ea);
0069         }
0070         if ((ea = hpfs_get_ea(i->i_sb, fnode, "GID", &ea_size))) {
0071             if (ea_size == 2) {
0072                 i_gid_write(i, le16_to_cpu(*(__le16*)ea));
0073                 hpfs_inode->i_ea_gid = 1;
0074             }
0075             kfree(ea);
0076         }
0077         if ((ea = hpfs_get_ea(i->i_sb, fnode, "SYMLINK", &ea_size))) {
0078             kfree(ea);
0079             i->i_mode = S_IFLNK | 0777;
0080             i->i_op = &page_symlink_inode_operations;
0081             inode_nohighmem(i);
0082             i->i_data.a_ops = &hpfs_symlink_aops;
0083             set_nlink(i, 1);
0084             i->i_size = ea_size;
0085             i->i_blocks = 1;
0086             brelse(bh);
0087             return;
0088         }
0089         if ((ea = hpfs_get_ea(i->i_sb, fnode, "MODE", &ea_size))) {
0090             int rdev = 0;
0091             umode_t mode = hpfs_sb(sb)->sb_mode;
0092             if (ea_size == 2) {
0093                 mode = le16_to_cpu(*(__le16*)ea);
0094                 hpfs_inode->i_ea_mode = 1;
0095             }
0096             kfree(ea);
0097             i->i_mode = mode;
0098             if (S_ISBLK(mode) || S_ISCHR(mode)) {
0099                 if ((ea = hpfs_get_ea(i->i_sb, fnode, "DEV", &ea_size))) {
0100                     if (ea_size == 4)
0101                         rdev = le32_to_cpu(*(__le32*)ea);
0102                     kfree(ea);
0103                 }
0104             }
0105             if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode)) {
0106                 brelse(bh);
0107                 set_nlink(i, 1);
0108                 i->i_size = 0;
0109                 i->i_blocks = 1;
0110                 init_special_inode(i, mode,
0111                     new_decode_dev(rdev));
0112                 return;
0113             }
0114         }
0115     }
0116     if (fnode_is_dir(fnode)) {
0117         int n_dnodes, n_subdirs;
0118         i->i_mode |= S_IFDIR;
0119         i->i_op = &hpfs_dir_iops;
0120         i->i_fop = &hpfs_dir_ops;
0121         hpfs_inode->i_parent_dir = le32_to_cpu(fnode->up);
0122         hpfs_inode->i_dno = le32_to_cpu(fnode->u.external[0].disk_secno);
0123         if (hpfs_sb(sb)->sb_chk >= 2) {
0124             struct buffer_head *bh0;
0125             if (hpfs_map_fnode(sb, hpfs_inode->i_parent_dir, &bh0)) brelse(bh0);
0126         }
0127         n_dnodes = 0; n_subdirs = 0;
0128         hpfs_count_dnodes(i->i_sb, hpfs_inode->i_dno, &n_dnodes, &n_subdirs, NULL);
0129         i->i_blocks = 4 * n_dnodes;
0130         i->i_size = 2048 * n_dnodes;
0131         set_nlink(i, 2 + n_subdirs);
0132     } else {
0133         i->i_mode |= S_IFREG;
0134         if (!hpfs_inode->i_ea_mode) i->i_mode &= ~0111;
0135         i->i_op = &hpfs_file_iops;
0136         i->i_fop = &hpfs_file_ops;
0137         set_nlink(i, 1);
0138         i->i_size = le32_to_cpu(fnode->file_size);
0139         i->i_blocks = ((i->i_size + 511) >> 9) + 1;
0140         i->i_data.a_ops = &hpfs_aops;
0141         hpfs_i(i)->mmu_private = i->i_size;
0142     }
0143     brelse(bh);
0144 }
0145 
0146 static void hpfs_write_inode_ea(struct inode *i, struct fnode *fnode)
0147 {
0148     struct hpfs_inode_info *hpfs_inode = hpfs_i(i);
0149     /*if (le32_to_cpu(fnode->acl_size_l) || le16_to_cpu(fnode->acl_size_s)) {
0150            Some unknown structures like ACL may be in fnode,
0151            we'd better not overwrite them
0152         hpfs_error(i->i_sb, "fnode %08x has some unknown HPFS386 structures", i->i_ino);
0153     } else*/ if (hpfs_sb(i->i_sb)->sb_eas >= 2) {
0154         __le32 ea;
0155         if (!uid_eq(i->i_uid, hpfs_sb(i->i_sb)->sb_uid) || hpfs_inode->i_ea_uid) {
0156             ea = cpu_to_le32(i_uid_read(i));
0157             hpfs_set_ea(i, fnode, "UID", (char*)&ea, 2);
0158             hpfs_inode->i_ea_uid = 1;
0159         }
0160         if (!gid_eq(i->i_gid, hpfs_sb(i->i_sb)->sb_gid) || hpfs_inode->i_ea_gid) {
0161             ea = cpu_to_le32(i_gid_read(i));
0162             hpfs_set_ea(i, fnode, "GID", (char *)&ea, 2);
0163             hpfs_inode->i_ea_gid = 1;
0164         }
0165         if (!S_ISLNK(i->i_mode))
0166             if ((i->i_mode != ((hpfs_sb(i->i_sb)->sb_mode & ~(S_ISDIR(i->i_mode) ? 0 : 0111))
0167               | (S_ISDIR(i->i_mode) ? S_IFDIR : S_IFREG))
0168               && i->i_mode != ((hpfs_sb(i->i_sb)->sb_mode & ~(S_ISDIR(i->i_mode) ? 0222 : 0333))
0169               | (S_ISDIR(i->i_mode) ? S_IFDIR : S_IFREG))) || hpfs_inode->i_ea_mode) {
0170                 ea = cpu_to_le32(i->i_mode);
0171                 /* sick, but legal */
0172                 hpfs_set_ea(i, fnode, "MODE", (char *)&ea, 2);
0173                 hpfs_inode->i_ea_mode = 1;
0174             }
0175         if (S_ISBLK(i->i_mode) || S_ISCHR(i->i_mode)) {
0176             ea = cpu_to_le32(new_encode_dev(i->i_rdev));
0177             hpfs_set_ea(i, fnode, "DEV", (char *)&ea, 4);
0178         }
0179     }
0180 }
0181 
0182 void hpfs_write_inode(struct inode *i)
0183 {
0184     struct hpfs_inode_info *hpfs_inode = hpfs_i(i);
0185     struct inode *parent;
0186     if (i->i_ino == hpfs_sb(i->i_sb)->sb_root) return;
0187     if (hpfs_inode->i_rddir_off && !atomic_read(&i->i_count)) {
0188         if (*hpfs_inode->i_rddir_off)
0189             pr_err("write_inode: some position still there\n");
0190         kfree(hpfs_inode->i_rddir_off);
0191         hpfs_inode->i_rddir_off = NULL;
0192     }
0193     if (!i->i_nlink) {
0194         return;
0195     }
0196     parent = iget_locked(i->i_sb, hpfs_inode->i_parent_dir);
0197     if (parent) {
0198         hpfs_inode->i_dirty = 0;
0199         if (parent->i_state & I_NEW) {
0200             hpfs_init_inode(parent);
0201             hpfs_read_inode(parent);
0202             unlock_new_inode(parent);
0203         }
0204         hpfs_write_inode_nolock(i);
0205         iput(parent);
0206     }
0207 }
0208 
0209 void hpfs_write_inode_nolock(struct inode *i)
0210 {
0211     struct hpfs_inode_info *hpfs_inode = hpfs_i(i);
0212     struct buffer_head *bh;
0213     struct fnode *fnode;
0214     struct quad_buffer_head qbh;
0215     struct hpfs_dirent *de;
0216     if (i->i_ino == hpfs_sb(i->i_sb)->sb_root) return;
0217     if (!(fnode = hpfs_map_fnode(i->i_sb, i->i_ino, &bh))) return;
0218     if (i->i_ino != hpfs_sb(i->i_sb)->sb_root && i->i_nlink) {
0219         if (!(de = map_fnode_dirent(i->i_sb, i->i_ino, fnode, &qbh))) {
0220             brelse(bh);
0221             return;
0222         }
0223     } else de = NULL;
0224     if (S_ISREG(i->i_mode)) {
0225         fnode->file_size = cpu_to_le32(i->i_size);
0226         if (de) de->file_size = cpu_to_le32(i->i_size);
0227     } else if (S_ISDIR(i->i_mode)) {
0228         fnode->file_size = cpu_to_le32(0);
0229         if (de) de->file_size = cpu_to_le32(0);
0230     }
0231     hpfs_write_inode_ea(i, fnode);
0232     if (de) {
0233         de->write_date = cpu_to_le32(gmt_to_local(i->i_sb, i->i_mtime.tv_sec));
0234         de->read_date = cpu_to_le32(gmt_to_local(i->i_sb, i->i_atime.tv_sec));
0235         de->creation_date = cpu_to_le32(gmt_to_local(i->i_sb, i->i_ctime.tv_sec));
0236         de->read_only = !(i->i_mode & 0222);
0237         de->ea_size = cpu_to_le32(hpfs_inode->i_ea_size);
0238         hpfs_mark_4buffers_dirty(&qbh);
0239         hpfs_brelse4(&qbh);
0240     }
0241     if (S_ISDIR(i->i_mode)) {
0242         if ((de = map_dirent(i, hpfs_inode->i_dno, "\001\001", 2, NULL, &qbh))) {
0243             de->write_date = cpu_to_le32(gmt_to_local(i->i_sb, i->i_mtime.tv_sec));
0244             de->read_date = cpu_to_le32(gmt_to_local(i->i_sb, i->i_atime.tv_sec));
0245             de->creation_date = cpu_to_le32(gmt_to_local(i->i_sb, i->i_ctime.tv_sec));
0246             de->read_only = !(i->i_mode & 0222);
0247             de->ea_size = cpu_to_le32(/*hpfs_inode->i_ea_size*/0);
0248             de->file_size = cpu_to_le32(0);
0249             hpfs_mark_4buffers_dirty(&qbh);
0250             hpfs_brelse4(&qbh);
0251         } else
0252             hpfs_error(i->i_sb,
0253                 "directory %08lx doesn't have '.' entry",
0254                 (unsigned long)i->i_ino);
0255     }
0256     mark_buffer_dirty(bh);
0257     brelse(bh);
0258 }
0259 
0260 int hpfs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
0261          struct iattr *attr)
0262 {
0263     struct inode *inode = d_inode(dentry);
0264     int error = -EINVAL;
0265 
0266     hpfs_lock(inode->i_sb);
0267     if (inode->i_ino == hpfs_sb(inode->i_sb)->sb_root)
0268         goto out_unlock;
0269     if ((attr->ia_valid & ATTR_UID) &&
0270         from_kuid(&init_user_ns, attr->ia_uid) >= 0x10000)
0271         goto out_unlock;
0272     if ((attr->ia_valid & ATTR_GID) &&
0273         from_kgid(&init_user_ns, attr->ia_gid) >= 0x10000)
0274         goto out_unlock;
0275     if ((attr->ia_valid & ATTR_SIZE) && attr->ia_size > inode->i_size)
0276         goto out_unlock;
0277 
0278     error = setattr_prepare(&init_user_ns, dentry, attr);
0279     if (error)
0280         goto out_unlock;
0281 
0282     if ((attr->ia_valid & ATTR_SIZE) &&
0283         attr->ia_size != i_size_read(inode)) {
0284         error = inode_newsize_ok(inode, attr->ia_size);
0285         if (error)
0286             goto out_unlock;
0287 
0288         truncate_setsize(inode, attr->ia_size);
0289         hpfs_truncate(inode);
0290     }
0291 
0292     setattr_copy(&init_user_ns, inode, attr);
0293 
0294     hpfs_write_inode(inode);
0295 
0296  out_unlock:
0297     hpfs_unlock(inode->i_sb);
0298     return error;
0299 }
0300 
0301 void hpfs_write_if_changed(struct inode *inode)
0302 {
0303     struct hpfs_inode_info *hpfs_inode = hpfs_i(inode);
0304 
0305     if (hpfs_inode->i_dirty)
0306         hpfs_write_inode(inode);
0307 }
0308 
0309 void hpfs_evict_inode(struct inode *inode)
0310 {
0311     truncate_inode_pages_final(&inode->i_data);
0312     clear_inode(inode);
0313     if (!inode->i_nlink) {
0314         hpfs_lock(inode->i_sb);
0315         hpfs_remove_fnode(inode->i_sb, inode->i_ino);
0316         hpfs_unlock(inode->i_sb);
0317     }
0318 }