Blame view

fs/udf/dir.c 4.93 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
  /*
   * dir.c
   *
   * PURPOSE
   *  Directory handling routines for the OSTA-UDF(tm) filesystem.
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
   * COPYRIGHT
   *	This file is distributed under the terms of the GNU General Public
   *	License (GPL). Copies of the GPL can be obtained from:
   *		ftp://prep.ai.mit.edu/pub/gnu/GPL
   *	Each contributing author retains all rights to their own work.
   *
   *  (C) 1998-2004 Ben Fennema
   *
   * HISTORY
   *
   *  10/05/98 dgb  Split directory operations into its own file
   *                Implemented directory reads via do_udf_readdir
   *  10/06/98      Made directory operations work!
   *  11/17/98      Rewrote directory to support ICBTAG_FLAG_AD_LONG
   *  11/25/98 blf  Rewrote directory handling (readdir+lookup) to support reading
   *                across blocks.
   *  12/12/98      Split out the lookup code to namei.c. bulk of directory
   *                code now in directory.c:udf_fileident_read.
   */
  
  #include "udfdecl.h"
  
  #include <linux/string.h>
  #include <linux/errno.h>
  #include <linux/mm.h>
  #include <linux/slab.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
33
34
35
  
  #include "udf_i.h"
  #include "udf_sb.h"
5add2ee19   Al Viro   [readdir] convert...
36
37
  
  static int udf_readdir(struct file *file, struct dir_context *ctx)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
38
  {
5add2ee19   Al Viro   [readdir] convert...
39
40
  	struct inode *dir = file_inode(file);
  	struct udf_inode_info *iinfo = UDF_I(dir);
b80697c14   Jan Kara   udf: Remove decla...
41
  	struct udf_fileident_bh fibh = { .sbh = NULL, .ebh = NULL};
cb00ea352   Cyrill Gorcunov   UDF: coding style...
42
  	struct fileIdentDesc *fi = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
43
44
  	struct fileIdentDesc cfi;
  	int block, iblock;
5add2ee19   Al Viro   [readdir] convert...
45
  	loff_t nf_pos;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
46
  	int flen;
391e8bbd3   Al Viro   sanitize const/si...
47
48
  	unsigned char *fname = NULL;
  	unsigned char *nameptr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
49
50
  	uint16_t liu;
  	uint8_t lfi;
e28d80f18   Jan Kara   udf: fix director...
51
  	loff_t size = udf_ext0_offset(dir) + dir->i_size;
ff116fc8d   Jan Kara   UDF: introduce st...
52
  	struct buffer_head *tmp, *bha[16];
5ca4e4be8   Pekka Enberg   Remove struct typ...
53
  	struct kernel_lb_addr eloc;
ff116fc8d   Jan Kara   UDF: introduce st...
54
  	uint32_t elen;
60448b1d6   Jan Kara   udf: use sector_t...
55
  	sector_t offset;
b80697c14   Jan Kara   udf: Remove decla...
56
  	int i, num, ret = 0;
cb00ea352   Cyrill Gorcunov   UDF: coding style...
57
  	struct extent_position epos = { NULL, 0, {0, 0} };
3ee3039c5   Jan Kara   udf: Reduce repea...
58
  	struct super_block *sb = dir->i_sb;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
59

5add2ee19   Al Viro   [readdir] convert...
60
61
62
63
64
65
  	if (ctx->pos == 0) {
  		if (!dir_emit_dot(file, ctx))
  			return 0;
  		ctx->pos = 1;
  	}
  	nf_pos = (ctx->pos - 1) << 2;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
66
  	if (nf_pos >= size)
b80697c14   Jan Kara   udf: Remove decla...
67
68
69
70
71
72
73
  		goto out;
  
  	fname = kmalloc(UDF_NAME_LEN, GFP_NOFS);
  	if (!fname) {
  		ret = -ENOMEM;
  		goto out;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
74
75
  
  	if (nf_pos == 0)
e28d80f18   Jan Kara   udf: fix director...
76
  		nf_pos = udf_ext0_offset(dir);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
77

3ee3039c5   Jan Kara   udf: Reduce repea...
78
  	fibh.soffset = fibh.eoffset = nf_pos & (sb->s_blocksize - 1);
b80697c14   Jan Kara   udf: Remove decla...
79
  	if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
3ee3039c5   Jan Kara   udf: Reduce repea...
80
  		if (inode_bmap(dir, nf_pos >> sb->s_blocksize_bits,
b80697c14   Jan Kara   udf: Remove decla...
81
82
83
84
85
  		    &epos, &eloc, &elen, &offset)
  		    != (EXT_RECORDED_ALLOCATED >> 30)) {
  			ret = -ENOENT;
  			goto out;
  		}
3ee3039c5   Jan Kara   udf: Reduce repea...
86
87
  		block = udf_get_lb_pblock(sb, &eloc, offset);
  		if ((++offset << sb->s_blocksize_bits) < elen) {
48d6d8ff7   Marcin Slusarz   udf: cache struct...
88
  			if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
5ca4e4be8   Pekka Enberg   Remove struct typ...
89
  				epos.offset -= sizeof(struct short_ad);
48d6d8ff7   Marcin Slusarz   udf: cache struct...
90
  			else if (iinfo->i_alloc_type ==
c0b344385   Marcin Slusarz   udf: remove UDF_I...
91
  					ICBTAG_FLAG_AD_LONG)
5ca4e4be8   Pekka Enberg   Remove struct typ...
92
  				epos.offset -= sizeof(struct long_ad);
28de7948a   Cyrill Gorcunov   UDF: coding style...
93
  		} else {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
94
  			offset = 0;
28de7948a   Cyrill Gorcunov   UDF: coding style...
95
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
96

3ee3039c5   Jan Kara   udf: Reduce repea...
97
  		if (!(fibh.sbh = fibh.ebh = udf_tread(sb, block))) {
b80697c14   Jan Kara   udf: Remove decla...
98
99
  			ret = -EIO;
  			goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
100
  		}
cb00ea352   Cyrill Gorcunov   UDF: coding style...
101

3ee3039c5   Jan Kara   udf: Reduce repea...
102
103
104
105
  		if (!(offset & ((16 >> (sb->s_blocksize_bits - 9)) - 1))) {
  			i = 16 >> (sb->s_blocksize_bits - 9);
  			if (i + offset > (elen >> sb->s_blocksize_bits))
  				i = (elen >> sb->s_blocksize_bits) - offset;
cb00ea352   Cyrill Gorcunov   UDF: coding style...
106
  			for (num = 0; i > 0; i--) {
3ee3039c5   Jan Kara   udf: Reduce repea...
107
108
  				block = udf_get_lb_pblock(sb, &eloc, offset + i);
  				tmp = udf_tgetblk(sb, block);
28de7948a   Cyrill Gorcunov   UDF: coding style...
109
  				if (tmp && !buffer_uptodate(tmp) && !buffer_locked(tmp))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
110
111
112
113
  					bha[num++] = tmp;
  				else
  					brelse(tmp);
  			}
cb00ea352   Cyrill Gorcunov   UDF: coding style...
114
  			if (num) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
115
  				ll_rw_block(READA, num, bha);
cb00ea352   Cyrill Gorcunov   UDF: coding style...
116
  				for (i = 0; i < num; i++)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
117
118
119
  					brelse(bha[i]);
  			}
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
120
  	}
cb00ea352   Cyrill Gorcunov   UDF: coding style...
121
  	while (nf_pos < size) {
5add2ee19   Al Viro   [readdir] convert...
122
123
124
  		struct kernel_lb_addr tloc;
  
  		ctx->pos = (nf_pos >> 2) + 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
125

cb00ea352   Cyrill Gorcunov   UDF: coding style...
126
127
  		fi = udf_fileident_read(dir, &nf_pos, &fibh, &cfi, &epos, &eloc,
  					&elen, &offset);
b80697c14   Jan Kara   udf: Remove decla...
128
129
  		if (!fi)
  			goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
130
131
132
  
  		liu = le16_to_cpu(cfi.lengthOfImpUse);
  		lfi = cfi.lengthFileIdent;
28de7948a   Cyrill Gorcunov   UDF: coding style...
133
  		if (fibh.sbh == fibh.ebh) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
134
  			nameptr = fi->fileIdent + liu;
28de7948a   Cyrill Gorcunov   UDF: coding style...
135
  		} else {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
136
  			int poffset;	/* Unpaded ending offset */
28de7948a   Cyrill Gorcunov   UDF: coding style...
137
  			poffset = fibh.soffset + sizeof(struct fileIdentDesc) + liu + lfi;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
138

28de7948a   Cyrill Gorcunov   UDF: coding style...
139
140
141
  			if (poffset >= lfi) {
  				nameptr = (char *)(fibh.ebh->b_data + poffset - lfi);
  			} else {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
142
  				nameptr = fname;
cb00ea352   Cyrill Gorcunov   UDF: coding style...
143
144
145
146
  				memcpy(nameptr, fi->fileIdent + liu,
  				       lfi - poffset);
  				memcpy(nameptr + lfi - poffset,
  				       fibh.ebh->b_data, poffset);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
147
148
  			}
  		}
cb00ea352   Cyrill Gorcunov   UDF: coding style...
149
  		if ((cfi.fileCharacteristics & FID_FILE_CHAR_DELETED) != 0) {
3ee3039c5   Jan Kara   udf: Reduce repea...
150
  			if (!UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
151
152
  				continue;
  		}
cb00ea352   Cyrill Gorcunov   UDF: coding style...
153
154
  
  		if ((cfi.fileCharacteristics & FID_FILE_CHAR_HIDDEN) != 0) {
3ee3039c5   Jan Kara   udf: Reduce repea...
155
  			if (!UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
156
157
  				continue;
  		}
cb00ea352   Cyrill Gorcunov   UDF: coding style...
158
  		if (cfi.fileCharacteristics & FID_FILE_CHAR_PARENT) {
5add2ee19   Al Viro   [readdir] convert...
159
160
161
  			if (!dir_emit_dotdot(file, ctx))
  				goto out;
  			continue;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
162
  		}
3ee3039c5   Jan Kara   udf: Reduce repea...
163
  		flen = udf_get_filename(sb, nameptr, lfi, fname, UDF_NAME_LEN);
6ce638367   Fabian Frederick   udf: Make udf_get...
164
  		if (flen < 0)
5add2ee19   Al Viro   [readdir] convert...
165
166
167
  			continue;
  
  		tloc = lelb_to_cpu(cfi.icb.extLocation);
3ee3039c5   Jan Kara   udf: Reduce repea...
168
  		iblock = udf_get_lb_pblock(sb, &tloc, 0);
5add2ee19   Al Viro   [readdir] convert...
169
  		if (!dir_emit(ctx, fname, flen, iblock, DT_UNKNOWN))
b80697c14   Jan Kara   udf: Remove decla...
170
  			goto out;
28de7948a   Cyrill Gorcunov   UDF: coding style...
171
  	} /* end while */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
172

5add2ee19   Al Viro   [readdir] convert...
173
  	ctx->pos = (nf_pos >> 2) + 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
174

b80697c14   Jan Kara   udf: Remove decla...
175
  out:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
176
  	if (fibh.sbh != fibh.ebh)
3bf25cb40   Jan Kara   udf: use get_bh()
177
178
179
  		brelse(fibh.ebh);
  	brelse(fibh.sbh);
  	brelse(epos.bh);
b80697c14   Jan Kara   udf: Remove decla...
180
  	kfree(fname);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
181

b80697c14   Jan Kara   udf: Remove decla...
182
  	return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
183
  }
934c5e601   Marcin Slusarz   udf: remove wrong...
184

934c5e601   Marcin Slusarz   udf: remove wrong...
185
186
  /* readdir and lookup functions */
  const struct file_operations udf_dir_operations = {
ca572727d   jan Blunck   fs/: do not fallb...
187
  	.llseek			= generic_file_llseek,
934c5e601   Marcin Slusarz   udf: remove wrong...
188
  	.read			= generic_read_dir,
5add2ee19   Al Viro   [readdir] convert...
189
  	.iterate		= udf_readdir,
2f07a88b3   John Kacur   udf: BKL ioctl pu...
190
  	.unlocked_ioctl		= udf_ioctl,
1b061d924   Christoph Hellwig   rename the generi...
191
  	.fsync			= generic_file_fsync,
934c5e601   Marcin Slusarz   udf: remove wrong...
192
  };