18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * directory.c 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * PURPOSE 58c2ecf20Sopenharmony_ci * Directory related functions 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * COPYRIGHT 88c2ecf20Sopenharmony_ci * This file is distributed under the terms of the GNU General Public 98c2ecf20Sopenharmony_ci * License (GPL). Copies of the GPL can be obtained from: 108c2ecf20Sopenharmony_ci * ftp://prep.ai.mit.edu/pub/gnu/GPL 118c2ecf20Sopenharmony_ci * Each contributing author retains all rights to their own work. 128c2ecf20Sopenharmony_ci */ 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci#include "udfdecl.h" 158c2ecf20Sopenharmony_ci#include "udf_i.h" 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci#include <linux/fs.h> 188c2ecf20Sopenharmony_ci#include <linux/string.h> 198c2ecf20Sopenharmony_ci#include <linux/bio.h> 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_cistruct fileIdentDesc *udf_fileident_read(struct inode *dir, loff_t *nf_pos, 228c2ecf20Sopenharmony_ci struct udf_fileident_bh *fibh, 238c2ecf20Sopenharmony_ci struct fileIdentDesc *cfi, 248c2ecf20Sopenharmony_ci struct extent_position *epos, 258c2ecf20Sopenharmony_ci struct kernel_lb_addr *eloc, uint32_t *elen, 268c2ecf20Sopenharmony_ci sector_t *offset) 278c2ecf20Sopenharmony_ci{ 288c2ecf20Sopenharmony_ci struct fileIdentDesc *fi; 298c2ecf20Sopenharmony_ci int i, num; 308c2ecf20Sopenharmony_ci udf_pblk_t block; 318c2ecf20Sopenharmony_ci struct buffer_head *tmp, *bha[16]; 328c2ecf20Sopenharmony_ci struct udf_inode_info *iinfo = UDF_I(dir); 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci fibh->soffset = fibh->eoffset; 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) { 378c2ecf20Sopenharmony_ci fi = udf_get_fileident(iinfo->i_data - 388c2ecf20Sopenharmony_ci (iinfo->i_efe ? 398c2ecf20Sopenharmony_ci sizeof(struct extendedFileEntry) : 408c2ecf20Sopenharmony_ci sizeof(struct fileEntry)), 418c2ecf20Sopenharmony_ci dir->i_sb->s_blocksize, 428c2ecf20Sopenharmony_ci &(fibh->eoffset)); 438c2ecf20Sopenharmony_ci if (!fi) 448c2ecf20Sopenharmony_ci return NULL; 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci *nf_pos += fibh->eoffset - fibh->soffset; 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci memcpy((uint8_t *)cfi, (uint8_t *)fi, 498c2ecf20Sopenharmony_ci sizeof(struct fileIdentDesc)); 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci return fi; 528c2ecf20Sopenharmony_ci } 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci if (fibh->eoffset == dir->i_sb->s_blocksize) { 558c2ecf20Sopenharmony_ci uint32_t lextoffset = epos->offset; 568c2ecf20Sopenharmony_ci unsigned char blocksize_bits = dir->i_sb->s_blocksize_bits; 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci if (udf_next_aext(dir, epos, eloc, elen, 1) != 598c2ecf20Sopenharmony_ci (EXT_RECORDED_ALLOCATED >> 30)) 608c2ecf20Sopenharmony_ci return NULL; 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci block = udf_get_lb_pblock(dir->i_sb, eloc, *offset); 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci (*offset)++; 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci if ((*offset << blocksize_bits) >= *elen) 678c2ecf20Sopenharmony_ci *offset = 0; 688c2ecf20Sopenharmony_ci else 698c2ecf20Sopenharmony_ci epos->offset = lextoffset; 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci brelse(fibh->sbh); 728c2ecf20Sopenharmony_ci fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block); 738c2ecf20Sopenharmony_ci if (!fibh->sbh) 748c2ecf20Sopenharmony_ci return NULL; 758c2ecf20Sopenharmony_ci fibh->soffset = fibh->eoffset = 0; 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci if (!(*offset & ((16 >> (blocksize_bits - 9)) - 1))) { 788c2ecf20Sopenharmony_ci i = 16 >> (blocksize_bits - 9); 798c2ecf20Sopenharmony_ci if (i + *offset > (*elen >> blocksize_bits)) 808c2ecf20Sopenharmony_ci i = (*elen >> blocksize_bits)-*offset; 818c2ecf20Sopenharmony_ci for (num = 0; i > 0; i--) { 828c2ecf20Sopenharmony_ci block = udf_get_lb_pblock(dir->i_sb, eloc, 838c2ecf20Sopenharmony_ci *offset + i); 848c2ecf20Sopenharmony_ci tmp = udf_tgetblk(dir->i_sb, block); 858c2ecf20Sopenharmony_ci if (tmp && !buffer_uptodate(tmp) && 868c2ecf20Sopenharmony_ci !buffer_locked(tmp)) 878c2ecf20Sopenharmony_ci bha[num++] = tmp; 888c2ecf20Sopenharmony_ci else 898c2ecf20Sopenharmony_ci brelse(tmp); 908c2ecf20Sopenharmony_ci } 918c2ecf20Sopenharmony_ci if (num) { 928c2ecf20Sopenharmony_ci ll_rw_block(REQ_OP_READ, REQ_RAHEAD, num, bha); 938c2ecf20Sopenharmony_ci for (i = 0; i < num; i++) 948c2ecf20Sopenharmony_ci brelse(bha[i]); 958c2ecf20Sopenharmony_ci } 968c2ecf20Sopenharmony_ci } 978c2ecf20Sopenharmony_ci } else if (fibh->sbh != fibh->ebh) { 988c2ecf20Sopenharmony_ci brelse(fibh->sbh); 998c2ecf20Sopenharmony_ci fibh->sbh = fibh->ebh; 1008c2ecf20Sopenharmony_ci } 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci fi = udf_get_fileident(fibh->sbh->b_data, dir->i_sb->s_blocksize, 1038c2ecf20Sopenharmony_ci &(fibh->eoffset)); 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci if (!fi) 1068c2ecf20Sopenharmony_ci return NULL; 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci *nf_pos += fibh->eoffset - fibh->soffset; 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci if (fibh->eoffset <= dir->i_sb->s_blocksize) { 1118c2ecf20Sopenharmony_ci memcpy((uint8_t *)cfi, (uint8_t *)fi, 1128c2ecf20Sopenharmony_ci sizeof(struct fileIdentDesc)); 1138c2ecf20Sopenharmony_ci } else if (fibh->eoffset > dir->i_sb->s_blocksize) { 1148c2ecf20Sopenharmony_ci uint32_t lextoffset = epos->offset; 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci if (udf_next_aext(dir, epos, eloc, elen, 1) != 1178c2ecf20Sopenharmony_ci (EXT_RECORDED_ALLOCATED >> 30)) 1188c2ecf20Sopenharmony_ci return NULL; 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci block = udf_get_lb_pblock(dir->i_sb, eloc, *offset); 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci (*offset)++; 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci if ((*offset << dir->i_sb->s_blocksize_bits) >= *elen) 1258c2ecf20Sopenharmony_ci *offset = 0; 1268c2ecf20Sopenharmony_ci else 1278c2ecf20Sopenharmony_ci epos->offset = lextoffset; 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci fibh->soffset -= dir->i_sb->s_blocksize; 1308c2ecf20Sopenharmony_ci fibh->eoffset -= dir->i_sb->s_blocksize; 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci fibh->ebh = udf_tread(dir->i_sb, block); 1338c2ecf20Sopenharmony_ci if (!fibh->ebh) 1348c2ecf20Sopenharmony_ci return NULL; 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci if (sizeof(struct fileIdentDesc) > -fibh->soffset) { 1378c2ecf20Sopenharmony_ci int fi_len; 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci memcpy((uint8_t *)cfi, (uint8_t *)fi, -fibh->soffset); 1408c2ecf20Sopenharmony_ci memcpy((uint8_t *)cfi - fibh->soffset, 1418c2ecf20Sopenharmony_ci fibh->ebh->b_data, 1428c2ecf20Sopenharmony_ci sizeof(struct fileIdentDesc) + fibh->soffset); 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci fi_len = udf_dir_entry_len(cfi); 1458c2ecf20Sopenharmony_ci *nf_pos += fi_len - (fibh->eoffset - fibh->soffset); 1468c2ecf20Sopenharmony_ci fibh->eoffset = fibh->soffset + fi_len; 1478c2ecf20Sopenharmony_ci } else { 1488c2ecf20Sopenharmony_ci memcpy((uint8_t *)cfi, (uint8_t *)fi, 1498c2ecf20Sopenharmony_ci sizeof(struct fileIdentDesc)); 1508c2ecf20Sopenharmony_ci } 1518c2ecf20Sopenharmony_ci } 1528c2ecf20Sopenharmony_ci /* Got last entry outside of dir size - fs is corrupted! */ 1538c2ecf20Sopenharmony_ci if (*nf_pos > dir->i_size) 1548c2ecf20Sopenharmony_ci return NULL; 1558c2ecf20Sopenharmony_ci return fi; 1568c2ecf20Sopenharmony_ci} 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_cistruct fileIdentDesc *udf_get_fileident(void *buffer, int bufsize, int *offset) 1598c2ecf20Sopenharmony_ci{ 1608c2ecf20Sopenharmony_ci struct fileIdentDesc *fi; 1618c2ecf20Sopenharmony_ci int lengthThisIdent; 1628c2ecf20Sopenharmony_ci uint8_t *ptr; 1638c2ecf20Sopenharmony_ci int padlen; 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci if ((!buffer) || (!offset)) { 1668c2ecf20Sopenharmony_ci udf_debug("invalidparms, buffer=%p, offset=%p\n", 1678c2ecf20Sopenharmony_ci buffer, offset); 1688c2ecf20Sopenharmony_ci return NULL; 1698c2ecf20Sopenharmony_ci } 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ci ptr = buffer; 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci if ((*offset > 0) && (*offset < bufsize)) 1748c2ecf20Sopenharmony_ci ptr += *offset; 1758c2ecf20Sopenharmony_ci fi = (struct fileIdentDesc *)ptr; 1768c2ecf20Sopenharmony_ci if (fi->descTag.tagIdent != cpu_to_le16(TAG_IDENT_FID)) { 1778c2ecf20Sopenharmony_ci udf_debug("0x%x != TAG_IDENT_FID\n", 1788c2ecf20Sopenharmony_ci le16_to_cpu(fi->descTag.tagIdent)); 1798c2ecf20Sopenharmony_ci udf_debug("offset: %d sizeof: %lu bufsize: %d\n", 1808c2ecf20Sopenharmony_ci *offset, (unsigned long)sizeof(struct fileIdentDesc), 1818c2ecf20Sopenharmony_ci bufsize); 1828c2ecf20Sopenharmony_ci return NULL; 1838c2ecf20Sopenharmony_ci } 1848c2ecf20Sopenharmony_ci if ((*offset + sizeof(struct fileIdentDesc)) > bufsize) 1858c2ecf20Sopenharmony_ci lengthThisIdent = sizeof(struct fileIdentDesc); 1868c2ecf20Sopenharmony_ci else 1878c2ecf20Sopenharmony_ci lengthThisIdent = sizeof(struct fileIdentDesc) + 1888c2ecf20Sopenharmony_ci fi->lengthFileIdent + le16_to_cpu(fi->lengthOfImpUse); 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_ci /* we need to figure padding, too! */ 1918c2ecf20Sopenharmony_ci padlen = lengthThisIdent % UDF_NAME_PAD; 1928c2ecf20Sopenharmony_ci if (padlen) 1938c2ecf20Sopenharmony_ci lengthThisIdent += (UDF_NAME_PAD - padlen); 1948c2ecf20Sopenharmony_ci *offset = *offset + lengthThisIdent; 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_ci return fi; 1978c2ecf20Sopenharmony_ci} 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_cistruct short_ad *udf_get_fileshortad(uint8_t *ptr, int maxoffset, uint32_t *offset, 2008c2ecf20Sopenharmony_ci int inc) 2018c2ecf20Sopenharmony_ci{ 2028c2ecf20Sopenharmony_ci struct short_ad *sa; 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ci if ((!ptr) || (!offset)) { 2058c2ecf20Sopenharmony_ci pr_err("%s: invalidparms\n", __func__); 2068c2ecf20Sopenharmony_ci return NULL; 2078c2ecf20Sopenharmony_ci } 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_ci if ((*offset + sizeof(struct short_ad)) > maxoffset) 2108c2ecf20Sopenharmony_ci return NULL; 2118c2ecf20Sopenharmony_ci else { 2128c2ecf20Sopenharmony_ci sa = (struct short_ad *)ptr; 2138c2ecf20Sopenharmony_ci if (sa->extLength == 0) 2148c2ecf20Sopenharmony_ci return NULL; 2158c2ecf20Sopenharmony_ci } 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_ci if (inc) 2188c2ecf20Sopenharmony_ci *offset += sizeof(struct short_ad); 2198c2ecf20Sopenharmony_ci return sa; 2208c2ecf20Sopenharmony_ci} 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_cistruct long_ad *udf_get_filelongad(uint8_t *ptr, int maxoffset, uint32_t *offset, int inc) 2238c2ecf20Sopenharmony_ci{ 2248c2ecf20Sopenharmony_ci struct long_ad *la; 2258c2ecf20Sopenharmony_ci 2268c2ecf20Sopenharmony_ci if ((!ptr) || (!offset)) { 2278c2ecf20Sopenharmony_ci pr_err("%s: invalidparms\n", __func__); 2288c2ecf20Sopenharmony_ci return NULL; 2298c2ecf20Sopenharmony_ci } 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_ci if ((*offset + sizeof(struct long_ad)) > maxoffset) 2328c2ecf20Sopenharmony_ci return NULL; 2338c2ecf20Sopenharmony_ci else { 2348c2ecf20Sopenharmony_ci la = (struct long_ad *)ptr; 2358c2ecf20Sopenharmony_ci if (la->extLength == 0) 2368c2ecf20Sopenharmony_ci return NULL; 2378c2ecf20Sopenharmony_ci } 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_ci if (inc) 2408c2ecf20Sopenharmony_ci *offset += sizeof(struct long_ad); 2418c2ecf20Sopenharmony_ci return la; 2428c2ecf20Sopenharmony_ci} 243