dir.c 5.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
/*
 * dir.c
 *
 * PURPOSE
 *  Directory handling routines for the OSTA-UDF(tm) filesystem.
 *
 * COPYRIGHT
 *	This file is distributed under the terms of the GNU General Public
 *	License (GPL). Copies of the GPL can be obtained from:
 *		ftp://prep.ai.mit.edu/pub/gnu/GPL
 *	Each contributing author retains all rights to their own work.
 *
 *  (C) 1998-2004 Ben Fennema
 *
 * HISTORY
 *
 *  10/05/98 dgb  Split directory operations into its own file
 *                Implemented directory reads via do_udf_readdir
 *  10/06/98      Made directory operations work!
 *  11/17/98      Rewrote directory to support ICBTAG_FLAG_AD_LONG
 *  11/25/98 blf  Rewrote directory handling (readdir+lookup) to support reading
 *                across blocks.
 *  12/12/98      Split out the lookup code to namei.c. bulk of directory
 *                code now in directory.c:udf_fileident_read.
 */

#include "udfdecl.h"

#include <linux/string.h>
#include <linux/errno.h>
#include <linux/mm.h>
#include <linux/slab.h>

#include "udf_i.h"
#include "udf_sb.h"

A
Al Viro 已提交
37 38

static int udf_readdir(struct file *file, struct dir_context *ctx)
L
Linus Torvalds 已提交
39
{
A
Al Viro 已提交
40 41
	struct inode *dir = file_inode(file);
	struct udf_inode_info *iinfo = UDF_I(dir);
42
	struct udf_fileident_bh fibh = { .sbh = NULL, .ebh = NULL};
43
	struct fileIdentDesc *fi = NULL;
L
Linus Torvalds 已提交
44 45
	struct fileIdentDesc cfi;
	int block, iblock;
A
Al Viro 已提交
46
	loff_t nf_pos;
L
Linus Torvalds 已提交
47
	int flen;
48
	unsigned char *fname = NULL, *copy_name = NULL;
A
Al Viro 已提交
49
	unsigned char *nameptr;
L
Linus Torvalds 已提交
50 51
	uint16_t liu;
	uint8_t lfi;
J
Jan Kara 已提交
52
	loff_t size = udf_ext0_offset(dir) + dir->i_size;
J
Jan Kara 已提交
53
	struct buffer_head *tmp, *bha[16];
54
	struct kernel_lb_addr eloc;
J
Jan Kara 已提交
55
	uint32_t elen;
56
	sector_t offset;
57
	int i, num, ret = 0;
58
	struct extent_position epos = { NULL, 0, {0, 0} };
J
Jan Kara 已提交
59
	struct super_block *sb = dir->i_sb;
L
Linus Torvalds 已提交
60

A
Al Viro 已提交
61 62 63 64 65 66
	if (ctx->pos == 0) {
		if (!dir_emit_dot(file, ctx))
			return 0;
		ctx->pos = 1;
	}
	nf_pos = (ctx->pos - 1) << 2;
L
Linus Torvalds 已提交
67
	if (nf_pos >= size)
68 69 70 71 72 73 74
		goto out;

	fname = kmalloc(UDF_NAME_LEN, GFP_NOFS);
	if (!fname) {
		ret = -ENOMEM;
		goto out;
	}
L
Linus Torvalds 已提交
75 76

	if (nf_pos == 0)
J
Jan Kara 已提交
77
		nf_pos = udf_ext0_offset(dir);
L
Linus Torvalds 已提交
78

J
Jan Kara 已提交
79
	fibh.soffset = fibh.eoffset = nf_pos & (sb->s_blocksize - 1);
80
	if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
J
Jan Kara 已提交
81
		if (inode_bmap(dir, nf_pos >> sb->s_blocksize_bits,
82 83 84 85 86
		    &epos, &eloc, &elen, &offset)
		    != (EXT_RECORDED_ALLOCATED >> 30)) {
			ret = -ENOENT;
			goto out;
		}
J
Jan Kara 已提交
87 88
		block = udf_get_lb_pblock(sb, &eloc, offset);
		if ((++offset << sb->s_blocksize_bits) < elen) {
89
			if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
90
				epos.offset -= sizeof(struct short_ad);
91
			else if (iinfo->i_alloc_type ==
92
					ICBTAG_FLAG_AD_LONG)
93
				epos.offset -= sizeof(struct long_ad);
94
		} else {
L
Linus Torvalds 已提交
95
			offset = 0;
96
		}
L
Linus Torvalds 已提交
97

J
Jan Kara 已提交
98
		if (!(fibh.sbh = fibh.ebh = udf_tread(sb, block))) {
99 100
			ret = -EIO;
			goto out;
L
Linus Torvalds 已提交
101
		}
102

J
Jan Kara 已提交
103 104 105 106
		if (!(offset & ((16 >> (sb->s_blocksize_bits - 9)) - 1))) {
			i = 16 >> (sb->s_blocksize_bits - 9);
			if (i + offset > (elen >> sb->s_blocksize_bits))
				i = (elen >> sb->s_blocksize_bits) - offset;
107
			for (num = 0; i > 0; i--) {
J
Jan Kara 已提交
108 109
				block = udf_get_lb_pblock(sb, &eloc, offset + i);
				tmp = udf_tgetblk(sb, block);
110
				if (tmp && !buffer_uptodate(tmp) && !buffer_locked(tmp))
L
Linus Torvalds 已提交
111 112 113 114
					bha[num++] = tmp;
				else
					brelse(tmp);
			}
115
			if (num) {
116
				ll_rw_block(REQ_OP_READ, READA, num, bha);
117
				for (i = 0; i < num; i++)
L
Linus Torvalds 已提交
118 119 120 121 122
					brelse(bha[i]);
			}
		}
	}

123
	while (nf_pos < size) {
A
Al Viro 已提交
124 125 126
		struct kernel_lb_addr tloc;

		ctx->pos = (nf_pos >> 2) + 1;
L
Linus Torvalds 已提交
127

128 129
		fi = udf_fileident_read(dir, &nf_pos, &fibh, &cfi, &epos, &eloc,
					&elen, &offset);
130 131
		if (!fi)
			goto out;
L
Linus Torvalds 已提交
132 133 134 135

		liu = le16_to_cpu(cfi.lengthOfImpUse);
		lfi = cfi.lengthFileIdent;

136
		if (fibh.sbh == fibh.ebh) {
L
Linus Torvalds 已提交
137
			nameptr = fi->fileIdent + liu;
138
		} else {
L
Linus Torvalds 已提交
139 140
			int poffset;	/* Unpaded ending offset */

141
			poffset = fibh.soffset + sizeof(struct fileIdentDesc) + liu + lfi;
L
Linus Torvalds 已提交
142

143 144 145
			if (poffset >= lfi) {
				nameptr = (char *)(fibh.ebh->b_data + poffset - lfi);
			} else {
146 147 148 149 150 151 152 153 154
				if (!copy_name) {
					copy_name = kmalloc(UDF_NAME_LEN,
							    GFP_NOFS);
					if (!copy_name) {
						ret = -ENOMEM;
						goto out;
					}
				}
				nameptr = copy_name;
155 156 157 158
				memcpy(nameptr, fi->fileIdent + liu,
				       lfi - poffset);
				memcpy(nameptr + lfi - poffset,
				       fibh.ebh->b_data, poffset);
L
Linus Torvalds 已提交
159 160 161
			}
		}

162
		if ((cfi.fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
J
Jan Kara 已提交
163
			if (!UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
L
Linus Torvalds 已提交
164 165
				continue;
		}
166 167

		if ((cfi.fileCharacteristics & FID_FILE_CHAR_HIDDEN) != 0) {
J
Jan Kara 已提交
168
			if (!UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
L
Linus Torvalds 已提交
169 170 171
				continue;
		}

172
		if (cfi.fileCharacteristics & FID_FILE_CHAR_PARENT) {
A
Al Viro 已提交
173 174 175
			if (!dir_emit_dotdot(file, ctx))
				goto out;
			continue;
L
Linus Torvalds 已提交
176 177
		}

J
Jan Kara 已提交
178
		flen = udf_get_filename(sb, nameptr, lfi, fname, UDF_NAME_LEN);
179
		if (flen < 0)
A
Al Viro 已提交
180 181 182
			continue;

		tloc = lelb_to_cpu(cfi.icb.extLocation);
J
Jan Kara 已提交
183
		iblock = udf_get_lb_pblock(sb, &tloc, 0);
A
Al Viro 已提交
184
		if (!dir_emit(ctx, fname, flen, iblock, DT_UNKNOWN))
185
			goto out;
186
	} /* end while */
L
Linus Torvalds 已提交
187

A
Al Viro 已提交
188
	ctx->pos = (nf_pos >> 2) + 1;
L
Linus Torvalds 已提交
189

190
out:
L
Linus Torvalds 已提交
191
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
192 193 194
		brelse(fibh.ebh);
	brelse(fibh.sbh);
	brelse(epos.bh);
195
	kfree(fname);
196
	kfree(copy_name);
L
Linus Torvalds 已提交
197

198
	return ret;
L
Linus Torvalds 已提交
199
}
200 201 202

/* readdir and lookup functions */
const struct file_operations udf_dir_operations = {
203
	.llseek			= generic_file_llseek,
204
	.read			= generic_read_dir,
205
	.iterate_shared		= udf_readdir,
J
John Kacur 已提交
206
	.unlocked_ioctl		= udf_ioctl,
207
	.fsync			= generic_file_fsync,
208
};