blob: 2a8e07425de01eab5f8a40b5505268b4cc6e2014 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include "hpfs_fn.h"
11
12static int hpfs_dir_release(struct inode *inode, struct file *filp)
13{
Arnd Bergmann9a311b92011-01-22 20:26:12 +010014 hpfs_lock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 hpfs_del_pos(inode, &filp->f_pos);
16 /*hpfs_write_if_changed(inode);*/
Arnd Bergmann9a311b92011-01-22 20:26:12 +010017 hpfs_unlock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 return 0;
19}
20
21/* This is slow, but it's not used often */
22
23static 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 Viro496ad9a2013-01-23 17:07:38 -050028 struct inode *i = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 struct hpfs_inode_info *hpfs_inode = hpfs_i(i);
30 struct super_block *s = i->i_sb;
31
Josef Bacik06222e42011-07-18 13:21:38 -040032 /* Somebody else will have to figure out what to do here */
33 if (whence == SEEK_DATA || whence == SEEK_HOLE)
34 return -EINVAL;
35
Al Viro31abdab2013-05-18 02:38:52 -040036 mutex_lock(&i->i_mutex);
Arnd Bergmann9a311b92011-01-22 20:26:12 +010037 hpfs_lock(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Fabian Frederickb7cb1ce2014-06-06 14:36:34 -070039 /*pr_info("dir lseek\n");*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 if (new_off == 0 || new_off == 1 || new_off == 11 || new_off == 12 || new_off == 13) goto ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 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 Viro31abdab2013-05-18 02:38:52 -040047 hpfs_add_pos(i, &filp->f_pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048ok:
Al Viro31abdab2013-05-18 02:38:52 -040049 filp->f_pos = new_off;
Arnd Bergmann9a311b92011-01-22 20:26:12 +010050 hpfs_unlock(s);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080051 mutex_unlock(&i->i_mutex);
Al Viro31abdab2013-05-18 02:38:52 -040052 return new_off;
53fail:
Fabian Frederickb7cb1ce2014-06-06 14:36:34 -070054 /*pr_warn("illegal lseek: %016llx\n", new_off);*/
Arnd Bergmann9a311b92011-01-22 20:26:12 +010055 hpfs_unlock(s);
Al Viro31abdab2013-05-18 02:38:52 -040056 mutex_unlock(&i->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 return -ESPIPE;
58}
59
Al Viro568f8f52013-05-18 02:58:57 -040060static int hpfs_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Al Viro568f8f52013-05-18 02:58:57 -040062 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 struct hpfs_inode_info *hpfs_inode = hpfs_i(inode);
64 struct quad_buffer_head qbh;
65 struct hpfs_dirent *de;
66 int lc;
Al Viro568f8f52013-05-18 02:58:57 -040067 loff_t next_pos;
Al Viro7e7742e2010-01-31 17:09:29 -050068 unsigned char *tempname;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 int c1, c2 = 0;
70 int ret = 0;
71
Arnd Bergmann9a311b92011-01-22 20:26:12 +010072 hpfs_lock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
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 Viroc4c99542012-04-06 14:30:07 -040092 if (!fnode_is_dir(fno)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 e = 1;
Randy Dunlap18debbb2006-12-06 20:37:05 -080094 hpfs_error(inode->i_sb, "not a directory, fnode %08lx",
95 (unsigned long)inode->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 }
Mikulas Patocka0b697602011-05-08 20:44:26 +020097 if (hpfs_inode->i_dno != le32_to_cpu(fno->u.external[0].disk_secno)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 e = 1;
Mikulas Patocka0b697602011-05-08 20:44:26 +020099 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 Torvalds1da177e2005-04-16 15:20:36 -0700100 }
101 brelse(bh);
102 if (e) {
103 ret = -EFSERROR;
104 goto out;
105 }
106 }
107 lc = hpfs_sb(inode->i_sb)->sb_lowercase;
Al Viro568f8f52013-05-18 02:58:57 -0400108 if (ctx->pos == 12) { /* diff -r requires this (note, that diff -r */
109 ctx->pos = 13; /* also fails on msdos filesystem in 2.0) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 goto out;
111 }
Al Viro568f8f52013-05-18 02:58:57 -0400112 if (ctx->pos == 13) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 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)
Al Viro568f8f52013-05-18 02:58:57 -0400123 if (hpfs_stop_cycles(inode->i_sb, ctx->pos, &c1, &c2, "hpfs_readdir")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 ret = -EFSERROR;
125 goto out;
126 }
Al Viro568f8f52013-05-18 02:58:57 -0400127 if (ctx->pos == 12)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 goto out;
Al Viro568f8f52013-05-18 02:58:57 -0400129 if (ctx->pos == 3 || ctx->pos == 4 || ctx->pos == 5) {
Fabian Fredericka19189e2014-06-06 14:36:36 -0700130 pr_err("pos==%d\n", (int)ctx->pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 goto out;
132 }
Al Viro568f8f52013-05-18 02:58:57 -0400133 if (ctx->pos == 0) {
134 if (!dir_emit_dot(file, ctx))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 goto out;
Al Viro568f8f52013-05-18 02:58:57 -0400136 ctx->pos = 11;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 }
Al Viro568f8f52013-05-18 02:58:57 -0400138 if (ctx->pos == 11) {
139 if (!dir_emit(ctx, "..", 2, hpfs_inode->i_parent_dir, DT_DIR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 goto out;
Al Viro568f8f52013-05-18 02:58:57 -0400141 ctx->pos = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 }
Al Viro568f8f52013-05-18 02:58:57 -0400143 if (ctx->pos == 1) {
144 ctx->pos = ((loff_t) hpfs_de_as_down_as_possible(inode->i_sb, hpfs_inode->i_dno) << 4) + 1;
145 hpfs_add_pos(inode, &file->f_pos);
146 file->f_version = inode->i_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 }
Al Viro568f8f52013-05-18 02:58:57 -0400148 next_pos = ctx->pos;
149 if (!(de = map_pos_dirent(inode, &next_pos, &qbh))) {
150 ctx->pos = next_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 ret = -EIOERROR;
152 goto out;
153 }
154 if (de->first || de->last) {
155 if (hpfs_sb(inode->i_sb)->sb_chk) {
Randy Dunlap18debbb2006-12-06 20:37:05 -0800156 if (de->first && !de->last && (de->namelen != 2
157 || de ->name[0] != 1 || de->name[1] != 1))
Al Viro568f8f52013-05-18 02:58:57 -0400158 hpfs_error(inode->i_sb, "hpfs_readdir: bad ^A^A entry; pos = %08lx", (unsigned long)ctx->pos);
Randy Dunlap18debbb2006-12-06 20:37:05 -0800159 if (de->last && (de->namelen != 1 || de ->name[0] != 255))
Al Viro568f8f52013-05-18 02:58:57 -0400160 hpfs_error(inode->i_sb, "hpfs_readdir: bad \\377 entry; pos = %08lx", (unsigned long)ctx->pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
162 hpfs_brelse4(&qbh);
Al Viro568f8f52013-05-18 02:58:57 -0400163 ctx->pos = next_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 goto again;
165 }
166 tempname = hpfs_translate_name(inode->i_sb, de->name, de->namelen, lc, de->not_8x3);
Al Viro568f8f52013-05-18 02:58:57 -0400167 if (!dir_emit(ctx, tempname, de->namelen, le32_to_cpu(de->fnode), DT_UNKNOWN)) {
Al Viro7e7742e2010-01-31 17:09:29 -0500168 if (tempname != de->name) kfree(tempname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 hpfs_brelse4(&qbh);
170 goto out;
171 }
Al Viro568f8f52013-05-18 02:58:57 -0400172 ctx->pos = next_pos;
Al Viro7e7742e2010-01-31 17:09:29 -0500173 if (tempname != de->name) kfree(tempname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 hpfs_brelse4(&qbh);
175 }
176out:
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100177 hpfs_unlock(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 return ret;
179}
180
181/*
182 * lookup. Search the specified directory for the specified name, set
183 * *result to the corresponding inode.
184 *
185 * lookup uses the inode number to tell read_inode whether it is reading
186 * the inode of a directory or a file -- file ino's are odd, directory
187 * ino's are even. read_inode avoids i/o for file inodes; everything
188 * needed is up here in the directory. (And file fnodes are out in
189 * the boondocks.)
190 *
191 * - M.P.: this is over, sometimes we've got to read file's fnode for eas
192 * inode numbers are just fnode sector numbers; iget lock is used
193 * to tell read_inode to read fnode or not.
194 */
195
Al Viro00cd8dd2012-06-10 17:13:09 -0400196struct dentry *hpfs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Al Viro7e7742e2010-01-31 17:09:29 -0500198 const unsigned char *name = dentry->d_name.name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 unsigned len = dentry->d_name.len;
200 struct quad_buffer_head qbh;
201 struct hpfs_dirent *de;
202 ino_t ino;
203 int err;
204 struct inode *result = NULL;
205 struct hpfs_inode_info *hpfs_result;
206
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100207 hpfs_lock(dir->i_sb);
Al Viro7e7742e2010-01-31 17:09:29 -0500208 if ((err = hpfs_chk_name(name, &len))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (err == -ENAMETOOLONG) {
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100210 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return ERR_PTR(-ENAMETOOLONG);
212 }
213 goto end_add;
214 }
215
216 /*
217 * '.' and '..' will never be passed here.
218 */
219
Al Viro7e7742e2010-01-31 17:09:29 -0500220 de = map_dirent(dir, hpfs_i(dir)->i_dno, name, len, NULL, &qbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 /*
223 * This is not really a bailout, just means file not found.
224 */
225
226 if (!de) goto end;
227
228 /*
229 * Get inode number, what we're after.
230 */
231
Mikulas Patocka0b697602011-05-08 20:44:26 +0200232 ino = le32_to_cpu(de->fnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 /*
235 * Go find or make an inode.
236 */
237
238 result = iget_locked(dir->i_sb, ino);
239 if (!result) {
240 hpfs_error(dir->i_sb, "hpfs_lookup: can't get inode");
241 goto bail1;
242 }
243 if (result->i_state & I_NEW) {
244 hpfs_init_inode(result);
245 if (de->directory)
246 hpfs_read_inode(result);
Mikulas Patocka0b697602011-05-08 20:44:26 +0200247 else if (le32_to_cpu(de->ea_size) && hpfs_sb(dir->i_sb)->sb_eas)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 hpfs_read_inode(result);
249 else {
250 result->i_mode |= S_IFREG;
251 result->i_mode &= ~0111;
252 result->i_op = &hpfs_file_iops;
253 result->i_fop = &hpfs_file_ops;
Miklos Szeredibfe86842011-10-28 14:13:29 +0200254 set_nlink(result, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 }
256 unlock_new_inode(result);
257 }
258 hpfs_result = hpfs_i(result);
259 if (!de->directory) hpfs_result->i_parent_dir = dir->i_ino;
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (de->has_acl || de->has_xtd_perm) if (!(dir->i_sb->s_flags & MS_RDONLY)) {
262 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");
263 goto bail1;
264 }
265
266 /*
267 * Fill in the info from the directory if this is a newly created
268 * inode.
269 */
270
271 if (!result->i_ctime.tv_sec) {
Mikulas Patocka0b697602011-05-08 20:44:26 +0200272 if (!(result->i_ctime.tv_sec = local_to_gmt(dir->i_sb, le32_to_cpu(de->creation_date))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 result->i_ctime.tv_sec = 1;
274 result->i_ctime.tv_nsec = 0;
Mikulas Patocka0b697602011-05-08 20:44:26 +0200275 result->i_mtime.tv_sec = local_to_gmt(dir->i_sb, le32_to_cpu(de->write_date));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 result->i_mtime.tv_nsec = 0;
Mikulas Patocka0b697602011-05-08 20:44:26 +0200277 result->i_atime.tv_sec = local_to_gmt(dir->i_sb, le32_to_cpu(de->read_date));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 result->i_atime.tv_nsec = 0;
Mikulas Patocka0b697602011-05-08 20:44:26 +0200279 hpfs_result->i_ea_size = le32_to_cpu(de->ea_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (!hpfs_result->i_ea_mode && de->read_only)
281 result->i_mode &= ~0222;
282 if (!de->directory) {
283 if (result->i_size == -1) {
Mikulas Patocka0b697602011-05-08 20:44:26 +0200284 result->i_size = le32_to_cpu(de->file_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 result->i_data.a_ops = &hpfs_aops;
286 hpfs_i(result)->mmu_private = result->i_size;
287 /*
288 * i_blocks should count the fnode and any anodes.
289 * We count 1 for the fnode and don't bother about
290 * anodes -- the disk heads are on the directory band
291 * and we want them to stay there.
292 */
293 result->i_blocks = 1 + ((result->i_size + 511) >> 9);
294 }
295 }
296 }
297
298 hpfs_brelse4(&qbh);
299
300 /*
301 * Made it.
302 */
303
304 end:
305 end_add:
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100306 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 d_add(dentry, result);
308 return NULL;
309
310 /*
311 * Didn't.
312 */
313 bail1:
314
315 hpfs_brelse4(&qbh);
316
317 /*bail:*/
318
Arnd Bergmann9a311b92011-01-22 20:26:12 +0100319 hpfs_unlock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return ERR_PTR(-ENOENT);
321}
322
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800323const struct file_operations hpfs_dir_ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
325 .llseek = hpfs_dir_lseek,
326 .read = generic_read_dir,
Al Viro568f8f52013-05-18 02:58:57 -0400327 .iterate = hpfs_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 .release = hpfs_dir_release,
329 .fsync = hpfs_file_fsync,
330};