directory.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * directory.c
  3. *
  4. * PURPOSE
  5. * Directory related functions
  6. *
  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.
  12. */
  13. #include "udfdecl.h"
  14. #include "udf_i.h"
  15. #include <linux/fs.h>
  16. #include <linux/string.h>
  17. #include <linux/buffer_head.h>
  18. #if 0
  19. static uint8_t *udf_filead_read(struct inode *dir, uint8_t *tmpad,
  20. uint8_t ad_size, struct kernel_lb_addr fe_loc,
  21. int *pos, int *offset, struct buffer_head **bh,
  22. int *error)
  23. {
  24. int loffset = *offset;
  25. int block;
  26. uint8_t *ad;
  27. int remainder;
  28. *error = 0;
  29. ad = (uint8_t *)(*bh)->b_data + *offset;
  30. *offset += ad_size;
  31. if (!ad) {
  32. brelse(*bh);
  33. *error = 1;
  34. return NULL;
  35. }
  36. if (*offset == dir->i_sb->s_blocksize) {
  37. brelse(*bh);
  38. block = udf_get_lb_pblock(dir->i_sb, fe_loc, ++*pos);
  39. if (!block)
  40. return NULL;
  41. *bh = udf_tread(dir->i_sb, block);
  42. if (!*bh)
  43. return NULL;
  44. } else if (*offset > dir->i_sb->s_blocksize) {
  45. ad = tmpad;
  46. remainder = dir->i_sb->s_blocksize - loffset;
  47. memcpy((uint8_t *)ad, (*bh)->b_data + loffset, remainder);
  48. brelse(*bh);
  49. block = udf_get_lb_pblock(dir->i_sb, fe_loc, ++*pos);
  50. if (!block)
  51. return NULL;
  52. (*bh) = udf_tread(dir->i_sb, block);
  53. if (!*bh)
  54. return NULL;
  55. memcpy((uint8_t *)ad + remainder, (*bh)->b_data,
  56. ad_size - remainder);
  57. *offset = ad_size - remainder;
  58. }
  59. return ad;
  60. }
  61. #endif
  62. struct fileIdentDesc *udf_fileident_read(struct inode *dir, loff_t *nf_pos,
  63. struct udf_fileident_bh *fibh,
  64. struct fileIdentDesc *cfi,
  65. struct extent_position *epos,
  66. struct kernel_lb_addr *eloc, uint32_t *elen,
  67. sector_t *offset)
  68. {
  69. struct fileIdentDesc *fi;
  70. int i, num, block;
  71. struct buffer_head *tmp, *bha[16];
  72. struct udf_inode_info *iinfo = UDF_I(dir);
  73. fibh->soffset = fibh->eoffset;
  74. if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
  75. fi = udf_get_fileident(iinfo->i_ext.i_data -
  76. (iinfo->i_efe ?
  77. sizeof(struct extendedFileEntry) :
  78. sizeof(struct fileEntry)),
  79. dir->i_sb->s_blocksize,
  80. &(fibh->eoffset));
  81. if (!fi)
  82. return NULL;
  83. *nf_pos += fibh->eoffset - fibh->soffset;
  84. memcpy((uint8_t *)cfi, (uint8_t *)fi,
  85. sizeof(struct fileIdentDesc));
  86. return fi;
  87. }
  88. if (fibh->eoffset == dir->i_sb->s_blocksize) {
  89. int lextoffset = epos->offset;
  90. unsigned char blocksize_bits = dir->i_sb->s_blocksize_bits;
  91. if (udf_next_aext(dir, epos, eloc, elen, 1) !=
  92. (EXT_RECORDED_ALLOCATED >> 30))
  93. return NULL;
  94. block = udf_get_lb_pblock(dir->i_sb, eloc, *offset);
  95. (*offset)++;
  96. if ((*offset << blocksize_bits) >= *elen)
  97. *offset = 0;
  98. else
  99. epos->offset = lextoffset;
  100. brelse(fibh->sbh);
  101. fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block);
  102. if (!fibh->sbh)
  103. return NULL;
  104. fibh->soffset = fibh->eoffset = 0;
  105. if (!(*offset & ((16 >> (blocksize_bits - 9)) - 1))) {
  106. i = 16 >> (blocksize_bits - 9);
  107. if (i + *offset > (*elen >> blocksize_bits))
  108. i = (*elen >> blocksize_bits)-*offset;
  109. for (num = 0; i > 0; i--) {
  110. block = udf_get_lb_pblock(dir->i_sb, eloc,
  111. *offset + i);
  112. tmp = udf_tgetblk(dir->i_sb, block);
  113. if (tmp && !buffer_uptodate(tmp) &&
  114. !buffer_locked(tmp))
  115. bha[num++] = tmp;
  116. else
  117. brelse(tmp);
  118. }
  119. if (num) {
  120. ll_rw_block(READA, num, bha);
  121. for (i = 0; i < num; i++)
  122. brelse(bha[i]);
  123. }
  124. }
  125. } else if (fibh->sbh != fibh->ebh) {
  126. brelse(fibh->sbh);
  127. fibh->sbh = fibh->ebh;
  128. }
  129. fi = udf_get_fileident(fibh->sbh->b_data, dir->i_sb->s_blocksize,
  130. &(fibh->eoffset));
  131. if (!fi)
  132. return NULL;
  133. *nf_pos += fibh->eoffset - fibh->soffset;
  134. if (fibh->eoffset <= dir->i_sb->s_blocksize) {
  135. memcpy((uint8_t *)cfi, (uint8_t *)fi,
  136. sizeof(struct fileIdentDesc));
  137. } else if (fibh->eoffset > dir->i_sb->s_blocksize) {
  138. int lextoffset = epos->offset;
  139. if (udf_next_aext(dir, epos, eloc, elen, 1) !=
  140. (EXT_RECORDED_ALLOCATED >> 30))
  141. return NULL;
  142. block = udf_get_lb_pblock(dir->i_sb, eloc, *offset);
  143. (*offset)++;
  144. if ((*offset << dir->i_sb->s_blocksize_bits) >= *elen)
  145. *offset = 0;
  146. else
  147. epos->offset = lextoffset;
  148. fibh->soffset -= dir->i_sb->s_blocksize;
  149. fibh->eoffset -= dir->i_sb->s_blocksize;
  150. fibh->ebh = udf_tread(dir->i_sb, block);
  151. if (!fibh->ebh)
  152. return NULL;
  153. if (sizeof(struct fileIdentDesc) > -fibh->soffset) {
  154. int fi_len;
  155. memcpy((uint8_t *)cfi, (uint8_t *)fi, -fibh->soffset);
  156. memcpy((uint8_t *)cfi - fibh->soffset,
  157. fibh->ebh->b_data,
  158. sizeof(struct fileIdentDesc) + fibh->soffset);
  159. fi_len = (sizeof(struct fileIdentDesc) +
  160. cfi->lengthFileIdent +
  161. le16_to_cpu(cfi->lengthOfImpUse) + 3) & ~3;
  162. *nf_pos += fi_len - (fibh->eoffset - fibh->soffset);
  163. fibh->eoffset = fibh->soffset + fi_len;
  164. } else {
  165. memcpy((uint8_t *)cfi, (uint8_t *)fi,
  166. sizeof(struct fileIdentDesc));
  167. }
  168. }
  169. return fi;
  170. }
  171. struct fileIdentDesc *udf_get_fileident(void *buffer, int bufsize, int *offset)
  172. {
  173. struct fileIdentDesc *fi;
  174. int lengthThisIdent;
  175. uint8_t *ptr;
  176. int padlen;
  177. if ((!buffer) || (!offset)) {
  178. udf_debug("invalidparms\n, buffer=%p, offset=%p\n", buffer,
  179. offset);
  180. return NULL;
  181. }
  182. ptr = buffer;
  183. if ((*offset > 0) && (*offset < bufsize))
  184. ptr += *offset;
  185. fi = (struct fileIdentDesc *)ptr;
  186. if (fi->descTag.tagIdent != cpu_to_le16(TAG_IDENT_FID)) {
  187. udf_debug("0x%x != TAG_IDENT_FID\n",
  188. le16_to_cpu(fi->descTag.tagIdent));
  189. udf_debug("offset: %u sizeof: %lu bufsize: %u\n",
  190. *offset, (unsigned long)sizeof(struct fileIdentDesc),
  191. bufsize);
  192. return NULL;
  193. }
  194. if ((*offset + sizeof(struct fileIdentDesc)) > bufsize)
  195. lengthThisIdent = sizeof(struct fileIdentDesc);
  196. else
  197. lengthThisIdent = sizeof(struct fileIdentDesc) +
  198. fi->lengthFileIdent + le16_to_cpu(fi->lengthOfImpUse);
  199. /* we need to figure padding, too! */
  200. padlen = lengthThisIdent % UDF_NAME_PAD;
  201. if (padlen)
  202. lengthThisIdent += (UDF_NAME_PAD - padlen);
  203. *offset = *offset + lengthThisIdent;
  204. return fi;
  205. }
  206. #if 0
  207. static struct extent_ad *udf_get_fileextent(void *buffer, int bufsize, int *offset)
  208. {
  209. struct extent_ad *ext;
  210. struct fileEntry *fe;
  211. uint8_t *ptr;
  212. if ((!buffer) || (!offset)) {
  213. printk(KERN_ERR "udf: udf_get_fileextent() invalidparms\n");
  214. return NULL;
  215. }
  216. fe = (struct fileEntry *)buffer;
  217. if (fe->descTag.tagIdent != cpu_to_le16(TAG_IDENT_FE)) {
  218. udf_debug("0x%x != TAG_IDENT_FE\n",
  219. le16_to_cpu(fe->descTag.tagIdent));
  220. return NULL;
  221. }
  222. ptr = (uint8_t *)(fe->extendedAttr) +
  223. le32_to_cpu(fe->lengthExtendedAttr);
  224. if ((*offset > 0) && (*offset < le32_to_cpu(fe->lengthAllocDescs)))
  225. ptr += *offset;
  226. ext = (struct extent_ad *)ptr;
  227. *offset = *offset + sizeof(struct extent_ad);
  228. return ext;
  229. }
  230. #endif
  231. struct short_ad *udf_get_fileshortad(uint8_t *ptr, int maxoffset, uint32_t *offset,
  232. int inc)
  233. {
  234. struct short_ad *sa;
  235. if ((!ptr) || (!offset)) {
  236. printk(KERN_ERR "udf: udf_get_fileshortad() invalidparms\n");
  237. return NULL;
  238. }
  239. if ((*offset + sizeof(struct short_ad)) > maxoffset)
  240. return NULL;
  241. else {
  242. sa = (struct short_ad *)ptr;
  243. if (sa->extLength == 0)
  244. return NULL;
  245. }
  246. if (inc)
  247. *offset += sizeof(struct short_ad);
  248. return sa;
  249. }
  250. struct long_ad *udf_get_filelongad(uint8_t *ptr, int maxoffset, uint32_t *offset, int inc)
  251. {
  252. struct long_ad *la;
  253. if ((!ptr) || (!offset)) {
  254. printk(KERN_ERR "udf: udf_get_filelongad() invalidparms\n");
  255. return NULL;
  256. }
  257. if ((*offset + sizeof(struct long_ad)) > maxoffset)
  258. return NULL;
  259. else {
  260. la = (struct long_ad *)ptr;
  261. if (la->extLength == 0)
  262. return NULL;
  263. }
  264. if (inc)
  265. *offset += sizeof(struct long_ad);
  266. return la;
  267. }