dir.c 5.0 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 37
/*
 * 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 <linux/buffer_head.h>

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

A
Al Viro 已提交
38 39

static int udf_readdir(struct file *file, struct dir_context *ctx)
L
Linus Torvalds 已提交
40
{
A
Al Viro 已提交
41 42
	struct inode *dir = file_inode(file);
	struct udf_inode_info *iinfo = UDF_I(dir);
43
	struct udf_fileident_bh fibh = { .sbh = NULL, .ebh = NULL};
44
	struct fileIdentDesc *fi = NULL;
L
Linus Torvalds 已提交
45 46
	struct fileIdentDesc cfi;
	int block, iblock;
A
Al Viro 已提交
47
	loff_t nf_pos;
L
Linus Torvalds 已提交
48
	int flen;
A
Al Viro 已提交
49 50
	unsigned char *fname = NULL;
	unsigned char *nameptr;
L
Linus Torvalds 已提交
51 52
	uint16_t liu;
	uint8_t lfi;
J
Jan Kara 已提交
53
	loff_t size = udf_ext0_offset(dir) + dir->i_size;
J
Jan Kara 已提交
54
	struct buffer_head *tmp, *bha[16];
55
	struct kernel_lb_addr eloc;
J
Jan Kara 已提交
56
	uint32_t elen;
57
	sector_t offset;
58
	int i, num, ret = 0;
59
	struct extent_position epos = { NULL, 0, {0, 0} };
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 & (dir->i_sb->s_blocksize - 1);
80 81 82 83 84 85 86
	if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
		if (inode_bmap(dir, nf_pos >> dir->i_sb->s_blocksize_bits,
		    &epos, &eloc, &elen, &offset)
		    != (EXT_RECORDED_ALLOCATED >> 30)) {
			ret = -ENOENT;
			goto out;
		}
87
		block = udf_get_lb_pblock(dir->i_sb, &eloc, offset);
88
		if ((++offset << dir->i_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

98
		if (!(fibh.sbh = fibh.ebh = udf_tread(dir->i_sb, block))) {
99 100
			ret = -EIO;
			goto out;
L
Linus Torvalds 已提交
101
		}
102 103

		if (!(offset & ((16 >> (dir->i_sb->s_blocksize_bits - 9)) - 1))) {
L
Linus Torvalds 已提交
104
			i = 16 >> (dir->i_sb->s_blocksize_bits - 9);
105
			if (i + offset > (elen >> dir->i_sb->s_blocksize_bits))
106
				i = (elen >> dir->i_sb->s_blocksize_bits) - offset;
107
			for (num = 0; i > 0; i--) {
108
				block = udf_get_lb_pblock(dir->i_sb, &eloc, offset + i);
L
Linus Torvalds 已提交
109
				tmp = udf_tgetblk(dir->i_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) {
L
Linus Torvalds 已提交
116
				ll_rw_block(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 {
L
Linus Torvalds 已提交
146
				nameptr = fname;
147 148 149 150
				memcpy(nameptr, fi->fileIdent + liu,
				       lfi - poffset);
				memcpy(nameptr + lfi - poffset,
				       fibh.ebh->b_data, poffset);
L
Linus Torvalds 已提交
151 152 153
			}
		}

154 155
		if ((cfi.fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
			if (!UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNDELETE))
L
Linus Torvalds 已提交
156 157
				continue;
		}
158 159 160

		if ((cfi.fileCharacteristics & FID_FILE_CHAR_HIDDEN) != 0) {
			if (!UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNHIDE))
L
Linus Torvalds 已提交
161 162 163
				continue;
		}

164
		if (cfi.fileCharacteristics & FID_FILE_CHAR_PARENT) {
A
Al Viro 已提交
165 166 167
			if (!dir_emit_dotdot(file, ctx))
				goto out;
			continue;
L
Linus Torvalds 已提交
168 169
		}

170 171
		flen = udf_get_filename(dir->i_sb, nameptr, lfi, fname,
					UDF_NAME_LEN);
A
Al Viro 已提交
172 173 174 175 176 177
		if (!flen)
			continue;

		tloc = lelb_to_cpu(cfi.icb.extLocation);
		iblock = udf_get_lb_pblock(dir->i_sb, &tloc, 0);
		if (!dir_emit(ctx, fname, flen, iblock, DT_UNKNOWN))
178
			goto out;
179
	} /* end while */
L
Linus Torvalds 已提交
180

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

183
out:
L
Linus Torvalds 已提交
184
	if (fibh.sbh != fibh.ebh)
J
Jan Kara 已提交
185 186 187
		brelse(fibh.ebh);
	brelse(fibh.sbh);
	brelse(epos.bh);
188
	kfree(fname);
L
Linus Torvalds 已提交
189

190
	return ret;
L
Linus Torvalds 已提交
191
}
192 193 194

/* readdir and lookup functions */
const struct file_operations udf_dir_operations = {
195
	.llseek			= generic_file_llseek,
196
	.read			= generic_read_dir,
A
Al Viro 已提交
197
	.iterate		= udf_readdir,
J
John Kacur 已提交
198
	.unlocked_ioctl		= udf_ioctl,
199
	.fsync			= generic_file_fsync,
200
};