0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #ifdef pr_fmt
0013 #undef pr_fmt
0014 #endif
0015
0016 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0017
0018 #include <linux/mutex.h>
0019 #include <linux/pagemap.h>
0020 #include <linux/buffer_head.h>
0021 #include <linux/slab.h>
0022 #include <linux/sched/signal.h>
0023 #include <linux/blkdev.h>
0024 #include <asm/unaligned.h>
0025
0026 #include "hpfs.h"
0027
0028 #define EIOERROR EIO
0029 #define EFSERROR EUCLEAN
0030
0031 #define ANODE_ALLOC_FWD 512
0032 #define FNODE_ALLOC_FWD 0
0033 #define ALLOC_FWD_MIN 16
0034 #define ALLOC_FWD_MAX 128
0035 #define ALLOC_M 1
0036 #define FNODE_RD_AHEAD 16
0037 #define ANODE_RD_AHEAD 0
0038 #define DNODE_RD_AHEAD 72
0039 #define COUNT_RD_AHEAD 62
0040
0041 #define FREE_DNODES_ADD 58
0042 #define FREE_DNODES_DEL 29
0043
0044 #define CHKCOND(x,y) if (!(x)) printk y
0045
0046 struct hpfs_inode_info {
0047 loff_t mmu_private;
0048 ino_t i_parent_dir;
0049 unsigned i_dno;
0050 unsigned i_dpos;
0051 unsigned i_dsubdno;
0052 unsigned i_file_sec;
0053 unsigned i_disk_sec;
0054 unsigned i_n_secs;
0055 unsigned i_ea_size;
0056 unsigned i_ea_mode : 1;
0057 unsigned i_ea_uid : 1;
0058 unsigned i_ea_gid : 1;
0059 unsigned i_dirty : 1;
0060 loff_t **i_rddir_off;
0061 struct inode vfs_inode;
0062 };
0063
0064 struct hpfs_sb_info {
0065 struct mutex hpfs_mutex;
0066 ino_t sb_root;
0067 unsigned sb_fs_size;
0068 unsigned sb_bitmaps;
0069 unsigned sb_dirband_start;
0070 unsigned sb_dirband_size;
0071 unsigned sb_dmap;
0072 unsigned sb_n_free;
0073 unsigned sb_n_free_dnodes;
0074 kuid_t sb_uid;
0075 kgid_t sb_gid;
0076 umode_t sb_mode;
0077 unsigned sb_eas : 2;
0078 unsigned sb_err : 2;
0079 unsigned sb_chk : 2;
0080 unsigned sb_lowercase : 1;
0081 unsigned sb_was_error : 1;
0082 unsigned sb_chkdsk : 2;
0083 unsigned char *sb_cp_table;
0084
0085
0086 __le32 *sb_bmp_dir;
0087 unsigned sb_c_bitmap;
0088 unsigned sb_max_fwd_alloc;
0089 int sb_timeshift;
0090 struct rcu_head rcu;
0091
0092 unsigned n_hotfixes;
0093 secno hotfix_from[256];
0094 secno hotfix_to[256];
0095 };
0096
0097
0098
0099 struct quad_buffer_head {
0100 struct buffer_head *bh[4];
0101 void *data;
0102 };
0103
0104
0105
0106 static inline dnode_secno de_down_pointer (struct hpfs_dirent *de)
0107 {
0108 CHKCOND(de->down,("HPFS: de_down_pointer: !de->down\n"));
0109 return le32_to_cpu(*(__le32 *) ((void *) de + le16_to_cpu(de->length) - 4));
0110 }
0111
0112
0113
0114 static inline struct hpfs_dirent *dnode_first_de (struct dnode *dnode)
0115 {
0116 return (void *) dnode->dirent;
0117 }
0118
0119
0120
0121 static inline struct hpfs_dirent *dnode_end_de (struct dnode *dnode)
0122 {
0123 CHKCOND(le32_to_cpu(dnode->first_free)>=0x14 && le32_to_cpu(dnode->first_free)<=0xa00,("HPFS: dnode_end_de: dnode->first_free = %x\n",(unsigned)le32_to_cpu(dnode->first_free)));
0124 return (void *) dnode + le32_to_cpu(dnode->first_free);
0125 }
0126
0127
0128
0129 static inline struct hpfs_dirent *de_next_de (struct hpfs_dirent *de)
0130 {
0131 CHKCOND(le16_to_cpu(de->length)>=0x20 && le16_to_cpu(de->length)<0x800,("HPFS: de_next_de: de->length = %x\n",(unsigned)le16_to_cpu(de->length)));
0132 return (void *) de + le16_to_cpu(de->length);
0133 }
0134
0135 static inline struct extended_attribute *fnode_ea(struct fnode *fnode)
0136 {
0137 return (struct extended_attribute *)((char *)fnode + le16_to_cpu(fnode->ea_offs) + le16_to_cpu(fnode->acl_size_s));
0138 }
0139
0140 static inline struct extended_attribute *fnode_end_ea(struct fnode *fnode)
0141 {
0142 return (struct extended_attribute *)((char *)fnode + le16_to_cpu(fnode->ea_offs) + le16_to_cpu(fnode->acl_size_s) + le16_to_cpu(fnode->ea_size_s));
0143 }
0144
0145 static unsigned ea_valuelen(struct extended_attribute *ea)
0146 {
0147 return ea->valuelen_lo + 256 * ea->valuelen_hi;
0148 }
0149
0150 static inline struct extended_attribute *next_ea(struct extended_attribute *ea)
0151 {
0152 return (struct extended_attribute *)((char *)ea + 5 + ea->namelen + ea_valuelen(ea));
0153 }
0154
0155 static inline secno ea_sec(struct extended_attribute *ea)
0156 {
0157 return le32_to_cpu(get_unaligned((__le32 *)((char *)ea + 9 + ea->namelen)));
0158 }
0159
0160 static inline secno ea_len(struct extended_attribute *ea)
0161 {
0162 return le32_to_cpu(get_unaligned((__le32 *)((char *)ea + 5 + ea->namelen)));
0163 }
0164
0165 static inline char *ea_data(struct extended_attribute *ea)
0166 {
0167 return (char *)((char *)ea + 5 + ea->namelen);
0168 }
0169
0170 static inline unsigned de_size(int namelen, secno down_ptr)
0171 {
0172 return ((0x1f + namelen + 3) & ~3) + (down_ptr ? 4 : 0);
0173 }
0174
0175 static inline void copy_de(struct hpfs_dirent *dst, struct hpfs_dirent *src)
0176 {
0177 int a;
0178 int n;
0179 if (!dst || !src) return;
0180 a = dst->down;
0181 n = dst->not_8x3;
0182 memcpy((char *)dst + 2, (char *)src + 2, 28);
0183 dst->down = a;
0184 dst->not_8x3 = n;
0185 }
0186
0187 static inline unsigned tstbits(__le32 *bmp, unsigned b, unsigned n)
0188 {
0189 int i;
0190 if ((b >= 0x4000) || (b + n - 1 >= 0x4000)) return n;
0191 if (!((le32_to_cpu(bmp[(b & 0x3fff) >> 5]) >> (b & 0x1f)) & 1)) return 1;
0192 for (i = 1; i < n; i++)
0193 if (!((le32_to_cpu(bmp[((b+i) & 0x3fff) >> 5]) >> ((b+i) & 0x1f)) & 1))
0194 return i + 1;
0195 return 0;
0196 }
0197
0198
0199
0200 int hpfs_chk_sectors(struct super_block *, secno, int, char *);
0201 secno hpfs_alloc_sector(struct super_block *, secno, unsigned, int);
0202 int hpfs_alloc_if_possible(struct super_block *, secno);
0203 void hpfs_free_sectors(struct super_block *, secno, unsigned);
0204 int hpfs_check_free_dnodes(struct super_block *, int);
0205 void hpfs_free_dnode(struct super_block *, secno);
0206 struct dnode *hpfs_alloc_dnode(struct super_block *, secno, dnode_secno *, struct quad_buffer_head *);
0207 struct fnode *hpfs_alloc_fnode(struct super_block *, secno, fnode_secno *, struct buffer_head **);
0208 struct anode *hpfs_alloc_anode(struct super_block *, secno, anode_secno *, struct buffer_head **);
0209 int hpfs_trim_fs(struct super_block *, u64, u64, u64, unsigned *);
0210
0211
0212
0213 secno hpfs_bplus_lookup(struct super_block *, struct inode *, struct bplus_header *, unsigned, struct buffer_head *);
0214 secno hpfs_add_sector_to_btree(struct super_block *, secno, int, unsigned);
0215 void hpfs_remove_btree(struct super_block *, struct bplus_header *);
0216 int hpfs_ea_read(struct super_block *, secno, int, unsigned, unsigned, char *);
0217 int hpfs_ea_write(struct super_block *, secno, int, unsigned, unsigned, const char *);
0218 void hpfs_ea_remove(struct super_block *, secno, int, unsigned);
0219 void hpfs_truncate_btree(struct super_block *, secno, int, unsigned);
0220 void hpfs_remove_fnode(struct super_block *, fnode_secno fno);
0221
0222
0223
0224 secno hpfs_search_hotfix_map(struct super_block *s, secno sec);
0225 unsigned hpfs_search_hotfix_map_for_range(struct super_block *s, secno sec, unsigned n);
0226 void hpfs_prefetch_sectors(struct super_block *, unsigned, int);
0227 void *hpfs_map_sector(struct super_block *, unsigned, struct buffer_head **, int);
0228 void *hpfs_get_sector(struct super_block *, unsigned, struct buffer_head **);
0229 void *hpfs_map_4sectors(struct super_block *, unsigned, struct quad_buffer_head *, int);
0230 void *hpfs_get_4sectors(struct super_block *, unsigned, struct quad_buffer_head *);
0231 void hpfs_brelse4(struct quad_buffer_head *);
0232 void hpfs_mark_4buffers_dirty(struct quad_buffer_head *);
0233
0234
0235
0236 extern const struct dentry_operations hpfs_dentry_operations;
0237
0238
0239
0240 struct dentry *hpfs_lookup(struct inode *, struct dentry *, unsigned int);
0241 extern const struct file_operations hpfs_dir_ops;
0242
0243
0244
0245 int hpfs_add_pos(struct inode *, loff_t *);
0246 void hpfs_del_pos(struct inode *, loff_t *);
0247 struct hpfs_dirent *hpfs_add_de(struct super_block *, struct dnode *,
0248 const unsigned char *, unsigned, secno);
0249 int hpfs_add_dirent(struct inode *, const unsigned char *, unsigned,
0250 struct hpfs_dirent *);
0251 int hpfs_remove_dirent(struct inode *, dnode_secno, struct hpfs_dirent *, struct quad_buffer_head *, int);
0252 void hpfs_count_dnodes(struct super_block *, dnode_secno, int *, int *, int *);
0253 dnode_secno hpfs_de_as_down_as_possible(struct super_block *, dnode_secno dno);
0254 struct hpfs_dirent *map_pos_dirent(struct inode *, loff_t *, struct quad_buffer_head *);
0255 struct hpfs_dirent *map_dirent(struct inode *, dnode_secno,
0256 const unsigned char *, unsigned, dnode_secno *,
0257 struct quad_buffer_head *);
0258 void hpfs_remove_dtree(struct super_block *, dnode_secno);
0259 struct hpfs_dirent *map_fnode_dirent(struct super_block *, fnode_secno, struct fnode *, struct quad_buffer_head *);
0260
0261
0262
0263 void hpfs_ea_ext_remove(struct super_block *, secno, int, unsigned);
0264 int hpfs_read_ea(struct super_block *, struct fnode *, char *, char *, int);
0265 char *hpfs_get_ea(struct super_block *, struct fnode *, char *, int *);
0266 void hpfs_set_ea(struct inode *, struct fnode *, const char *,
0267 const char *, int);
0268
0269
0270
0271 int hpfs_file_fsync(struct file *, loff_t, loff_t, int);
0272 void hpfs_truncate(struct inode *);
0273 extern const struct file_operations hpfs_file_ops;
0274 extern const struct inode_operations hpfs_file_iops;
0275 extern const struct address_space_operations hpfs_aops;
0276
0277
0278
0279 void hpfs_init_inode(struct inode *);
0280 void hpfs_read_inode(struct inode *);
0281 void hpfs_write_inode(struct inode *);
0282 void hpfs_write_inode_nolock(struct inode *);
0283 int hpfs_setattr(struct user_namespace *, struct dentry *, struct iattr *);
0284 void hpfs_write_if_changed(struct inode *);
0285 void hpfs_evict_inode(struct inode *);
0286
0287
0288
0289 __le32 *hpfs_map_dnode_bitmap(struct super_block *, struct quad_buffer_head *);
0290 __le32 *hpfs_map_bitmap(struct super_block *, unsigned, struct quad_buffer_head *, char *);
0291 void hpfs_prefetch_bitmap(struct super_block *, unsigned);
0292 unsigned char *hpfs_load_code_page(struct super_block *, secno);
0293 __le32 *hpfs_load_bitmap_directory(struct super_block *, secno bmp);
0294 void hpfs_load_hotfix_map(struct super_block *s, struct hpfs_spare_block *spareblock);
0295 struct fnode *hpfs_map_fnode(struct super_block *s, ino_t, struct buffer_head **);
0296 struct anode *hpfs_map_anode(struct super_block *s, anode_secno, struct buffer_head **);
0297 struct dnode *hpfs_map_dnode(struct super_block *s, dnode_secno, struct quad_buffer_head *);
0298 dnode_secno hpfs_fnode_dno(struct super_block *s, ino_t ino);
0299
0300
0301
0302 unsigned char hpfs_upcase(unsigned char *, unsigned char);
0303 int hpfs_chk_name(const unsigned char *, unsigned *);
0304 unsigned char *hpfs_translate_name(struct super_block *, unsigned char *, unsigned, int, int);
0305 int hpfs_compare_names(struct super_block *, const unsigned char *, unsigned,
0306 const unsigned char *, unsigned, int);
0307 int hpfs_is_name_long(const unsigned char *, unsigned);
0308 void hpfs_adjust_length(const unsigned char *, unsigned *);
0309
0310
0311
0312 extern const struct inode_operations hpfs_dir_iops;
0313 extern const struct address_space_operations hpfs_symlink_aops;
0314
0315 static inline struct hpfs_inode_info *hpfs_i(struct inode *inode)
0316 {
0317 return container_of(inode, struct hpfs_inode_info, vfs_inode);
0318 }
0319
0320 static inline struct hpfs_sb_info *hpfs_sb(struct super_block *sb)
0321 {
0322 return sb->s_fs_info;
0323 }
0324
0325
0326
0327 __printf(2, 3)
0328 void hpfs_error(struct super_block *, const char *, ...);
0329 int hpfs_stop_cycles(struct super_block *, int, int *, int *, char *);
0330 unsigned hpfs_get_free_dnodes(struct super_block *);
0331 long hpfs_ioctl(struct file *file, unsigned cmd, unsigned long arg);
0332
0333
0334
0335
0336
0337 static inline time64_t local_to_gmt(struct super_block *s, time64_t t)
0338 {
0339 extern struct timezone sys_tz;
0340 return t + sys_tz.tz_minuteswest * 60 + hpfs_sb(s)->sb_timeshift;
0341 }
0342
0343 static inline time32_t gmt_to_local(struct super_block *s, time64_t t)
0344 {
0345 extern struct timezone sys_tz;
0346 return t - sys_tz.tz_minuteswest * 60 - hpfs_sb(s)->sb_timeshift;
0347 }
0348
0349 static inline time32_t local_get_seconds(struct super_block *s)
0350 {
0351 return gmt_to_local(s, ktime_get_real_seconds());
0352 }
0353
0354
0355
0356
0357
0358
0359
0360
0361
0362
0363 static inline void hpfs_lock(struct super_block *s)
0364 {
0365 struct hpfs_sb_info *sbi = hpfs_sb(s);
0366 mutex_lock(&sbi->hpfs_mutex);
0367 }
0368
0369 static inline void hpfs_unlock(struct super_block *s)
0370 {
0371 struct hpfs_sb_info *sbi = hpfs_sb(s);
0372 mutex_unlock(&sbi->hpfs_mutex);
0373 }
0374
0375 static inline void hpfs_lock_assert(struct super_block *s)
0376 {
0377 struct hpfs_sb_info *sbi = hpfs_sb(s);
0378 WARN_ON(!mutex_is_locked(&sbi->hpfs_mutex));
0379 }