Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * linux/fs/hpfs/dir.c |
| 3 | * |
| 4 | * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999 |
| 5 | * |
| 6 | * directory VFS functions |
| 7 | */ |
| 8 | |
| 9 | #include "hpfs_fn.h" |
| 10 | |
| 11 | static int hpfs_dir_release(struct inode *inode, struct file *filp) |
| 12 | { |
| 13 | lock_kernel(); |
| 14 | hpfs_del_pos(inode, &filp->f_pos); |
| 15 | /*hpfs_write_if_changed(inode);*/ |
| 16 | unlock_kernel(); |
| 17 | return 0; |
| 18 | } |
| 19 | |
| 20 | /* This is slow, but it's not used often */ |
| 21 | |
| 22 | static loff_t hpfs_dir_lseek(struct file *filp, loff_t off, int whence) |
| 23 | { |
| 24 | loff_t new_off = off + (whence == 1 ? filp->f_pos : 0); |
| 25 | loff_t pos; |
| 26 | struct quad_buffer_head qbh; |
| 27 | struct inode *i = filp->f_dentry->d_inode; |
| 28 | struct hpfs_inode_info *hpfs_inode = hpfs_i(i); |
| 29 | struct super_block *s = i->i_sb; |
| 30 | |
| 31 | lock_kernel(); |
| 32 | |
| 33 | /*printk("dir lseek\n");*/ |
| 34 | if (new_off == 0 || new_off == 1 || new_off == 11 || new_off == 12 || new_off == 13) goto ok; |
| 35 | down(&i->i_sem); |
| 36 | pos = ((loff_t) hpfs_de_as_down_as_possible(s, hpfs_inode->i_dno) << 4) + 1; |
| 37 | while (pos != new_off) { |
| 38 | if (map_pos_dirent(i, &pos, &qbh)) hpfs_brelse4(&qbh); |
| 39 | else goto fail; |
| 40 | if (pos == 12) goto fail; |
| 41 | } |
| 42 | up(&i->i_sem); |
| 43 | ok: |
| 44 | unlock_kernel(); |
| 45 | return filp->f_pos = new_off; |
| 46 | fail: |
| 47 | up(&i->i_sem); |
| 48 | /*printk("illegal lseek: %016llx\n", new_off);*/ |
| 49 | unlock_kernel(); |
| 50 | return -ESPIPE; |
| 51 | } |
| 52 | |
| 53 | static int hpfs_readdir(struct file *filp, void *dirent, filldir_t filldir) |
| 54 | { |
| 55 | struct inode *inode = filp->f_dentry->d_inode; |
| 56 | struct hpfs_inode_info *hpfs_inode = hpfs_i(inode); |
| 57 | struct quad_buffer_head qbh; |
| 58 | struct hpfs_dirent *de; |
| 59 | int lc; |
| 60 | long old_pos; |
| 61 | char *tempname; |
| 62 | int c1, c2 = 0; |
| 63 | int ret = 0; |
| 64 | |
| 65 | lock_kernel(); |
| 66 | |
| 67 | if (hpfs_sb(inode->i_sb)->sb_chk) { |
| 68 | if (hpfs_chk_sectors(inode->i_sb, inode->i_ino, 1, "dir_fnode")) { |
| 69 | ret = -EFSERROR; |
| 70 | goto out; |
| 71 | } |
| 72 | if (hpfs_chk_sectors(inode->i_sb, hpfs_inode->i_dno, 4, "dir_dnode")) { |
| 73 | ret = -EFSERROR; |
| 74 | goto out; |
| 75 | } |
| 76 | } |
| 77 | if (hpfs_sb(inode->i_sb)->sb_chk >= 2) { |
| 78 | struct buffer_head *bh; |
| 79 | struct fnode *fno; |
| 80 | int e = 0; |
| 81 | if (!(fno = hpfs_map_fnode(inode->i_sb, inode->i_ino, &bh))) { |
| 82 | ret = -EIOERROR; |
| 83 | goto out; |
| 84 | } |
| 85 | if (!fno->dirflag) { |
| 86 | e = 1; |
| 87 | hpfs_error(inode->i_sb, "not a directory, fnode %08x",inode->i_ino); |
| 88 | } |
| 89 | if (hpfs_inode->i_dno != fno->u.external[0].disk_secno) { |
| 90 | e = 1; |
| 91 | hpfs_error(inode->i_sb, "corrupted inode: i_dno == %08x, fnode -> dnode == %08x", hpfs_inode->i_dno, fno->u.external[0].disk_secno); |
| 92 | } |
| 93 | brelse(bh); |
| 94 | if (e) { |
| 95 | ret = -EFSERROR; |
| 96 | goto out; |
| 97 | } |
| 98 | } |
| 99 | lc = hpfs_sb(inode->i_sb)->sb_lowercase; |
| 100 | if (filp->f_pos == 12) { /* diff -r requires this (note, that diff -r */ |
| 101 | filp->f_pos = 13; /* also fails on msdos filesystem in 2.0) */ |
| 102 | goto out; |
| 103 | } |
| 104 | if (filp->f_pos == 13) { |
| 105 | ret = -ENOENT; |
| 106 | goto out; |
| 107 | } |
| 108 | |
| 109 | while (1) { |
| 110 | again: |
| 111 | /* This won't work when cycle is longer than number of dirents |
| 112 | accepted by filldir, but what can I do? |
| 113 | maybe killall -9 ls helps */ |
| 114 | if (hpfs_sb(inode->i_sb)->sb_chk) |
| 115 | if (hpfs_stop_cycles(inode->i_sb, filp->f_pos, &c1, &c2, "hpfs_readdir")) { |
| 116 | ret = -EFSERROR; |
| 117 | goto out; |
| 118 | } |
| 119 | if (filp->f_pos == 12) |
| 120 | goto out; |
| 121 | if (filp->f_pos == 3 || filp->f_pos == 4 || filp->f_pos == 5) { |
| 122 | printk("HPFS: warning: pos==%d\n",(int)filp->f_pos); |
| 123 | goto out; |
| 124 | } |
| 125 | if (filp->f_pos == 0) { |
| 126 | if (filldir(dirent, ".", 1, filp->f_pos, inode->i_ino, DT_DIR) < 0) |
| 127 | goto out; |
| 128 | filp->f_pos = 11; |
| 129 | } |
| 130 | if (filp->f_pos == 11) { |
| 131 | if (filldir(dirent, "..", 2, filp->f_pos, hpfs_inode->i_parent_dir, DT_DIR) < 0) |
| 132 | goto out; |
| 133 | filp->f_pos = 1; |
| 134 | } |
| 135 | if (filp->f_pos == 1) { |
| 136 | filp->f_pos = ((loff_t) hpfs_de_as_down_as_possible(inode->i_sb, hpfs_inode->i_dno) << 4) + 1; |
| 137 | hpfs_add_pos(inode, &filp->f_pos); |
| 138 | filp->f_version = inode->i_version; |
| 139 | } |
| 140 | old_pos = filp->f_pos; |
| 141 | if (!(de = map_pos_dirent(inode, &filp->f_pos, &qbh))) { |
| 142 | ret = -EIOERROR; |
| 143 | goto out; |
| 144 | } |
| 145 | if (de->first || de->last) { |
| 146 | if (hpfs_sb(inode->i_sb)->sb_chk) { |
| 147 | if (de->first && !de->last && (de->namelen != 2 || de ->name[0] != 1 || de->name[1] != 1)) hpfs_error(inode->i_sb, "hpfs_readdir: bad ^A^A entry; pos = %08x", old_pos); |
| 148 | if (de->last && (de->namelen != 1 || de ->name[0] != 255)) hpfs_error(inode->i_sb, "hpfs_readdir: bad \\377 entry; pos = %08x", old_pos); |
| 149 | } |
| 150 | hpfs_brelse4(&qbh); |
| 151 | goto again; |
| 152 | } |
| 153 | tempname = hpfs_translate_name(inode->i_sb, de->name, de->namelen, lc, de->not_8x3); |
| 154 | if (filldir(dirent, tempname, de->namelen, old_pos, de->fnode, DT_UNKNOWN) < 0) { |
| 155 | filp->f_pos = old_pos; |
| 156 | if (tempname != (char *)de->name) kfree(tempname); |
| 157 | hpfs_brelse4(&qbh); |
| 158 | goto out; |
| 159 | } |
| 160 | if (tempname != (char *)de->name) kfree(tempname); |
| 161 | hpfs_brelse4(&qbh); |
| 162 | } |
| 163 | out: |
| 164 | unlock_kernel(); |
| 165 | return ret; |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | * lookup. Search the specified directory for the specified name, set |
| 170 | * *result to the corresponding inode. |
| 171 | * |
| 172 | * lookup uses the inode number to tell read_inode whether it is reading |
| 173 | * the inode of a directory or a file -- file ino's are odd, directory |
| 174 | * ino's are even. read_inode avoids i/o for file inodes; everything |
| 175 | * needed is up here in the directory. (And file fnodes are out in |
| 176 | * the boondocks.) |
| 177 | * |
| 178 | * - M.P.: this is over, sometimes we've got to read file's fnode for eas |
| 179 | * inode numbers are just fnode sector numbers; iget lock is used |
| 180 | * to tell read_inode to read fnode or not. |
| 181 | */ |
| 182 | |
| 183 | struct dentry *hpfs_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd) |
| 184 | { |
| 185 | const char *name = dentry->d_name.name; |
| 186 | unsigned len = dentry->d_name.len; |
| 187 | struct quad_buffer_head qbh; |
| 188 | struct hpfs_dirent *de; |
| 189 | ino_t ino; |
| 190 | int err; |
| 191 | struct inode *result = NULL; |
| 192 | struct hpfs_inode_info *hpfs_result; |
| 193 | |
| 194 | lock_kernel(); |
| 195 | if ((err = hpfs_chk_name((char *)name, &len))) { |
| 196 | if (err == -ENAMETOOLONG) { |
| 197 | unlock_kernel(); |
| 198 | return ERR_PTR(-ENAMETOOLONG); |
| 199 | } |
| 200 | goto end_add; |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * '.' and '..' will never be passed here. |
| 205 | */ |
| 206 | |
| 207 | de = map_dirent(dir, hpfs_i(dir)->i_dno, (char *) name, len, NULL, &qbh); |
| 208 | |
| 209 | /* |
| 210 | * This is not really a bailout, just means file not found. |
| 211 | */ |
| 212 | |
| 213 | if (!de) goto end; |
| 214 | |
| 215 | /* |
| 216 | * Get inode number, what we're after. |
| 217 | */ |
| 218 | |
| 219 | ino = de->fnode; |
| 220 | |
| 221 | /* |
| 222 | * Go find or make an inode. |
| 223 | */ |
| 224 | |
| 225 | result = iget_locked(dir->i_sb, ino); |
| 226 | if (!result) { |
| 227 | hpfs_error(dir->i_sb, "hpfs_lookup: can't get inode"); |
| 228 | goto bail1; |
| 229 | } |
| 230 | if (result->i_state & I_NEW) { |
| 231 | hpfs_init_inode(result); |
| 232 | if (de->directory) |
| 233 | hpfs_read_inode(result); |
| 234 | else if (de->ea_size && hpfs_sb(dir->i_sb)->sb_eas) |
| 235 | hpfs_read_inode(result); |
| 236 | else { |
| 237 | result->i_mode |= S_IFREG; |
| 238 | result->i_mode &= ~0111; |
| 239 | result->i_op = &hpfs_file_iops; |
| 240 | result->i_fop = &hpfs_file_ops; |
| 241 | result->i_nlink = 1; |
| 242 | } |
| 243 | unlock_new_inode(result); |
| 244 | } |
| 245 | hpfs_result = hpfs_i(result); |
| 246 | if (!de->directory) hpfs_result->i_parent_dir = dir->i_ino; |
| 247 | |
| 248 | hpfs_decide_conv(result, (char *)name, len); |
| 249 | |
| 250 | if (de->has_acl || de->has_xtd_perm) if (!(dir->i_sb->s_flags & MS_RDONLY)) { |
| 251 | hpfs_error(result->i_sb, "ACLs or XPERM found. This is probably HPFS386. This driver doesn't support it now. Send me some info on these structures"); |
| 252 | goto bail1; |
| 253 | } |
| 254 | |
| 255 | /* |
| 256 | * Fill in the info from the directory if this is a newly created |
| 257 | * inode. |
| 258 | */ |
| 259 | |
| 260 | if (!result->i_ctime.tv_sec) { |
| 261 | if (!(result->i_ctime.tv_sec = local_to_gmt(dir->i_sb, de->creation_date))) |
| 262 | result->i_ctime.tv_sec = 1; |
| 263 | result->i_ctime.tv_nsec = 0; |
| 264 | result->i_mtime.tv_sec = local_to_gmt(dir->i_sb, de->write_date); |
| 265 | result->i_mtime.tv_nsec = 0; |
| 266 | result->i_atime.tv_sec = local_to_gmt(dir->i_sb, de->read_date); |
| 267 | result->i_atime.tv_nsec = 0; |
| 268 | hpfs_result->i_ea_size = de->ea_size; |
| 269 | if (!hpfs_result->i_ea_mode && de->read_only) |
| 270 | result->i_mode &= ~0222; |
| 271 | if (!de->directory) { |
| 272 | if (result->i_size == -1) { |
| 273 | result->i_size = de->file_size; |
| 274 | result->i_data.a_ops = &hpfs_aops; |
| 275 | hpfs_i(result)->mmu_private = result->i_size; |
| 276 | /* |
| 277 | * i_blocks should count the fnode and any anodes. |
| 278 | * We count 1 for the fnode and don't bother about |
| 279 | * anodes -- the disk heads are on the directory band |
| 280 | * and we want them to stay there. |
| 281 | */ |
| 282 | result->i_blocks = 1 + ((result->i_size + 511) >> 9); |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | hpfs_brelse4(&qbh); |
| 288 | |
| 289 | /* |
| 290 | * Made it. |
| 291 | */ |
| 292 | |
| 293 | end: |
| 294 | end_add: |
| 295 | hpfs_set_dentry_operations(dentry); |
| 296 | unlock_kernel(); |
| 297 | d_add(dentry, result); |
| 298 | return NULL; |
| 299 | |
| 300 | /* |
| 301 | * Didn't. |
| 302 | */ |
| 303 | bail1: |
| 304 | |
| 305 | hpfs_brelse4(&qbh); |
| 306 | |
| 307 | /*bail:*/ |
| 308 | |
| 309 | unlock_kernel(); |
| 310 | return ERR_PTR(-ENOENT); |
| 311 | } |
| 312 | |
| 313 | struct file_operations hpfs_dir_ops = |
| 314 | { |
| 315 | .llseek = hpfs_dir_lseek, |
| 316 | .read = generic_read_dir, |
| 317 | .readdir = hpfs_readdir, |
| 318 | .release = hpfs_dir_release, |
| 319 | .fsync = hpfs_file_fsync, |
| 320 | }; |