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