File: /usr/src/linux/fs/udf/dir.c
1 /*
2 * dir.c
3 *
4 * PURPOSE
5 * Directory handling routines for the OSTA-UDF(tm) filesystem.
6 *
7 * CONTACTS
8 * E-mail regarding any portion of the Linux UDF file system should be
9 * directed to the development team mailing list (run by majordomo):
10 * linux_udf@hpesjro.fc.hp.com
11 *
12 * COPYRIGHT
13 * This file is distributed under the terms of the GNU General Public
14 * License (GPL). Copies of the GPL can be obtained from:
15 * ftp://prep.ai.mit.edu/pub/gnu/GPL
16 * Each contributing author retains all rights to their own work.
17 *
18 * (C) 1998-2000 Ben Fennema
19 *
20 * HISTORY
21 *
22 * 10/05/98 dgb Split directory operations into it's own file
23 * Implemented directory reads via do_udf_readdir
24 * 10/06/98 Made directory operations work!
25 * 11/17/98 Rewrote directory to support ICB_FLAG_AD_LONG
26 * 11/25/98 blf Rewrote directory handling (readdir+lookup) to support reading
27 * across blocks.
28 * 12/12/98 Split out the lookup code to namei.c. bulk of directory
29 * code now in directory.c:udf_fileident_read.
30 */
31
32 #include "udfdecl.h"
33
34 #if defined(__linux__) && defined(__KERNEL__)
35 #include <linux/version.h>
36 #include "udf_i.h"
37 #include "udf_sb.h"
38 #include <linux/string.h>
39 #include <linux/errno.h>
40 #include <linux/mm.h>
41 #include <linux/slab.h>
42 #include <linux/udf_fs.h>
43 #endif
44
45 /* Prototypes for file operations */
46 static int udf_readdir(struct file *, void *, filldir_t);
47 static int do_udf_readdir(struct inode *, struct file *, filldir_t, void *);
48
49 /* readdir and lookup functions */
50
51 struct file_operations udf_dir_operations = {
52 read: generic_read_dir,
53 readdir: udf_readdir,
54 ioctl: udf_ioctl,
55 fsync: udf_fsync_file,
56 };
57
58 /*
59 * udf_readdir
60 *
61 * PURPOSE
62 * Read a directory entry.
63 *
64 * DESCRIPTION
65 * Optional - sys_getdents() will return -ENOTDIR if this routine is not
66 * available.
67 *
68 * Refer to sys_getdents() in fs/readdir.c
69 * sys_getdents() -> .
70 *
71 * PRE-CONDITIONS
72 * filp Pointer to directory file.
73 * buf Pointer to directory entry buffer.
74 * filldir Pointer to filldir function.
75 *
76 * POST-CONDITIONS
77 * <return> >=0 on success.
78 *
79 * HISTORY
80 * July 1, 1997 - Andrew E. Mileski
81 * Written, tested, and released.
82 */
83
84 int udf_readdir(struct file *filp, void *dirent, filldir_t filldir)
85 {
86 struct inode *dir = filp->f_dentry->d_inode;
87 int result;
88
89 if ( filp->f_pos == 0 )
90 {
91 if (filldir(dirent, ".", 1, filp->f_pos, dir->i_ino, DT_DIR) < 0)
92 return 0;
93 filp->f_pos ++;
94 }
95
96 result = do_udf_readdir(dir, filp, filldir, dirent);
97 UPDATE_ATIME(dir);
98 return result;
99 }
100
101 static int
102 do_udf_readdir(struct inode * dir, struct file *filp, filldir_t filldir, void *dirent)
103 {
104 struct udf_fileident_bh fibh;
105 struct FileIdentDesc *fi=NULL;
106 struct FileIdentDesc cfi;
107 int block, iblock;
108 loff_t nf_pos = filp->f_pos - 1;
109 int flen;
110 char fname[255];
111 char *nameptr;
112 Uint16 liu;
113 Uint8 lfi;
114 loff_t size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
115 struct buffer_head * bh = NULL, * tmp, * bha[16];
116 lb_addr bloc, eloc;
117 Uint32 extoffset, elen, offset;
118 int i, num;
119 unsigned int dt_type;
120
121 if (nf_pos >= size)
122 return 0;
123
124 if (nf_pos == 0)
125 nf_pos = (udf_ext0_offset(dir) >> 2);
126
127 fibh.soffset = fibh.eoffset = (nf_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
128 if (inode_bmap(dir, nf_pos >> (dir->i_sb->s_blocksize_bits - 2),
129 &bloc, &extoffset, &eloc, &elen, &offset, &bh) == EXTENT_RECORDED_ALLOCATED)
130 {
131 offset >>= dir->i_sb->s_blocksize_bits;
132 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
133 if ((++offset << dir->i_sb->s_blocksize_bits) < elen)
134 {
135 if (UDF_I_ALLOCTYPE(dir) == ICB_FLAG_AD_SHORT)
136 extoffset -= sizeof(short_ad);
137 else if (UDF_I_ALLOCTYPE(dir) == ICB_FLAG_AD_LONG)
138 extoffset -= sizeof(long_ad);
139 }
140 else
141 offset = 0;
142 }
143 else
144 {
145 udf_release_data(bh);
146 return -ENOENT;
147 }
148
149 if (!(fibh.sbh = fibh.ebh = udf_tread(dir->i_sb, block, dir->i_sb->s_blocksize)))
150 {
151 udf_release_data(bh);
152 return -EIO;
153 }
154
155 if (!(offset & ((16 >> (dir->i_sb->s_blocksize_bits - 9))-1)))
156 {
157 i = 16 >> (dir->i_sb->s_blocksize_bits - 9);
158 if (i+offset > (elen >> dir->i_sb->s_blocksize_bits))
159 i = (elen >> dir->i_sb->s_blocksize_bits)-offset;
160 for (num=0; i>0; i--)
161 {
162 block = udf_get_lb_pblock(dir->i_sb, eloc, offset+i);
163 tmp = udf_tgetblk(dir->i_sb, block, dir->i_sb->s_blocksize);
164 if (tmp && !buffer_uptodate(tmp) && !buffer_locked(tmp))
165 bha[num++] = tmp;
166 else
167 brelse(tmp);
168 }
169 if (num)
170 {
171 ll_rw_block(READA, num, bha);
172 for (i=0; i<num; i++)
173 brelse(bha[i]);
174 }
175 }
176
177 while ( nf_pos < size )
178 {
179 filp->f_pos = nf_pos + 1;
180
181 fi = udf_fileident_read(dir, &nf_pos, &fibh, &cfi, &bloc, &extoffset, &eloc, &elen, &offset, &bh);
182
183 if (!fi)
184 {
185 if (fibh.sbh != fibh.ebh)
186 udf_release_data(fibh.ebh);
187 udf_release_data(fibh.sbh);
188 udf_release_data(bh);
189 return -ENOENT;
190 }
191
192 liu = le16_to_cpu(cfi.lengthOfImpUse);
193 lfi = cfi.lengthFileIdent;
194
195 if (fibh.sbh == fibh.ebh)
196 nameptr = fi->fileIdent + liu;
197 else
198 {
199 int poffset; /* Unpaded ending offset */
200
201 poffset = fibh.soffset + sizeof(struct FileIdentDesc) + liu + lfi;
202
203 if (poffset >= lfi)
204 nameptr = (char *)(fibh.ebh->b_data + poffset - lfi);
205 else
206 {
207 nameptr = fname;
208 memcpy(nameptr, fi->fileIdent + liu, lfi - poffset);
209 memcpy(nameptr + lfi - poffset, fibh.ebh->b_data, poffset);
210 }
211 }
212
213 if ( (cfi.fileCharacteristics & FILE_DELETED) != 0 )
214 {
215 if ( !UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNDELETE) )
216 continue;
217 }
218
219 if ( (cfi.fileCharacteristics & FILE_HIDDEN) != 0 )
220 {
221 if ( !UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNHIDE) )
222 continue;
223 }
224
225 if ( cfi.fileCharacteristics & FILE_PARENT )
226 {
227 iblock = udf_get_lb_pblock(dir->i_sb, UDF_I_LOCATION(filp->f_dentry->d_parent->d_inode), 0);
228 flen = 2;
229 memcpy(fname, "..", flen);
230 dt_type = DT_DIR;
231 }
232 else
233 {
234 iblock = udf_get_lb_pblock(dir->i_sb, lelb_to_cpu(cfi.icb.extLocation), 0);
235 flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi);
236 dt_type = DT_UNKNOWN;
237 }
238
239 if (flen)
240 {
241 if (filldir(dirent, fname, flen, filp->f_pos, iblock, dt_type) < 0)
242 {
243 if (fibh.sbh != fibh.ebh)
244 udf_release_data(fibh.ebh);
245 udf_release_data(fibh.sbh);
246 udf_release_data(bh);
247 return 0;
248 }
249 }
250 } /* end while */
251
252 filp->f_pos = nf_pos + 1;
253
254 if (fibh.sbh != fibh.ebh)
255 udf_release_data(fibh.ebh);
256 udf_release_data(fibh.sbh);
257 udf_release_data(bh);
258
259 return 0;
260 }
261