Merge with 2.3.48.
[linux-2.6/linux-mips.git] / fs / udf / dir.c
blobc31ff527a2320b5c6c8b2948e340870b3da2d08f
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-1999 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/config.h>
36 #include <linux/version.h>
37 #include "udf_i.h"
38 #include "udf_sb.h"
39 #include <linux/string.h>
40 #include <linux/errno.h>
41 #include <linux/mm.h>
42 #include <linux/malloc.h>
43 #include <linux/udf_fs.h>
44 #endif
46 /* Prototypes for file operations */
47 static int udf_readdir(struct file *, void *, filldir_t);
48 static int do_udf_readdir(struct inode *, struct file *, filldir_t, void *);
50 /* readdir and lookup functions */
52 struct file_operations udf_dir_operations = {
53 read: generic_read_dir,
54 readdir: udf_readdir,
55 ioctl: udf_ioctl,
56 fsync: udf_sync_file,
60 * udf_readdir
62 * PURPOSE
63 * Read a directory entry.
65 * DESCRIPTION
66 * Optional - sys_getdents() will return -ENOTDIR if this routine is not
67 * available.
69 * Refer to sys_getdents() in fs/readdir.c
70 * sys_getdents() -> .
72 * PRE-CONDITIONS
73 * filp Pointer to directory file.
74 * buf Pointer to directory entry buffer.
75 * filldir Pointer to filldir function.
77 * POST-CONDITIONS
78 * <return> >=0 on success.
80 * HISTORY
81 * July 1, 1997 - Andrew E. Mileski
82 * Written, tested, and released.
85 int udf_readdir(struct file *filp, void *dirent, filldir_t filldir)
87 struct inode *dir = filp->f_dentry->d_inode;
88 int result;
90 if (!dir)
91 return -EBADF;
93 if (!S_ISDIR(dir->i_mode))
94 return -ENOTDIR;
96 if ( filp->f_pos == 0 )
98 if (filldir(dirent, ".", 1, filp->f_pos, dir->i_ino))
99 return 0;
102 result = do_udf_readdir(dir, filp, filldir, dirent);
103 UPDATE_ATIME(dir);
104 return result;
107 static int
108 do_udf_readdir(struct inode * dir, struct file *filp, filldir_t filldir, void *dirent)
110 struct udf_fileident_bh fibh;
111 struct FileIdentDesc *fi=NULL;
112 struct FileIdentDesc cfi;
113 int block, iblock;
114 loff_t nf_pos = filp->f_pos;
115 int flen;
116 char fname[255];
117 char *nameptr;
118 Uint16 liu;
119 Uint8 lfi;
120 loff_t size = (udf_ext0_offset(dir) + dir->i_size) >> 2;
121 struct buffer_head * bh = NULL;
122 lb_addr bloc, eloc;
123 Uint32 extoffset, elen, offset;
125 if (nf_pos >= size)
126 return 1;
128 if (nf_pos == 0)
129 nf_pos = (udf_ext0_offset(dir) >> 2);
131 fibh.soffset = fibh.eoffset = (nf_pos & ((dir->i_sb->s_blocksize - 1) >> 2)) << 2;
132 if (inode_bmap(dir, nf_pos >> (dir->i_sb->s_blocksize_bits - 2),
133 &bloc, &extoffset, &eloc, &elen, &offset, &bh) == EXTENT_RECORDED_ALLOCATED)
135 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
136 if ((++offset << dir->i_sb->s_blocksize_bits) < elen)
138 if (UDF_I_ALLOCTYPE(dir) == ICB_FLAG_AD_SHORT)
139 extoffset -= sizeof(short_ad);
140 else if (UDF_I_ALLOCTYPE(dir) == ICB_FLAG_AD_LONG)
141 extoffset -= sizeof(long_ad);
143 else
144 offset = 0;
146 else
148 udf_release_data(bh);
149 return 0;
152 if (!(fibh.sbh = fibh.ebh = udf_tread(dir->i_sb, block, dir->i_sb->s_blocksize)))
154 udf_release_data(bh);
155 return 0;
158 while ( nf_pos < size )
160 filp->f_pos = nf_pos;
162 fi = udf_fileident_read(dir, &nf_pos, &fibh, &cfi, &bloc, &extoffset, &offset, &bh);
164 if (!fi)
166 if (fibh.sbh != fibh.ebh)
167 udf_release_data(fibh.ebh);
168 udf_release_data(fibh.sbh);
169 udf_release_data(bh);
170 return 1;
173 liu = le16_to_cpu(cfi.lengthOfImpUse);
174 lfi = cfi.lengthFileIdent;
176 if (fibh.sbh == fibh.ebh)
177 nameptr = fi->fileIdent + liu;
178 else
180 int poffset; /* Unpaded ending offset */
182 poffset = fibh.soffset + sizeof(struct FileIdentDesc) + liu + lfi;
184 if (poffset >= lfi)
185 nameptr = (char *)(fibh.ebh->b_data + poffset - lfi);
186 else
188 nameptr = fname;
189 memcpy(nameptr, fi->fileIdent + liu, lfi - poffset);
190 memcpy(nameptr + lfi - poffset, fibh.ebh->b_data, poffset);
194 if ( (cfi.fileCharacteristics & FILE_DELETED) != 0 )
196 if ( !IS_UNDELETE(dir->i_sb) )
197 continue;
200 if ( (cfi.fileCharacteristics & FILE_HIDDEN) != 0 )
202 if ( !IS_UNHIDE(dir->i_sb) )
203 continue;
206 iblock = udf_get_lb_pblock(dir->i_sb, lelb_to_cpu(cfi.icb.extLocation), 0);
208 if (!lfi) /* parent directory */
210 if (filldir(dirent, "..", 2, filp->f_pos, filp->f_dentry->d_parent->d_inode->i_ino))
212 if (fibh.sbh != fibh.ebh)
213 udf_release_data(fibh.ebh);
214 udf_release_data(fibh.sbh);
215 udf_release_data(bh);
216 return 1;
219 else
221 if ((flen = udf_get_filename(nameptr, fname, lfi)))
223 if (filldir(dirent, fname, flen, filp->f_pos, iblock))
225 if (fibh.sbh != fibh.ebh)
226 udf_release_data(fibh.ebh);
227 udf_release_data(fibh.sbh);
228 udf_release_data(bh);
229 return 1; /* halt enum */
233 } /* end while */
235 filp->f_pos = nf_pos;
237 if (fibh.sbh != fibh.ebh)
238 udf_release_data(fibh.ebh);
239 udf_release_data(fibh.sbh);
240 udf_release_data(bh);
242 if ( filp->f_pos >= size)
243 return 1;
244 else
245 return 0;