11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * directory.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * PURPOSE 51da177e4SLinus Torvalds * Directory related functions 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * COPYRIGHT 81da177e4SLinus Torvalds * This file is distributed under the terms of the GNU General Public 91da177e4SLinus Torvalds * License (GPL). Copies of the GPL can be obtained from: 101da177e4SLinus Torvalds * ftp://prep.ai.mit.edu/pub/gnu/GPL 111da177e4SLinus Torvalds * Each contributing author retains all rights to their own work. 121da177e4SLinus Torvalds */ 131da177e4SLinus Torvalds 141da177e4SLinus Torvalds #include "udfdecl.h" 151da177e4SLinus Torvalds #include "udf_i.h" 161da177e4SLinus Torvalds 171da177e4SLinus Torvalds #include <linux/fs.h> 181da177e4SLinus Torvalds #include <linux/string.h> 191da177e4SLinus Torvalds #include <linux/buffer_head.h> 201da177e4SLinus Torvalds 211da177e4SLinus Torvalds #if 0 22cb00ea35SCyrill Gorcunov static uint8_t *udf_filead_read(struct inode *dir, uint8_t *tmpad, 23cb00ea35SCyrill Gorcunov uint8_t ad_size, kernel_lb_addr fe_loc, 24cb00ea35SCyrill Gorcunov int *pos, int *offset, struct buffer_head **bh, 25cb00ea35SCyrill Gorcunov int *error) 261da177e4SLinus Torvalds { 271da177e4SLinus Torvalds int loffset = *offset; 281da177e4SLinus Torvalds int block; 291da177e4SLinus Torvalds uint8_t *ad; 301da177e4SLinus Torvalds int remainder; 311da177e4SLinus Torvalds 321da177e4SLinus Torvalds *error = 0; 331da177e4SLinus Torvalds 341da177e4SLinus Torvalds ad = (uint8_t *)(*bh)->b_data + *offset; 351da177e4SLinus Torvalds *offset += ad_size; 361da177e4SLinus Torvalds 37cb00ea35SCyrill Gorcunov if (!ad) { 383bf25cb4SJan Kara brelse(*bh); 391da177e4SLinus Torvalds *error = 1; 401da177e4SLinus Torvalds return NULL; 411da177e4SLinus Torvalds } 421da177e4SLinus Torvalds 43cb00ea35SCyrill Gorcunov if (*offset == dir->i_sb->s_blocksize) { 443bf25cb4SJan Kara brelse(*bh); 451da177e4SLinus Torvalds block = udf_get_lb_pblock(dir->i_sb, fe_loc, ++*pos); 461da177e4SLinus Torvalds if (!block) 471da177e4SLinus Torvalds return NULL; 484b11111aSMarcin Slusarz *bh = udf_tread(dir->i_sb, block); 494b11111aSMarcin Slusarz if (!*bh) 501da177e4SLinus Torvalds return NULL; 51cb00ea35SCyrill Gorcunov } else if (*offset > dir->i_sb->s_blocksize) { 521da177e4SLinus Torvalds ad = tmpad; 531da177e4SLinus Torvalds 541da177e4SLinus Torvalds remainder = dir->i_sb->s_blocksize - loffset; 551da177e4SLinus Torvalds memcpy((uint8_t *)ad, (*bh)->b_data + loffset, remainder); 561da177e4SLinus Torvalds 573bf25cb4SJan Kara brelse(*bh); 581da177e4SLinus Torvalds block = udf_get_lb_pblock(dir->i_sb, fe_loc, ++*pos); 591da177e4SLinus Torvalds if (!block) 601da177e4SLinus Torvalds return NULL; 614b11111aSMarcin Slusarz (*bh) = udf_tread(dir->i_sb, block); 624b11111aSMarcin Slusarz if (!*bh) 631da177e4SLinus Torvalds return NULL; 641da177e4SLinus Torvalds 654b11111aSMarcin Slusarz memcpy((uint8_t *)ad + remainder, (*bh)->b_data, 664b11111aSMarcin Slusarz ad_size - remainder); 671da177e4SLinus Torvalds *offset = ad_size - remainder; 681da177e4SLinus Torvalds } 6928de7948SCyrill Gorcunov 701da177e4SLinus Torvalds return ad; 711da177e4SLinus Torvalds } 721da177e4SLinus Torvalds #endif 731da177e4SLinus Torvalds 74cb00ea35SCyrill Gorcunov struct fileIdentDesc *udf_fileident_read(struct inode *dir, loff_t *nf_pos, 751da177e4SLinus Torvalds struct udf_fileident_bh *fibh, 761da177e4SLinus Torvalds struct fileIdentDesc *cfi, 77ff116fc8SJan Kara struct extent_position *epos, 781da177e4SLinus Torvalds kernel_lb_addr *eloc, uint32_t *elen, 79ff116fc8SJan Kara sector_t *offset) 801da177e4SLinus Torvalds { 811da177e4SLinus Torvalds struct fileIdentDesc *fi; 821da177e4SLinus Torvalds int i, num, block; 831da177e4SLinus Torvalds struct buffer_head *tmp, *bha[16]; 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds fibh->soffset = fibh->eoffset; 861da177e4SLinus Torvalds 87cb00ea35SCyrill Gorcunov if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) { 881da177e4SLinus Torvalds fi = udf_get_fileident(UDF_I_DATA(dir) - 891da177e4SLinus Torvalds (UDF_I_EFE(dir) ? 901da177e4SLinus Torvalds sizeof(struct extendedFileEntry) : 911da177e4SLinus Torvalds sizeof(struct fileEntry)), 924b11111aSMarcin Slusarz dir->i_sb->s_blocksize, 934b11111aSMarcin Slusarz &(fibh->eoffset)); 941da177e4SLinus Torvalds if (!fi) 951da177e4SLinus Torvalds return NULL; 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds *nf_pos += ((fibh->eoffset - fibh->soffset) >> 2); 981da177e4SLinus Torvalds 99cb00ea35SCyrill Gorcunov memcpy((uint8_t *)cfi, (uint8_t *)fi, 100cb00ea35SCyrill Gorcunov sizeof(struct fileIdentDesc)); 1011da177e4SLinus Torvalds 1021da177e4SLinus Torvalds return fi; 1031da177e4SLinus Torvalds } 1041da177e4SLinus Torvalds 105cb00ea35SCyrill Gorcunov if (fibh->eoffset == dir->i_sb->s_blocksize) { 106ff116fc8SJan Kara int lextoffset = epos->offset; 1074b11111aSMarcin Slusarz unsigned char blocksize_bits = dir->i_sb->s_blocksize_bits; 1081da177e4SLinus Torvalds 109ff116fc8SJan Kara if (udf_next_aext(dir, epos, eloc, elen, 1) != 1101da177e4SLinus Torvalds (EXT_RECORDED_ALLOCATED >> 30)) 1111da177e4SLinus Torvalds return NULL; 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds block = udf_get_lb_pblock(dir->i_sb, *eloc, *offset); 1141da177e4SLinus Torvalds 1151da177e4SLinus Torvalds (*offset)++; 1161da177e4SLinus Torvalds 1174b11111aSMarcin Slusarz if ((*offset << blocksize_bits) >= *elen) 1181da177e4SLinus Torvalds *offset = 0; 1191da177e4SLinus Torvalds else 120ff116fc8SJan Kara epos->offset = lextoffset; 1211da177e4SLinus Torvalds 1223bf25cb4SJan Kara brelse(fibh->sbh); 1234b11111aSMarcin Slusarz fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block); 1244b11111aSMarcin Slusarz if (!fibh->sbh) 1251da177e4SLinus Torvalds return NULL; 1261da177e4SLinus Torvalds fibh->soffset = fibh->eoffset = 0; 1271da177e4SLinus Torvalds 1284b11111aSMarcin Slusarz if (!(*offset & ((16 >> (blocksize_bits - 9)) - 1))) { 1294b11111aSMarcin Slusarz i = 16 >> (blocksize_bits - 9); 1304b11111aSMarcin Slusarz if (i + *offset > (*elen >> blocksize_bits)) 1314b11111aSMarcin Slusarz i = (*elen >> blocksize_bits)-*offset; 132cb00ea35SCyrill Gorcunov for (num = 0; i > 0; i--) { 1334b11111aSMarcin Slusarz block = udf_get_lb_pblock(dir->i_sb, *eloc, 1344b11111aSMarcin Slusarz *offset + i); 1351da177e4SLinus Torvalds tmp = udf_tgetblk(dir->i_sb, block); 1364b11111aSMarcin Slusarz if (tmp && !buffer_uptodate(tmp) && 1374b11111aSMarcin Slusarz !buffer_locked(tmp)) 1381da177e4SLinus Torvalds bha[num++] = tmp; 1391da177e4SLinus Torvalds else 1401da177e4SLinus Torvalds brelse(tmp); 1411da177e4SLinus Torvalds } 142cb00ea35SCyrill Gorcunov if (num) { 1431da177e4SLinus Torvalds ll_rw_block(READA, num, bha); 1441da177e4SLinus Torvalds for (i = 0; i < num; i++) 1451da177e4SLinus Torvalds brelse(bha[i]); 1461da177e4SLinus Torvalds } 1471da177e4SLinus Torvalds } 148cb00ea35SCyrill Gorcunov } else if (fibh->sbh != fibh->ebh) { 1493bf25cb4SJan Kara brelse(fibh->sbh); 1501da177e4SLinus Torvalds fibh->sbh = fibh->ebh; 1511da177e4SLinus Torvalds } 1521da177e4SLinus Torvalds 1531da177e4SLinus Torvalds fi = udf_get_fileident(fibh->sbh->b_data, dir->i_sb->s_blocksize, 1541da177e4SLinus Torvalds &(fibh->eoffset)); 1551da177e4SLinus Torvalds 1561da177e4SLinus Torvalds if (!fi) 1571da177e4SLinus Torvalds return NULL; 1581da177e4SLinus Torvalds 1591da177e4SLinus Torvalds *nf_pos += ((fibh->eoffset - fibh->soffset) >> 2); 1601da177e4SLinus Torvalds 161cb00ea35SCyrill Gorcunov if (fibh->eoffset <= dir->i_sb->s_blocksize) { 162cb00ea35SCyrill Gorcunov memcpy((uint8_t *)cfi, (uint8_t *)fi, 163cb00ea35SCyrill Gorcunov sizeof(struct fileIdentDesc)); 164cb00ea35SCyrill Gorcunov } else if (fibh->eoffset > dir->i_sb->s_blocksize) { 165ff116fc8SJan Kara int lextoffset = epos->offset; 1661da177e4SLinus Torvalds 167ff116fc8SJan Kara if (udf_next_aext(dir, epos, eloc, elen, 1) != 1681da177e4SLinus Torvalds (EXT_RECORDED_ALLOCATED >> 30)) 1691da177e4SLinus Torvalds return NULL; 1701da177e4SLinus Torvalds 1711da177e4SLinus Torvalds block = udf_get_lb_pblock(dir->i_sb, *eloc, *offset); 1721da177e4SLinus Torvalds 1731da177e4SLinus Torvalds (*offset)++; 1741da177e4SLinus Torvalds 1751da177e4SLinus Torvalds if ((*offset << dir->i_sb->s_blocksize_bits) >= *elen) 1761da177e4SLinus Torvalds *offset = 0; 1771da177e4SLinus Torvalds else 178ff116fc8SJan Kara epos->offset = lextoffset; 1791da177e4SLinus Torvalds 1801da177e4SLinus Torvalds fibh->soffset -= dir->i_sb->s_blocksize; 1811da177e4SLinus Torvalds fibh->eoffset -= dir->i_sb->s_blocksize; 1821da177e4SLinus Torvalds 1834b11111aSMarcin Slusarz fibh->ebh = udf_tread(dir->i_sb, block); 1844b11111aSMarcin Slusarz if (!fibh->ebh) 1851da177e4SLinus Torvalds return NULL; 1861da177e4SLinus Torvalds 187cb00ea35SCyrill Gorcunov if (sizeof(struct fileIdentDesc) > -fibh->soffset) { 1881da177e4SLinus Torvalds int fi_len; 1891da177e4SLinus Torvalds 1901da177e4SLinus Torvalds memcpy((uint8_t *)cfi, (uint8_t *)fi, -fibh->soffset); 1914b11111aSMarcin Slusarz memcpy((uint8_t *)cfi - fibh->soffset, 1924b11111aSMarcin Slusarz fibh->ebh->b_data, 1931da177e4SLinus Torvalds sizeof(struct fileIdentDesc) + fibh->soffset); 1941da177e4SLinus Torvalds 1954b11111aSMarcin Slusarz fi_len = (sizeof(struct fileIdentDesc) + 1964b11111aSMarcin Slusarz cfi->lengthFileIdent + 1971da177e4SLinus Torvalds le16_to_cpu(cfi->lengthOfImpUse) + 3) & ~3; 1981da177e4SLinus Torvalds 1994b11111aSMarcin Slusarz *nf_pos += (fi_len - (fibh->eoffset - fibh->soffset)) 2004b11111aSMarcin Slusarz >> 2; 2011da177e4SLinus Torvalds fibh->eoffset = fibh->soffset + fi_len; 202cb00ea35SCyrill Gorcunov } else { 203cb00ea35SCyrill Gorcunov memcpy((uint8_t *)cfi, (uint8_t *)fi, 204cb00ea35SCyrill Gorcunov sizeof(struct fileIdentDesc)); 2051da177e4SLinus Torvalds } 2061da177e4SLinus Torvalds } 2071da177e4SLinus Torvalds return fi; 2081da177e4SLinus Torvalds } 2091da177e4SLinus Torvalds 210cb00ea35SCyrill Gorcunov struct fileIdentDesc *udf_get_fileident(void *buffer, int bufsize, int *offset) 2111da177e4SLinus Torvalds { 2121da177e4SLinus Torvalds struct fileIdentDesc *fi; 2131da177e4SLinus Torvalds int lengthThisIdent; 2141da177e4SLinus Torvalds uint8_t *ptr; 2151da177e4SLinus Torvalds int padlen; 2161da177e4SLinus Torvalds 2171da177e4SLinus Torvalds if ((!buffer) || (!offset)) { 218cb00ea35SCyrill Gorcunov udf_debug("invalidparms\n, buffer=%p, offset=%p\n", buffer, 219cb00ea35SCyrill Gorcunov offset); 2201da177e4SLinus Torvalds return NULL; 2211da177e4SLinus Torvalds } 2221da177e4SLinus Torvalds 2231da177e4SLinus Torvalds ptr = buffer; 2241da177e4SLinus Torvalds 2254b11111aSMarcin Slusarz if ((*offset > 0) && (*offset < bufsize)) 2261da177e4SLinus Torvalds ptr += *offset; 2271da177e4SLinus Torvalds fi = (struct fileIdentDesc *)ptr; 228*5e0f0017SMarcin Slusarz if (fi->descTag.tagIdent != cpu_to_le16(TAG_IDENT_FID)) { 2291da177e4SLinus Torvalds udf_debug("0x%x != TAG_IDENT_FID\n", 2301da177e4SLinus Torvalds le16_to_cpu(fi->descTag.tagIdent)); 2311da177e4SLinus Torvalds udf_debug("offset: %u sizeof: %lu bufsize: %u\n", 232cb00ea35SCyrill Gorcunov *offset, (unsigned long)sizeof(struct fileIdentDesc), 233cb00ea35SCyrill Gorcunov bufsize); 2341da177e4SLinus Torvalds return NULL; 2351da177e4SLinus Torvalds } 2364b11111aSMarcin Slusarz if ((*offset + sizeof(struct fileIdentDesc)) > bufsize) 2371da177e4SLinus Torvalds lengthThisIdent = sizeof(struct fileIdentDesc); 2384b11111aSMarcin Slusarz else 2391da177e4SLinus Torvalds lengthThisIdent = sizeof(struct fileIdentDesc) + 2401da177e4SLinus Torvalds fi->lengthFileIdent + le16_to_cpu(fi->lengthOfImpUse); 2411da177e4SLinus Torvalds 2421da177e4SLinus Torvalds /* we need to figure padding, too! */ 2431da177e4SLinus Torvalds padlen = lengthThisIdent % UDF_NAME_PAD; 2441da177e4SLinus Torvalds if (padlen) 2451da177e4SLinus Torvalds lengthThisIdent += (UDF_NAME_PAD - padlen); 2461da177e4SLinus Torvalds *offset = *offset + lengthThisIdent; 2471da177e4SLinus Torvalds 2481da177e4SLinus Torvalds return fi; 2491da177e4SLinus Torvalds } 2501da177e4SLinus Torvalds 2511da177e4SLinus Torvalds #if 0 252cb00ea35SCyrill Gorcunov static extent_ad *udf_get_fileextent(void *buffer, int bufsize, int *offset) 2531da177e4SLinus Torvalds { 2541da177e4SLinus Torvalds extent_ad *ext; 2551da177e4SLinus Torvalds struct fileEntry *fe; 2561da177e4SLinus Torvalds uint8_t *ptr; 2571da177e4SLinus Torvalds 258cb00ea35SCyrill Gorcunov if ((!buffer) || (!offset)) { 2591da177e4SLinus Torvalds printk(KERN_ERR "udf: udf_get_fileextent() invalidparms\n"); 2601da177e4SLinus Torvalds return NULL; 2611da177e4SLinus Torvalds } 2621da177e4SLinus Torvalds 2631da177e4SLinus Torvalds fe = (struct fileEntry *)buffer; 2641da177e4SLinus Torvalds 265*5e0f0017SMarcin Slusarz if (fe->descTag.tagIdent != cpu_to_le16(TAG_IDENT_FE)) { 2661da177e4SLinus Torvalds udf_debug("0x%x != TAG_IDENT_FE\n", 2671da177e4SLinus Torvalds le16_to_cpu(fe->descTag.tagIdent)); 2681da177e4SLinus Torvalds return NULL; 2691da177e4SLinus Torvalds } 2701da177e4SLinus Torvalds 2714b11111aSMarcin Slusarz ptr = (uint8_t *)(fe->extendedAttr) + 2724b11111aSMarcin Slusarz le32_to_cpu(fe->lengthExtendedAttr); 2731da177e4SLinus Torvalds 2744b11111aSMarcin Slusarz if ((*offset > 0) && (*offset < le32_to_cpu(fe->lengthAllocDescs))) 2751da177e4SLinus Torvalds ptr += *offset; 2761da177e4SLinus Torvalds 2771da177e4SLinus Torvalds ext = (extent_ad *)ptr; 2781da177e4SLinus Torvalds 2791da177e4SLinus Torvalds *offset = *offset + sizeof(extent_ad); 2801da177e4SLinus Torvalds return ext; 2811da177e4SLinus Torvalds } 2821da177e4SLinus Torvalds #endif 2831da177e4SLinus Torvalds 284cb00ea35SCyrill Gorcunov short_ad *udf_get_fileshortad(uint8_t *ptr, int maxoffset, int *offset, 285cb00ea35SCyrill Gorcunov int inc) 2861da177e4SLinus Torvalds { 2871da177e4SLinus Torvalds short_ad *sa; 2881da177e4SLinus Torvalds 289cb00ea35SCyrill Gorcunov if ((!ptr) || (!offset)) { 2901da177e4SLinus Torvalds printk(KERN_ERR "udf: udf_get_fileshortad() invalidparms\n"); 2911da177e4SLinus Torvalds return NULL; 2921da177e4SLinus Torvalds } 2931da177e4SLinus Torvalds 2941da177e4SLinus Torvalds if ((*offset < 0) || ((*offset + sizeof(short_ad)) > maxoffset)) 2951da177e4SLinus Torvalds return NULL; 2964b11111aSMarcin Slusarz else { 2974b11111aSMarcin Slusarz sa = (short_ad *)ptr; 2984b11111aSMarcin Slusarz if (sa->extLength == 0) 2991da177e4SLinus Torvalds return NULL; 3004b11111aSMarcin Slusarz } 3011da177e4SLinus Torvalds 3021da177e4SLinus Torvalds if (inc) 3031da177e4SLinus Torvalds *offset += sizeof(short_ad); 3041da177e4SLinus Torvalds return sa; 3051da177e4SLinus Torvalds } 3061da177e4SLinus Torvalds 307cb00ea35SCyrill Gorcunov long_ad *udf_get_filelongad(uint8_t *ptr, int maxoffset, int *offset, int inc) 3081da177e4SLinus Torvalds { 3091da177e4SLinus Torvalds long_ad *la; 3101da177e4SLinus Torvalds 311cb00ea35SCyrill Gorcunov if ((!ptr) || (!offset)) { 3121da177e4SLinus Torvalds printk(KERN_ERR "udf: udf_get_filelongad() invalidparms\n"); 3131da177e4SLinus Torvalds return NULL; 3141da177e4SLinus Torvalds } 3151da177e4SLinus Torvalds 3161da177e4SLinus Torvalds if ((*offset < 0) || ((*offset + sizeof(long_ad)) > maxoffset)) 3171da177e4SLinus Torvalds return NULL; 3184b11111aSMarcin Slusarz else { 3194b11111aSMarcin Slusarz la = (long_ad *)ptr; 3204b11111aSMarcin Slusarz if (la->extLength == 0) 3211da177e4SLinus Torvalds return NULL; 3224b11111aSMarcin Slusarz } 3231da177e4SLinus Torvalds 3241da177e4SLinus Torvalds if (inc) 3251da177e4SLinus Torvalds *offset += sizeof(long_ad); 3261da177e4SLinus Torvalds return la; 3271da177e4SLinus Torvalds } 328