Blame view

fs/udf/directory.c 5.74 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
  /*
   * directory.c
   *
   * PURPOSE
   *	Directory related functions
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
7
8
9
10
11
12
13
14
15
16
17
18
19
   * 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.
   */
  
  #include "udfdecl.h"
  #include "udf_i.h"
  
  #include <linux/fs.h>
  #include <linux/string.h>
  #include <linux/buffer_head.h>
4b11111ab   Marcin Slusarz   udf: fix coding s...
20
  struct fileIdentDesc *udf_fileident_read(struct inode *dir, loff_t *nf_pos,
cb00ea352   Cyrill Gorcunov   UDF: coding style...
21
22
23
  					 struct udf_fileident_bh *fibh,
  					 struct fileIdentDesc *cfi,
  					 struct extent_position *epos,
5ca4e4be8   Pekka Enberg   Remove struct typ...
24
  					 struct kernel_lb_addr *eloc, uint32_t *elen,
4b11111ab   Marcin Slusarz   udf: fix coding s...
25
  					 sector_t *offset)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
26
27
28
  {
  	struct fileIdentDesc *fi;
  	int i, num, block;
cb00ea352   Cyrill Gorcunov   UDF: coding style...
29
  	struct buffer_head *tmp, *bha[16];
48d6d8ff7   Marcin Slusarz   udf: cache struct...
30
  	struct udf_inode_info *iinfo = UDF_I(dir);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
31
32
  
  	fibh->soffset = fibh->eoffset;
48d6d8ff7   Marcin Slusarz   udf: cache struct...
33
34
35
  	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
  		fi = udf_get_fileident(iinfo->i_ext.i_data -
  				       (iinfo->i_efe ?
cb00ea352   Cyrill Gorcunov   UDF: coding style...
36
37
  					sizeof(struct extendedFileEntry) :
  					sizeof(struct fileEntry)),
4b11111ab   Marcin Slusarz   udf: fix coding s...
38
39
  				       dir->i_sb->s_blocksize,
  				       &(fibh->eoffset));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
40
41
  		if (!fi)
  			return NULL;
af793295b   Jan Kara   udf: cleanup dire...
42
  		*nf_pos += fibh->eoffset - fibh->soffset;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
43

28de7948a   Cyrill Gorcunov   UDF: coding style...
44
  		memcpy((uint8_t *)cfi, (uint8_t *)fi,
cb00ea352   Cyrill Gorcunov   UDF: coding style...
45
  		       sizeof(struct fileIdentDesc));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
46
47
48
  
  		return fi;
  	}
cb00ea352   Cyrill Gorcunov   UDF: coding style...
49
  	if (fibh->eoffset == dir->i_sb->s_blocksize) {
ff116fc8d   Jan Kara   UDF: introduce st...
50
  		int lextoffset = epos->offset;
4b11111ab   Marcin Slusarz   udf: fix coding s...
51
  		unsigned char blocksize_bits = dir->i_sb->s_blocksize_bits;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
52

ff116fc8d   Jan Kara   UDF: introduce st...
53
  		if (udf_next_aext(dir, epos, eloc, elen, 1) !=
cb00ea352   Cyrill Gorcunov   UDF: coding style...
54
  		    (EXT_RECORDED_ALLOCATED >> 30))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
55
  			return NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
56

97e961fdb   Pekka Enberg   Fix the udf code ...
57
  		block = udf_get_lb_pblock(dir->i_sb, eloc, *offset);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
58

cb00ea352   Cyrill Gorcunov   UDF: coding style...
59
  		(*offset)++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
60

4b11111ab   Marcin Slusarz   udf: fix coding s...
61
  		if ((*offset << blocksize_bits) >= *elen)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
62
63
  			*offset = 0;
  		else
ff116fc8d   Jan Kara   UDF: introduce st...
64
  			epos->offset = lextoffset;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
65

3bf25cb40   Jan Kara   udf: use get_bh()
66
  		brelse(fibh->sbh);
4b11111ab   Marcin Slusarz   udf: fix coding s...
67
68
  		fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block);
  		if (!fibh->sbh)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
69
70
  			return NULL;
  		fibh->soffset = fibh->eoffset = 0;
4b11111ab   Marcin Slusarz   udf: fix coding s...
71
72
73
74
  		if (!(*offset & ((16 >> (blocksize_bits - 9)) - 1))) {
  			i = 16 >> (blocksize_bits - 9);
  			if (i + *offset > (*elen >> blocksize_bits))
  				i = (*elen >> blocksize_bits)-*offset;
cb00ea352   Cyrill Gorcunov   UDF: coding style...
75
  			for (num = 0; i > 0; i--) {
97e961fdb   Pekka Enberg   Fix the udf code ...
76
  				block = udf_get_lb_pblock(dir->i_sb, eloc,
4b11111ab   Marcin Slusarz   udf: fix coding s...
77
  							  *offset + i);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
78
  				tmp = udf_tgetblk(dir->i_sb, block);
4b11111ab   Marcin Slusarz   udf: fix coding s...
79
80
  				if (tmp && !buffer_uptodate(tmp) &&
  						!buffer_locked(tmp))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
81
82
83
84
  					bha[num++] = tmp;
  				else
  					brelse(tmp);
  			}
cb00ea352   Cyrill Gorcunov   UDF: coding style...
85
  			if (num) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
86
  				ll_rw_block(READA, num, bha);
cb00ea352   Cyrill Gorcunov   UDF: coding style...
87
  				for (i = 0; i < num; i++)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
88
89
90
  					brelse(bha[i]);
  			}
  		}
cb00ea352   Cyrill Gorcunov   UDF: coding style...
91
  	} else if (fibh->sbh != fibh->ebh) {
3bf25cb40   Jan Kara   udf: use get_bh()
92
  		brelse(fibh->sbh);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
93
94
95
96
  		fibh->sbh = fibh->ebh;
  	}
  
  	fi = udf_get_fileident(fibh->sbh->b_data, dir->i_sb->s_blocksize,
cb00ea352   Cyrill Gorcunov   UDF: coding style...
97
  			       &(fibh->eoffset));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
98
99
100
  
  	if (!fi)
  		return NULL;
af793295b   Jan Kara   udf: cleanup dire...
101
  	*nf_pos += fibh->eoffset - fibh->soffset;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
102

cb00ea352   Cyrill Gorcunov   UDF: coding style...
103
  	if (fibh->eoffset <= dir->i_sb->s_blocksize) {
28de7948a   Cyrill Gorcunov   UDF: coding style...
104
  		memcpy((uint8_t *)cfi, (uint8_t *)fi,
cb00ea352   Cyrill Gorcunov   UDF: coding style...
105
106
  		       sizeof(struct fileIdentDesc));
  	} else if (fibh->eoffset > dir->i_sb->s_blocksize) {
ff116fc8d   Jan Kara   UDF: introduce st...
107
  		int lextoffset = epos->offset;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
108

ff116fc8d   Jan Kara   UDF: introduce st...
109
  		if (udf_next_aext(dir, epos, eloc, elen, 1) !=
cb00ea352   Cyrill Gorcunov   UDF: coding style...
110
  		    (EXT_RECORDED_ALLOCATED >> 30))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
111
  			return NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
112

97e961fdb   Pekka Enberg   Fix the udf code ...
113
  		block = udf_get_lb_pblock(dir->i_sb, eloc, *offset);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
114

cb00ea352   Cyrill Gorcunov   UDF: coding style...
115
  		(*offset)++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
116
117
118
119
  
  		if ((*offset << dir->i_sb->s_blocksize_bits) >= *elen)
  			*offset = 0;
  		else
ff116fc8d   Jan Kara   UDF: introduce st...
120
  			epos->offset = lextoffset;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
121
122
123
  
  		fibh->soffset -= dir->i_sb->s_blocksize;
  		fibh->eoffset -= dir->i_sb->s_blocksize;
4b11111ab   Marcin Slusarz   udf: fix coding s...
124
125
  		fibh->ebh = udf_tread(dir->i_sb, block);
  		if (!fibh->ebh)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
126
  			return NULL;
cb00ea352   Cyrill Gorcunov   UDF: coding style...
127
  		if (sizeof(struct fileIdentDesc) > -fibh->soffset) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
128
  			int fi_len;
28de7948a   Cyrill Gorcunov   UDF: coding style...
129
  			memcpy((uint8_t *)cfi, (uint8_t *)fi, -fibh->soffset);
4b11111ab   Marcin Slusarz   udf: fix coding s...
130
131
  			memcpy((uint8_t *)cfi - fibh->soffset,
  			       fibh->ebh->b_data,
cb00ea352   Cyrill Gorcunov   UDF: coding style...
132
  			       sizeof(struct fileIdentDesc) + fibh->soffset);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
133

4b11111ab   Marcin Slusarz   udf: fix coding s...
134
135
  			fi_len = (sizeof(struct fileIdentDesc) +
  				  cfi->lengthFileIdent +
28de7948a   Cyrill Gorcunov   UDF: coding style...
136
  				  le16_to_cpu(cfi->lengthOfImpUse) + 3) & ~3;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
137

af793295b   Jan Kara   udf: cleanup dire...
138
  			*nf_pos += fi_len - (fibh->eoffset - fibh->soffset);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
139
  			fibh->eoffset = fibh->soffset + fi_len;
cb00ea352   Cyrill Gorcunov   UDF: coding style...
140
  		} else {
28de7948a   Cyrill Gorcunov   UDF: coding style...
141
  			memcpy((uint8_t *)cfi, (uint8_t *)fi,
cb00ea352   Cyrill Gorcunov   UDF: coding style...
142
  			       sizeof(struct fileIdentDesc));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
143
144
145
146
  		}
  	}
  	return fi;
  }
cb00ea352   Cyrill Gorcunov   UDF: coding style...
147
  struct fileIdentDesc *udf_get_fileident(void *buffer, int bufsize, int *offset)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
148
149
150
  {
  	struct fileIdentDesc *fi;
  	int lengthThisIdent;
cb00ea352   Cyrill Gorcunov   UDF: coding style...
151
  	uint8_t *ptr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
152
  	int padlen;
cb00ea352   Cyrill Gorcunov   UDF: coding style...
153
  	if ((!buffer) || (!offset)) {
a983f368f   Joe Perches   udf: Neaten udf_d...
154
155
156
  		udf_debug("invalidparms, buffer=%p, offset=%p
  ",
  			  buffer, offset);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
157
158
159
160
  		return NULL;
  	}
  
  	ptr = buffer;
4b11111ab   Marcin Slusarz   udf: fix coding s...
161
  	if ((*offset > 0) && (*offset < bufsize))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
162
  		ptr += *offset;
cb00ea352   Cyrill Gorcunov   UDF: coding style...
163
  	fi = (struct fileIdentDesc *)ptr;
5e0f00173   Marcin Slusarz   udf: convert byte...
164
  	if (fi->descTag.tagIdent != cpu_to_le16(TAG_IDENT_FID)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
165
166
  		udf_debug("0x%x != TAG_IDENT_FID
  ",
cb00ea352   Cyrill Gorcunov   UDF: coding style...
167
  			  le16_to_cpu(fi->descTag.tagIdent));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
168
169
  		udf_debug("offset: %u sizeof: %lu bufsize: %u
  ",
cb00ea352   Cyrill Gorcunov   UDF: coding style...
170
171
  			  *offset, (unsigned long)sizeof(struct fileIdentDesc),
  			  bufsize);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
172
173
  		return NULL;
  	}
4b11111ab   Marcin Slusarz   udf: fix coding s...
174
  	if ((*offset + sizeof(struct fileIdentDesc)) > bufsize)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
175
  		lengthThisIdent = sizeof(struct fileIdentDesc);
4b11111ab   Marcin Slusarz   udf: fix coding s...
176
  	else
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
177
  		lengthThisIdent = sizeof(struct fileIdentDesc) +
28de7948a   Cyrill Gorcunov   UDF: coding style...
178
  			fi->lengthFileIdent + le16_to_cpu(fi->lengthOfImpUse);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
179
180
181
182
183
184
185
186
187
  
  	/* we need to figure padding, too! */
  	padlen = lengthThisIdent % UDF_NAME_PAD;
  	if (padlen)
  		lengthThisIdent += (UDF_NAME_PAD - padlen);
  	*offset = *offset + lengthThisIdent;
  
  	return fi;
  }
5ca4e4be8   Pekka Enberg   Remove struct typ...
188
  struct short_ad *udf_get_fileshortad(uint8_t *ptr, int maxoffset, uint32_t *offset,
cb00ea352   Cyrill Gorcunov   UDF: coding style...
189
  			      int inc)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
190
  {
5ca4e4be8   Pekka Enberg   Remove struct typ...
191
  	struct short_ad *sa;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
192

cb00ea352   Cyrill Gorcunov   UDF: coding style...
193
  	if ((!ptr) || (!offset)) {
78ace70c4   Joe Perches   udf: Convert prin...
194
195
  		pr_err("%s: invalidparms
  ", __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
196
197
  		return NULL;
  	}
5ca4e4be8   Pekka Enberg   Remove struct typ...
198
  	if ((*offset + sizeof(struct short_ad)) > maxoffset)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
199
  		return NULL;
4b11111ab   Marcin Slusarz   udf: fix coding s...
200
  	else {
5ca4e4be8   Pekka Enberg   Remove struct typ...
201
  		sa = (struct short_ad *)ptr;
4b11111ab   Marcin Slusarz   udf: fix coding s...
202
203
204
  		if (sa->extLength == 0)
  			return NULL;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
205
206
  
  	if (inc)
5ca4e4be8   Pekka Enberg   Remove struct typ...
207
  		*offset += sizeof(struct short_ad);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
208
209
  	return sa;
  }
5ca4e4be8   Pekka Enberg   Remove struct typ...
210
  struct long_ad *udf_get_filelongad(uint8_t *ptr, int maxoffset, uint32_t *offset, int inc)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
211
  {
5ca4e4be8   Pekka Enberg   Remove struct typ...
212
  	struct long_ad *la;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
213

cb00ea352   Cyrill Gorcunov   UDF: coding style...
214
  	if ((!ptr) || (!offset)) {
78ace70c4   Joe Perches   udf: Convert prin...
215
216
  		pr_err("%s: invalidparms
  ", __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
217
218
  		return NULL;
  	}
5ca4e4be8   Pekka Enberg   Remove struct typ...
219
  	if ((*offset + sizeof(struct long_ad)) > maxoffset)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
220
  		return NULL;
4b11111ab   Marcin Slusarz   udf: fix coding s...
221
  	else {
5ca4e4be8   Pekka Enberg   Remove struct typ...
222
  		la = (struct long_ad *)ptr;
4b11111ab   Marcin Slusarz   udf: fix coding s...
223
224
225
  		if (la->extLength == 0)
  			return NULL;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
226
227
  
  	if (inc)
5ca4e4be8   Pekka Enberg   Remove struct typ...
228
  		*offset += sizeof(struct long_ad);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
229
230
  	return la;
  }