Merge with Linux 2.4.0-test5-pre6.
[linux-2.6/linux-mips.git] / fs / udf / dir.c
blob09121f0efdf80fddc259688b60112a32b55a5404
1 /*
2 * dir.c
4 * PURPOSE
5 * Directory handling routines for the OSTA-UDF(tm) filesystem.
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@hootie.lvld.hp.com
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.
18 * (C) 1998-2000 Ben Fennema
20 * HISTORY
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.
32 #include "udfdecl.h"
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/malloc.h>
42 #include <linux/udf_fs.h>
43 #endif
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 *);
49 /* readdir and lookup functions */
51 struct file_operations udf_dir_operations = {
52 read: generic_read_dir,
53 readdir: udf_readdir,
54 ioctl: udf_ioctl,
55 fsync: udf_sync_file,
59 * udf_readdir
61 * PURPOSE
62 * Read a directory entry.
64 * DESCRIPTION
65 * Optional - sys_getdents() will return -ENOTDIR if this routine is not
66 * available.
68 * Refer to sys_getdents() in fs/readdir.c
69 * sys_getdents() -> .
71 * PRE-CONDITIONS
72 * filp Pointer to directory file.
73 * buf Pointer to directory entry buffer.
74 * filldir Pointer to filldir function.
76 * POST-CONDITIONS
77 * <return> >=0 on success.
79 * HISTORY
80 * July 1, 1997 - Andrew E. Mileski
81 * Written, tested, and released.
84 int udf_readdir(struct file *filp, void *dirent, filldir_t filldir)
86 struct inode *dir = filp->f_dentry->d_inode;
87 int result;
89 if (!dir)
90 return -EBADF;
92 if (!S_ISDIR(dir->i_mode))
93 return -ENOTDIR;
95 if ( filp->f_pos == 0 )
97 if (filldir(dirent, ".", 1, filp->f_pos, dir->i_ino))
98 return 0;
101 result = do_udf_readdir(dir, filp, filldir, dirent);
102 UPDATE_ATIME(dir);
103 return result;
106 static int
107 do_udf_readdir(struct inode * dir, struct file *filp, filldir_t filldir, void *dirent)
109 struct udf_fileident_bh fibh;
110 struct FileIdentDesc *fi=NULL;
111 struct FileIdentDesc cfi;
112 int block, iblock;
113 loff_t nf_pos = filp->f_pos;
114 int flen;
115 char fname[255];
116 char *nameptr;
117 Uint16 liu;
118 Uint8 lfi;
119 loff_t size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
120 struct buffer_head * bh = NULL;
121 lb_addr bloc, eloc;
122 Uint32 extoffset, elen, offset;
124 if (nf_pos >= size)
125 return 1;
127 if (nf_pos == 0)
128 nf_pos = (udf_ext0_offset(dir) >> 2);
130 fibh.soffset = fibh.eoffset = (nf_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
131 if (inode_bmap(dir, nf_pos >> (dir->i_sb->s_blocksize_bits - 2),
132 &bloc, &extoffset, &eloc, &elen, &offset, &bh) == EXTENT_RECORDED_ALLOCATED)
134 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
135 if ((++offset << dir->i_sb->s_blocksize_bits) < elen)
137 if (UDF_I_ALLOCTYPE(dir) == ICB_FLAG_AD_SHORT)
138 extoffset -= sizeof(short_ad);
139 else if (UDF_I_ALLOCTYPE(dir) == ICB_FLAG_AD_LONG)
140 extoffset -= sizeof(long_ad);
142 else
143 offset = 0;
145 else
147 udf_release_data(bh);
148 return 0;
151 if (!(fibh.sbh = fibh.ebh = udf_tread(dir->i_sb, block, dir->i_sb->s_blocksize)))
153 udf_release_data(bh);
154 return 0;
157 while ( nf_pos < size )
159 filp->f_pos = nf_pos;
161 fi = udf_fileident_read(dir, &nf_pos, &fibh, &cfi, &bloc, &extoffset, &eloc, &elen, &offset, &bh);
163 if (!fi)
165 if (fibh.sbh != fibh.ebh)
166 udf_release_data(fibh.ebh);
167 udf_release_data(fibh.sbh);
168 udf_release_data(bh);
169 return 1;
172 liu = le16_to_cpu(cfi.lengthOfImpUse);
173 lfi = cfi.lengthFileIdent;
175 if (fibh.sbh == fibh.ebh)
176 nameptr = fi->fileIdent + liu;
177 else
179 int poffset; /* Unpaded ending offset */
181 poffset = fibh.soffset + sizeof(struct FileIdentDesc) + liu + lfi;
183 if (poffset >= lfi)
184 nameptr = (char *)(fibh.ebh->b_data + poffset - lfi);
185 else
187 nameptr = fname;
188 memcpy(nameptr, fi->fileIdent + liu, lfi - poffset);
189 memcpy(nameptr + lfi - poffset, fibh.ebh->b_data, poffset);
193 if ( (cfi.fileCharacteristics & FILE_DELETED) != 0 )
195 if ( !UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNDELETE) )
196 continue;
199 if ( (cfi.fileCharacteristics & FILE_HIDDEN) != 0 )
201 if ( !UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNHIDE) )
202 continue;
205 iblock = udf_get_lb_pblock(dir->i_sb, lelb_to_cpu(cfi.icb.extLocation), 0);
207 if (!lfi) /* parent directory */
209 if (filldir(dirent, "..", 2, filp->f_pos, filp->f_dentry->d_parent->d_inode->i_ino))
211 if (fibh.sbh != fibh.ebh)
212 udf_release_data(fibh.ebh);
213 udf_release_data(fibh.sbh);
214 udf_release_data(bh);
215 return 1;
218 else
220 if ((flen = udf_get_filename(nameptr, fname, lfi)))
222 if (filldir(dirent, fname, flen, filp->f_pos, iblock))
224 if (fibh.sbh != fibh.ebh)
225 udf_release_data(fibh.ebh);
226 udf_release_data(fibh.sbh);
227 udf_release_data(bh);
228 return 1; /* halt enum */
232 } /* end while */
234 filp->f_pos = nf_pos;
236 if (fibh.sbh != fibh.ebh)
237 udf_release_data(fibh.ebh);
238 udf_release_data(fibh.sbh);
239 udf_release_data(bh);
241 if ( filp->f_pos >= size)
242 return 1;
243 else
244 return 0;