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