Merge tag 'char-misc-3.11-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/gregk...
[linux-2.6.git] / fs / udf / directory.c
blob3e44f575fb9cbaf39d8f05bb4594807bb0a7a3be
1 /*
2 * directory.c
4 * PURPOSE
5 * Directory related functions
7 * 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.
14 #include "udfdecl.h"
15 #include "udf_i.h"
17 #include <linux/fs.h>
18 #include <linux/string.h>
19 #include <linux/buffer_head.h>
21 struct fileIdentDesc *udf_fileident_read(struct inode *dir, loff_t *nf_pos,
22 struct udf_fileident_bh *fibh,
23 struct fileIdentDesc *cfi,
24 struct extent_position *epos,
25 struct kernel_lb_addr *eloc, uint32_t *elen,
26 sector_t *offset)
28 struct fileIdentDesc *fi;
29 int i, num, block;
30 struct buffer_head *tmp, *bha[16];
31 struct udf_inode_info *iinfo = UDF_I(dir);
33 fibh->soffset = fibh->eoffset;
35 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
36 fi = udf_get_fileident(iinfo->i_ext.i_data -
37 (iinfo->i_efe ?
38 sizeof(struct extendedFileEntry) :
39 sizeof(struct fileEntry)),
40 dir->i_sb->s_blocksize,
41 &(fibh->eoffset));
42 if (!fi)
43 return NULL;
45 *nf_pos += fibh->eoffset - fibh->soffset;
47 memcpy((uint8_t *)cfi, (uint8_t *)fi,
48 sizeof(struct fileIdentDesc));
50 return fi;
53 if (fibh->eoffset == dir->i_sb->s_blocksize) {
54 int lextoffset = epos->offset;
55 unsigned char blocksize_bits = dir->i_sb->s_blocksize_bits;
57 if (udf_next_aext(dir, epos, eloc, elen, 1) !=
58 (EXT_RECORDED_ALLOCATED >> 30))
59 return NULL;
61 block = udf_get_lb_pblock(dir->i_sb, eloc, *offset);
63 (*offset)++;
65 if ((*offset << blocksize_bits) >= *elen)
66 *offset = 0;
67 else
68 epos->offset = lextoffset;
70 brelse(fibh->sbh);
71 fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block);
72 if (!fibh->sbh)
73 return NULL;
74 fibh->soffset = fibh->eoffset = 0;
76 if (!(*offset & ((16 >> (blocksize_bits - 9)) - 1))) {
77 i = 16 >> (blocksize_bits - 9);
78 if (i + *offset > (*elen >> blocksize_bits))
79 i = (*elen >> blocksize_bits)-*offset;
80 for (num = 0; i > 0; i--) {
81 block = udf_get_lb_pblock(dir->i_sb, eloc,
82 *offset + i);
83 tmp = udf_tgetblk(dir->i_sb, block);
84 if (tmp && !buffer_uptodate(tmp) &&
85 !buffer_locked(tmp))
86 bha[num++] = tmp;
87 else
88 brelse(tmp);
90 if (num) {
91 ll_rw_block(READA, num, bha);
92 for (i = 0; i < num; i++)
93 brelse(bha[i]);
96 } else if (fibh->sbh != fibh->ebh) {
97 brelse(fibh->sbh);
98 fibh->sbh = fibh->ebh;
101 fi = udf_get_fileident(fibh->sbh->b_data, dir->i_sb->s_blocksize,
102 &(fibh->eoffset));
104 if (!fi)
105 return NULL;
107 *nf_pos += fibh->eoffset - fibh->soffset;
109 if (fibh->eoffset <= dir->i_sb->s_blocksize) {
110 memcpy((uint8_t *)cfi, (uint8_t *)fi,
111 sizeof(struct fileIdentDesc));
112 } else if (fibh->eoffset > dir->i_sb->s_blocksize) {
113 int lextoffset = epos->offset;
115 if (udf_next_aext(dir, epos, eloc, elen, 1) !=
116 (EXT_RECORDED_ALLOCATED >> 30))
117 return NULL;
119 block = udf_get_lb_pblock(dir->i_sb, eloc, *offset);
121 (*offset)++;
123 if ((*offset << dir->i_sb->s_blocksize_bits) >= *elen)
124 *offset = 0;
125 else
126 epos->offset = lextoffset;
128 fibh->soffset -= dir->i_sb->s_blocksize;
129 fibh->eoffset -= dir->i_sb->s_blocksize;
131 fibh->ebh = udf_tread(dir->i_sb, block);
132 if (!fibh->ebh)
133 return NULL;
135 if (sizeof(struct fileIdentDesc) > -fibh->soffset) {
136 int fi_len;
138 memcpy((uint8_t *)cfi, (uint8_t *)fi, -fibh->soffset);
139 memcpy((uint8_t *)cfi - fibh->soffset,
140 fibh->ebh->b_data,
141 sizeof(struct fileIdentDesc) + fibh->soffset);
143 fi_len = (sizeof(struct fileIdentDesc) +
144 cfi->lengthFileIdent +
145 le16_to_cpu(cfi->lengthOfImpUse) + 3) & ~3;
147 *nf_pos += fi_len - (fibh->eoffset - fibh->soffset);
148 fibh->eoffset = fibh->soffset + fi_len;
149 } else {
150 memcpy((uint8_t *)cfi, (uint8_t *)fi,
151 sizeof(struct fileIdentDesc));
154 return fi;
157 struct fileIdentDesc *udf_get_fileident(void *buffer, int bufsize, int *offset)
159 struct fileIdentDesc *fi;
160 int lengthThisIdent;
161 uint8_t *ptr;
162 int padlen;
164 if ((!buffer) || (!offset)) {
165 udf_debug("invalidparms, buffer=%p, offset=%p\n",
166 buffer, offset);
167 return NULL;
170 ptr = buffer;
172 if ((*offset > 0) && (*offset < bufsize))
173 ptr += *offset;
174 fi = (struct fileIdentDesc *)ptr;
175 if (fi->descTag.tagIdent != cpu_to_le16(TAG_IDENT_FID)) {
176 udf_debug("0x%x != TAG_IDENT_FID\n",
177 le16_to_cpu(fi->descTag.tagIdent));
178 udf_debug("offset: %u sizeof: %lu bufsize: %u\n",
179 *offset, (unsigned long)sizeof(struct fileIdentDesc),
180 bufsize);
181 return NULL;
183 if ((*offset + sizeof(struct fileIdentDesc)) > bufsize)
184 lengthThisIdent = sizeof(struct fileIdentDesc);
185 else
186 lengthThisIdent = sizeof(struct fileIdentDesc) +
187 fi->lengthFileIdent + le16_to_cpu(fi->lengthOfImpUse);
189 /* we need to figure padding, too! */
190 padlen = lengthThisIdent % UDF_NAME_PAD;
191 if (padlen)
192 lengthThisIdent += (UDF_NAME_PAD - padlen);
193 *offset = *offset + lengthThisIdent;
195 return fi;
198 struct short_ad *udf_get_fileshortad(uint8_t *ptr, int maxoffset, uint32_t *offset,
199 int inc)
201 struct short_ad *sa;
203 if ((!ptr) || (!offset)) {
204 pr_err("%s: invalidparms\n", __func__);
205 return NULL;
208 if ((*offset + sizeof(struct short_ad)) > maxoffset)
209 return NULL;
210 else {
211 sa = (struct short_ad *)ptr;
212 if (sa->extLength == 0)
213 return NULL;
216 if (inc)
217 *offset += sizeof(struct short_ad);
218 return sa;
221 struct long_ad *udf_get_filelongad(uint8_t *ptr, int maxoffset, uint32_t *offset, int inc)
223 struct long_ad *la;
225 if ((!ptr) || (!offset)) {
226 pr_err("%s: invalidparms\n", __func__);
227 return NULL;
230 if ((*offset + sizeof(struct long_ad)) > maxoffset)
231 return NULL;
232 else {
233 la = (struct long_ad *)ptr;
234 if (la->extLength == 0)
235 return NULL;
238 if (inc)
239 *offset += sizeof(struct long_ad);
240 return la;