blob: 8d8643ada1994c55d449f9c63646f1fc71ec597d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * dir.c
3 *
4 * PURPOSE
5 * Directory handling routines for the OSTA-UDF(tm) filesystem.
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 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/smp_lock.h>
34#include <linux/buffer_head.h>
35
36#include "udf_i.h"
37#include "udf_sb.h"
38
Marcin Slusarz934c5e62008-02-08 04:20:47 -080039static int do_udf_readdir(struct inode *dir, struct file *filp,
40 filldir_t filldir, void *dirent)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
42 struct udf_fileident_bh fibh;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070043 struct fileIdentDesc *fi = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 struct fileIdentDesc cfi;
45 int block, iblock;
Jan Karae28d80f2008-02-13 15:03:33 -080046 loff_t nf_pos = (filp->f_pos - 1) << 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 int flen;
48 char fname[UDF_NAME_LEN];
49 char *nameptr;
50 uint16_t liu;
51 uint8_t lfi;
Jan Karae28d80f2008-02-13 15:03:33 -080052 loff_t size = udf_ext0_offset(dir) + dir->i_size;
Jan Karaff116fc2007-05-08 00:35:14 -070053 struct buffer_head *tmp, *bha[16];
54 kernel_lb_addr eloc;
55 uint32_t elen;
Jan Kara60448b12007-05-08 00:35:13 -070056 sector_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 int i, num;
58 unsigned int dt_type;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070059 struct extent_position epos = { NULL, 0, {0, 0} };
Marcin Slusarz48d6d8ff2008-02-08 04:20:44 -080060 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62 if (nf_pos >= size)
63 return 0;
64
65 if (nf_pos == 0)
Jan Karae28d80f2008-02-13 15:03:33 -080066 nf_pos = udf_ext0_offset(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Jan Karae28d80f2008-02-13 15:03:33 -080068 fibh.soffset = fibh.eoffset = nf_pos & (dir->i_sb->s_blocksize - 1);
Marcin Slusarz48d6d8ff2008-02-08 04:20:44 -080069 iinfo = UDF_I(dir);
70 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 fibh.sbh = fibh.ebh = NULL;
Jan Karae28d80f2008-02-13 15:03:33 -080072 } else if (inode_bmap(dir, nf_pos >> dir->i_sb->s_blocksize_bits,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070073 &epos, &eloc, &elen, &offset) == (EXT_RECORDED_ALLOCATED >> 30)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 block = udf_get_lb_pblock(dir->i_sb, eloc, offset);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070075 if ((++offset << dir->i_sb->s_blocksize_bits) < elen) {
Marcin Slusarz48d6d8ff2008-02-08 04:20:44 -080076 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
Jan Karaff116fc2007-05-08 00:35:14 -070077 epos.offset -= sizeof(short_ad);
Marcin Slusarz48d6d8ff2008-02-08 04:20:44 -080078 else if (iinfo->i_alloc_type ==
Marcin Slusarzc0b34432008-02-08 04:20:42 -080079 ICBTAG_FLAG_AD_LONG)
Jan Karaff116fc2007-05-08 00:35:14 -070080 epos.offset -= sizeof(long_ad);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070081 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 offset = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070085 if (!(fibh.sbh = fibh.ebh = udf_tread(dir->i_sb, block))) {
Jan Kara3bf25cb2007-05-08 00:35:16 -070086 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 return -EIO;
88 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070089
90 if (!(offset & ((16 >> (dir->i_sb->s_blocksize_bits - 9)) - 1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 i = 16 >> (dir->i_sb->s_blocksize_bits - 9);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070092 if (i + offset > (elen >> dir->i_sb->s_blocksize_bits))
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070093 i = (elen >> dir->i_sb->s_blocksize_bits) - offset;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070094 for (num = 0; i > 0; i--) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070095 block = udf_get_lb_pblock(dir->i_sb, eloc, offset + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 tmp = udf_tgetblk(dir->i_sb, block);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070097 if (tmp && !buffer_uptodate(tmp) && !buffer_locked(tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 bha[num++] = tmp;
99 else
100 brelse(tmp);
101 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700102 if (num) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 ll_rw_block(READA, num, bha);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700104 for (i = 0; i < num; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 brelse(bha[i]);
106 }
107 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700108 } else {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700109 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return -ENOENT;
111 }
112
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700113 while (nf_pos < size) {
Jan Karae28d80f2008-02-13 15:03:33 -0800114 filp->f_pos = (nf_pos >> 2) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700116 fi = udf_fileident_read(dir, &nf_pos, &fibh, &cfi, &epos, &eloc,
117 &elen, &offset);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700118 if (!fi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700120 brelse(fibh.ebh);
121 brelse(fibh.sbh);
122 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 return 0;
124 }
125
126 liu = le16_to_cpu(cfi.lengthOfImpUse);
127 lfi = cfi.lengthFileIdent;
128
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700129 if (fibh.sbh == fibh.ebh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 nameptr = fi->fileIdent + liu;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700131 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 int poffset; /* Unpaded ending offset */
133
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700134 poffset = fibh.soffset + sizeof(struct fileIdentDesc) + liu + lfi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700136 if (poffset >= lfi) {
137 nameptr = (char *)(fibh.ebh->b_data + poffset - lfi);
138 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 nameptr = fname;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700140 memcpy(nameptr, fi->fileIdent + liu,
141 lfi - poffset);
142 memcpy(nameptr + lfi - poffset,
143 fibh.ebh->b_data, poffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 }
145 }
146
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700147 if ((cfi.fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
148 if (!UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNDELETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 continue;
150 }
151
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700152 if ((cfi.fileCharacteristics & FID_FILE_CHAR_HIDDEN) != 0) {
153 if (!UDF_QUERY_FLAG(dir->i_sb, UDF_FLAG_UNHIDE))
154 continue;
155 }
156
157 if (cfi.fileCharacteristics & FID_FILE_CHAR_PARENT) {
Josef Sipek5096e932006-12-08 02:37:44 -0800158 iblock = parent_ino(filp->f_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 flen = 2;
160 memcpy(fname, "..", flen);
161 dt_type = DT_DIR;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700162 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 kernel_lb_addr tloc = lelb_to_cpu(cfi.icb.extLocation);
164
165 iblock = udf_get_lb_pblock(dir->i_sb, tloc, 0);
166 flen = udf_get_filename(dir->i_sb, nameptr, fname, lfi);
167 dt_type = DT_UNKNOWN;
168 }
169
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700170 if (flen) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700171 if (filldir(dirent, fname, flen, filp->f_pos, iblock, dt_type) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700173 brelse(fibh.ebh);
174 brelse(fibh.sbh);
175 brelse(epos.bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700176 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 }
178 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700179 } /* end while */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Jan Karae28d80f2008-02-13 15:03:33 -0800181 filp->f_pos = (nf_pos >> 2) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 if (fibh.sbh != fibh.ebh)
Jan Kara3bf25cb2007-05-08 00:35:16 -0700184 brelse(fibh.ebh);
185 brelse(fibh.sbh);
186 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 return 0;
189}
Marcin Slusarz934c5e62008-02-08 04:20:47 -0800190
191/*
192 * udf_readdir
193 *
194 * PURPOSE
195 * Read a directory entry.
196 *
197 * DESCRIPTION
198 * Optional - sys_getdents() will return -ENOTDIR if this routine is not
199 * available.
200 *
201 * Refer to sys_getdents() in fs/readdir.c
202 * sys_getdents() -> .
203 *
204 * PRE-CONDITIONS
205 * filp Pointer to directory file.
206 * buf Pointer to directory entry buffer.
207 * filldir Pointer to filldir function.
208 *
209 * POST-CONDITIONS
210 * <return> >=0 on success.
211 *
212 * HISTORY
213 * July 1, 1997 - Andrew E. Mileski
214 * Written, tested, and released.
215 */
216
217static int udf_readdir(struct file *filp, void *dirent, filldir_t filldir)
218{
219 struct inode *dir = filp->f_path.dentry->d_inode;
220 int result;
221
222 lock_kernel();
223
224 if (filp->f_pos == 0) {
225 if (filldir(dirent, ".", 1, filp->f_pos, dir->i_ino, DT_DIR) < 0) {
226 unlock_kernel();
227 return 0;
228 }
229 filp->f_pos++;
230 }
231
232 result = do_udf_readdir(dir, filp, filldir, dirent);
233 unlock_kernel();
234 return result;
235}
236
237/* readdir and lookup functions */
238const struct file_operations udf_dir_operations = {
239 .read = generic_read_dir,
240 .readdir = udf_readdir,
241 .ioctl = udf_ioctl,
242 .fsync = udf_fsync_file,
243};