1 /*
2  * dir.c
3  *
4  * PURPOSE
5  *  Directory handling routines for the OSTA-UDF(tm) filesystem.
6  *
7  * COPYRIGHT
8  *	This file is distributed under the terms of the GNU General Public
9  *	License (GPL). Copies of the GPL can be obtained from:
10  *		ftp://prep.ai.mit.edu/pub/gnu/GPL
11  *	Each contributing author retains all rights to their own work.
12  *
13  *  (C) 1998-2004 Ben Fennema
14  *
15  * HISTORY
16  *
17  *  10/05/98 dgb  Split directory operations into its own file
18  *                Implemented directory reads via do_udf_readdir
19  *  10/06/98      Made directory operations work!
20  *  11/17/98      Rewrote directory to support ICBTAG_FLAG_AD_LONG
21  *  11/25/98 blf  Rewrote directory handling (readdir+lookup) to support reading
22  *                across blocks.
23  *  12/12/98      Split out the lookup code to namei.c. bulk of directory
24  *                code now in directory.c:udf_fileident_read.
25  */
26 
27 #include "udfdecl.h"
28 
29 #include <linux/string.h>
30 #include <linux/errno.h>
31 #include <linux/mm.h>
32 #include <linux/slab.h>
33 #include <linux/bio.h>
34 #include <linux/iversion.h>
35 
36 #include "udf_i.h"
37 #include "udf_sb.h"
38 
39 
udf_readdir(struct file * file,struct dir_context * ctx)40 static int udf_readdir(struct file *file, struct dir_context *ctx)
41 {
42 	struct inode *dir = file_inode(file);
43 	struct udf_inode_info *iinfo = UDF_I(dir);
44 	struct udf_fileident_bh fibh = { .sbh = NULL, .ebh = NULL};
45 	struct fileIdentDesc *fi = NULL;
46 	struct fileIdentDesc cfi;
47 	udf_pblk_t block, iblock;
48 	loff_t nf_pos, emit_pos = 0;
49 	int flen;
50 	unsigned char *fname = NULL, *copy_name = NULL;
51 	unsigned char *nameptr;
52 	uint16_t liu;
53 	uint8_t lfi;
54 	loff_t size = udf_ext0_offset(dir) + dir->i_size;
55 	struct buffer_head *tmp, *bha[16];
56 	struct kernel_lb_addr eloc;
57 	uint32_t elen;
58 	sector_t offset;
59 	int i, num, ret = 0;
60 	struct extent_position epos = { NULL, 0, {0, 0} };
61 	struct super_block *sb = dir->i_sb;
62 	bool pos_valid = false;
63 
64 	if (ctx->pos == 0) {
65 		if (!dir_emit_dot(file, ctx))
66 			return 0;
67 		ctx->pos = 1;
68 	}
69 	nf_pos = (ctx->pos - 1) << 2;
70 	if (nf_pos >= size)
71 		goto out;
72 
73 	/*
74 	 * Something changed since last readdir (either lseek was called or dir
75 	 * changed)?  We need to verify the position correctly points at the
76 	 * beginning of some dir entry so that the directory parsing code does
77 	 * not get confused. Since UDF does not have any reliable way of
78 	 * identifying beginning of dir entry (names are under user control),
79 	 * we need to scan the directory from the beginning.
80 	 */
81 	if (!inode_eq_iversion(dir, file->f_version)) {
82 		emit_pos = nf_pos;
83 		nf_pos = 0;
84 	} else {
85 		pos_valid = true;
86 	}
87 
88 	fname = kmalloc(UDF_NAME_LEN, GFP_NOFS);
89 	if (!fname) {
90 		ret = -ENOMEM;
91 		goto out;
92 	}
93 
94 	if (nf_pos == 0)
95 		nf_pos = udf_ext0_offset(dir);
96 
97 	fibh.soffset = fibh.eoffset = nf_pos & (sb->s_blocksize - 1);
98 	if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
99 		if (inode_bmap(dir, nf_pos >> sb->s_blocksize_bits,
100 		    &epos, &eloc, &elen, &offset)
101 		    != (EXT_RECORDED_ALLOCATED >> 30)) {
102 			ret = -ENOENT;
103 			goto out;
104 		}
105 		block = udf_get_lb_pblock(sb, &eloc, offset);
106 		if ((++offset << sb->s_blocksize_bits) < elen) {
107 			if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
108 				epos.offset -= sizeof(struct short_ad);
109 			else if (iinfo->i_alloc_type ==
110 					ICBTAG_FLAG_AD_LONG)
111 				epos.offset -= sizeof(struct long_ad);
112 		} else {
113 			offset = 0;
114 		}
115 
116 		if (!(fibh.sbh = fibh.ebh = udf_tread(sb, block))) {
117 			ret = -EIO;
118 			goto out;
119 		}
120 
121 		if (!(offset & ((16 >> (sb->s_blocksize_bits - 9)) - 1))) {
122 			i = 16 >> (sb->s_blocksize_bits - 9);
123 			if (i + offset > (elen >> sb->s_blocksize_bits))
124 				i = (elen >> sb->s_blocksize_bits) - offset;
125 			for (num = 0; i > 0; i--) {
126 				block = udf_get_lb_pblock(sb, &eloc, offset + i);
127 				tmp = udf_tgetblk(sb, block);
128 				if (tmp && !buffer_uptodate(tmp) && !buffer_locked(tmp))
129 					bha[num++] = tmp;
130 				else
131 					brelse(tmp);
132 			}
133 			if (num) {
134 				ll_rw_block(REQ_OP_READ, REQ_RAHEAD, num, bha);
135 				for (i = 0; i < num; i++)
136 					brelse(bha[i]);
137 			}
138 		}
139 	}
140 
141 	while (nf_pos < size) {
142 		struct kernel_lb_addr tloc;
143 		loff_t cur_pos = nf_pos;
144 
145 		/* Update file position only if we got past the current one */
146 		if (nf_pos >= emit_pos) {
147 			ctx->pos = (nf_pos >> 2) + 1;
148 			pos_valid = true;
149 		}
150 
151 		fi = udf_fileident_read(dir, &nf_pos, &fibh, &cfi, &epos, &eloc,
152 					&elen, &offset);
153 		if (!fi)
154 			goto out;
155 		/* Still not at offset where user asked us to read from? */
156 		if (cur_pos < emit_pos)
157 			continue;
158 
159 		liu = le16_to_cpu(cfi.lengthOfImpUse);
160 		lfi = cfi.lengthFileIdent;
161 
162 		if (fibh.sbh == fibh.ebh) {
163 			nameptr = fi->fileIdent + liu;
164 		} else {
165 			int poffset;	/* Unpaded ending offset */
166 
167 			poffset = fibh.soffset + sizeof(struct fileIdentDesc) + liu + lfi;
168 
169 			if (poffset >= lfi) {
170 				nameptr = (char *)(fibh.ebh->b_data + poffset - lfi);
171 			} else {
172 				if (!copy_name) {
173 					copy_name = kmalloc(UDF_NAME_LEN,
174 							    GFP_NOFS);
175 					if (!copy_name) {
176 						ret = -ENOMEM;
177 						goto out;
178 					}
179 				}
180 				nameptr = copy_name;
181 				memcpy(nameptr, fi->fileIdent + liu,
182 				       lfi - poffset);
183 				memcpy(nameptr + lfi - poffset,
184 				       fibh.ebh->b_data, poffset);
185 			}
186 		}
187 
188 		if ((cfi.fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
189 			if (!UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
190 				continue;
191 		}
192 
193 		if ((cfi.fileCharacteristics & FID_FILE_CHAR_HIDDEN) != 0) {
194 			if (!UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
195 				continue;
196 		}
197 
198 		if (cfi.fileCharacteristics & FID_FILE_CHAR_PARENT) {
199 			if (!dir_emit_dotdot(file, ctx))
200 				goto out;
201 			continue;
202 		}
203 
204 		flen = udf_get_filename(sb, nameptr, lfi, fname, UDF_NAME_LEN);
205 		if (flen < 0)
206 			continue;
207 
208 		tloc = lelb_to_cpu(cfi.icb.extLocation);
209 		iblock = udf_get_lb_pblock(sb, &tloc, 0);
210 		if (!dir_emit(ctx, fname, flen, iblock, DT_UNKNOWN))
211 			goto out;
212 	} /* end while */
213 
214 	ctx->pos = (nf_pos >> 2) + 1;
215 	pos_valid = true;
216 
217 out:
218 	if (pos_valid)
219 		file->f_version = inode_query_iversion(dir);
220 	if (fibh.sbh != fibh.ebh)
221 		brelse(fibh.ebh);
222 	brelse(fibh.sbh);
223 	brelse(epos.bh);
224 	kfree(fname);
225 	kfree(copy_name);
226 
227 	return ret;
228 }
229 
230 /* readdir and lookup functions */
231 const struct file_operations udf_dir_operations = {
232 	.llseek			= generic_file_llseek,
233 	.read			= generic_read_dir,
234 	.iterate_shared		= udf_readdir,
235 	.unlocked_ioctl		= udf_ioctl,
236 	.fsync			= generic_file_fsync,
237 };
238