CTC++ Coverage Report - Execution Profile    #919/1532

Files Summary | Functions Summary | Execution Profile | Index | No Index
First | Previous | Next | Last


File: fs/fat/dir.c
Instrumentation mode: function-decision-multicondition
TER: 43 % (274/636)

Start/ End/    
True False - Line Source

  1 /*
  2  *  linux/fs/fat/dir.c
  3  *
  4  *  directory handling functions for fat-based filesystems
  5  *
  6  *  Written 1992,1993 by Werner Almesberger
  7  *
  8  *  Hidden files 1995 by Albert Cahalan <albert@ccs.neu.edu> <adc@coe.neu.edu>
  9  *
  10  *  VFAT extensions by Gordon Chaffee <chaffee@plateau.cs.berkeley.edu>
  11  *  Merged with msdos fs by Henrik Storner <storner@osiris.ping.dk>
  12  *  Rewritten for constant inumbers. Plugged buffer overrun in readdir(). AV
  13  *  Short name translation 1999, 2001 by Wolfram Pienkoss <wp@bszh.de>
  14  */
  15 
  16 #include <linux/module.h>
  17 #include <linux/slab.h>
  18 #include <linux/time.h>
  19 #include <linux/msdos_fs.h>
  20 #include <linux/dirent.h>
  21 #include <linux/smp_lock.h>
  22 #include <linux/buffer_head.h>
  23 #include <asm/uaccess.h>
  24 
 
430975   25 static inline loff_t fat_make_i_pos(struct super_block *sb,
  26                 struct buffer_head *bh,
  27                 struct msdos_dir_entry *de)
  28 {
  29    return ((loff_t)bh->b_blocknr << MSDOS_SB(sb)->dir_per_block_bits)
430975    30       | (de - (struct msdos_dir_entry *)bh->b_data);
  31 }
  32 
 
8774E3 310711   33 static inline void fat_dir_readahead(struct inode *dir, sector_t iblock,
  34                  sector_t phys)
  35 {
  36    struct super_block *sb = dir->i_sb;
  37    struct msdos_sb_info *sbi = MSDOS_SB(sb);
  38    struct buffer_head *bh;
  39    int sec;
  40 
  41    /* This is not a first sector of cluster, or sec_per_clus == 1 */
8463E3 310721   42    if ((iblock & (sbi->sec_per_clus - 1)) || sbi->sec_per_clus == 1)
8463E3    42   (T) || _
 - 42   (F) || T
 310721   42   (F) || F
8463E3    43       return;
  44    /* root dir of FAT12/FAT16 */
10 310711   45    if ((sbi->fat_bits != 32) && (dir->i_ino == MSDOS_ROOT_INO))
10    45   (T) && (T)
 111   45   (T) && (F)
 310600   45   (F) && (_)
10    46       return;
  47 
  48    bh = sb_find_get_block(sb, phys);
20780 289931   49    if (bh == NULL || !buffer_uptodate(bh)) {
20708    49   T || _
72    49   F || T
 289931   49   F || F
1317E3 20780   50       for (sec = 0; sec < sbi->sec_per_clus; sec++)
  51          sb_breadahead(sb, phys + sec);
  52    }
  53    brelse(bh);
  54 }
  55 
  56 /* Returns the inode number of the directory entry at offset pos. If bh is
  57    non-NULL, it is brelse'd before. Pos is incremented. The buffer header is
  58    returned in bh.
  59    AV. Most often we do it item-by-item. Makes sense to optimize.
  60    AV. OK, there we go: if both bh and de are non-NULL we assume that we just
  61    AV. want the next entry (took one explicit de=NULL in vfat/namei.c).
  62    AV. It's done in fat_get_entry() (inlined), here the slow case lives.
  63    AV. Additionally, when we return -1 (i.e. reached the end of directory)
  64    AV. we make bh NULL.
  65  */
 
8836E3   66 static int fat__get_entry(struct inode *dir, loff_t *pos,
  67            struct buffer_head **bh, struct msdos_dir_entry **de)
  68 {
  69    struct super_block *sb = dir->i_sb;
  70    sector_t phys, iblock;
  71    unsigned long mapped_blocks;
  72    int err, offset;
  73 
  74 next:
8559E3 276986   75    if (*bh)
  76       brelse(*bh);
  77 
  78    *bh = NULL;
  79    iblock = *pos >> sb->s_blocksize_bits;
  80    err = fat_bmap(dir, iblock, &phys, &mapped_blocks);
61725 8774E3   81    if (err || !phys)
 - 81   T || _
61725    81   F || T
 8774E3   81   F || F
61725    82       return -1;   /* beyond EOF or error */
  83 
  84    fat_dir_readahead(dir, iblock, phys);
  85 
  86    *bh = sb_bread(sb, phys);
8774E3 - 87    if (*bh == NULL) {
  88       printk(KERN_ERR "FAT: Directory bread(block %llu) failed\n",
  89              (unsigned long long)phys);
  90       /* skip this block */
  91       *pos = (iblock + 1) << sb->s_blocksize_bits;
 - 92       goto next;
  93    }
  94 
  95    offset = *pos & (sb->s_blocksize - 1);
  96    *pos += sizeof(struct msdos_dir_entry);
  97    *de = (struct msdos_dir_entry *)((*bh)->b_data + offset);
  98 
8774E3    99    return 0;
  100 }
  101 
 
1387E5   102 static inline int fat_get_entry(struct inode *dir, loff_t *pos,
  103             struct buffer_head **bh,
  104             struct msdos_dir_entry **de)
  105 {
  106    /* Fast stuff first */
  107    if (*bh && *de &&
1299E5 8836E3   108        (*de - (struct msdos_dir_entry *)(*bh)->b_data) < MSDOS_SB(dir->i_sb)->dir_per_block - 1) {
1299E5    108   T && T && T
 8559E3   108   T && T && F
 - 108   T && F && _
 276986   108   F && _ && _
  109       *pos += sizeof(struct msdos_dir_entry);
  110       (*de)++;
1299E5    111       return 0;
  112    }
8836E3    113    return fat__get_entry(dir, pos, bh, de);
  114 }
  115 
  116 /*
  117  * Convert Unicode 16 to UTF8, translated Unicode, or ASCII.
  118  * If uni_xlate is enabled and we can't get a 1:1 conversion, use a
  119  * colon as an escape character since it is normally invalid on the vfat
  120  * filesystem. The following four characters are the hexadecimal digits
  121  * of Unicode value. This lets us do a full dump and restore of Unicode
  122  * filenames. We could get into some trouble with long Unicode names,
  123  * but ignore that right now.
  124  * Ahem... Stack smashing in ring 0 isn't fun. Fixed.
  125  */
 
8723E4   126 static int uni16_to_x8(unsigned char *ascii, wchar_t *uni, int uni_xlate,
  127              struct nls_table *nls)
  128 {
  129    wchar_t *ip, ec;
  130    unsigned char *op, nc;
  131    int charlen;
  132    int k;
  133 
  134    ip = uni;
  135    op = ascii;
  136 
9319E5 8723E4   137    while (*ip) {
  138       ec = *ip++;
9319E5 - 139       if ( (charlen = nls->uni2char(ec, op, NLS_MAX_CHARSET_SIZE)) > 0) {
  140          op += charlen;
    141       } else {
- 142          if (uni_xlate == 1) {
  143             *op = ':';
- 144             for (k = 4; k > 0; k--) {
  145                nc = ec & 0xF;
    146                op[k] = nc > 9   ? nc + ('a' - 10)
- 146         ternary-?: nc > 9
  147                      : nc + '0';
  148                ec >>= 4;
  149             }
  150             op += 5;
    151          } else {
  152             *op++ = '?';
  153          }
  154       }
  155       /* We have some slack there, so it's OK */
9319E5 - 156       if (op>ascii+256) {
  157          op = ascii + 256;
 - 158          break;
  159       }
  160    }
  161    *op = 0;
8723E4    162    return (op - ascii);
  163 }
  164 
  165 static inline int
 
9736E5   166 fat_short2uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
  167 {
  168    int charlen;
  169 
  170    charlen = t->char2uni(c, clen, uni);
9736E5 - 171    if (charlen < 0) {
  172       *uni = 0x003f;   /* a question mark */
  173       charlen = 1;
  174    }
9736E5    175    return charlen;
  176 }
  177 
  178 static inline int
 
61226   179 fat_short2lower_uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
  180 {
  181    int charlen;
  182    wchar_t wc;
  183 
  184    charlen = t->char2uni(c, clen, &wc);
61226 - 185    if (charlen < 0) {
  186       *uni = 0x003f;   /* a question mark */
  187       charlen = 1;
61226 - 188    } else if (charlen <= 1) {
  189       unsigned char nc = t->charset2lower[*c];
  190 
61226 - 191       if (!nc)
  192          nc = *c;
  193 
61226 - 194       if ( (charlen = t->char2uni(&nc, 1, uni)) < 0) {
  195          *uni = 0x003f;   /* a question mark */
  196          charlen = 1;
  197       }
    198    } else
  199       *uni = wc;
  200 
61226    201    return charlen;
  202 }
  203 
  204 static inline int
 
8925E5   205 fat_shortname2uni(struct nls_table *nls, unsigned char *buf, int buf_size,
  206         wchar_t *uni_buf, unsigned short opt, int lower)
  207 {
  208    int len = 0;
  209 
61226 8925E5   210    if (opt & VFAT_SFN_DISPLAY_LOWER)
  211       len =  fat_short2lower_uni(nls, buf, buf_size, uni_buf);
8925E5 - 212    else if (opt & VFAT_SFN_DISPLAY_WIN95)
  213       len = fat_short2uni(nls, buf, buf_size, uni_buf);
- 214    else if (opt & VFAT_SFN_DISPLAY_WINNT) {
- 215       if (lower)
  216          len = fat_short2lower_uni(nls, buf, buf_size, uni_buf);
    217       else
  218          len = fat_short2uni(nls, buf, buf_size, uni_buf);
    219    } else
  220       len = fat_short2uni(nls, buf, buf_size, uni_buf);
  221 
8925E5    222    return len;
  223 }
  224 
  225 enum { PARSE_INVALID = 1, PARSE_NOT_LONGNAME, PARSE_EOF, };
  226 
  227 /**
  228  * fat_parse_long - Parse extended directory entry.
  229  *
  230  * This function returns zero on success, negative value on error, or one of
  231  * the following:
  232  *
  233  * %PARSE_INVALID - Directory entry is invalid.
  234  * %PARSE_NOT_LONGNAME - Directory entry does not contain longname.
  235  * %PARSE_EOF - Directory has no more entries.
  236  */
 
6114E3   237 static int fat_parse_long(struct inode *dir, loff_t *pos,
  238            struct buffer_head **bh, struct msdos_dir_entry **de,
  239            wchar_t **unicode, unsigned char *nr_slots)
  240 {
  241    struct msdos_dir_slot *ds;
  242    unsigned char id, slot, slots, alias_checksum;
  243 
168700 5945E3   244    if (!*unicode) {
  245       *unicode = (wchar_t *)__get_free_page(GFP_KERNEL);
168700 - 246       if (!*unicode) {
  247          brelse(*bh);
 - 248          return -ENOMEM;
  249       }
  250    }
  251 parse_long:
  252    slots = 0;
  253    ds = (struct msdos_dir_slot *)*de;
  254    id = ds->id;
6114E3 - 255    if (!(id & 0x40))
 - 256       return PARSE_INVALID;
  257    slots = id & ~0x40;
6114E3 - 258    if (slots > 20 || !slots)   /* ceil(256 * 2 / 26) */
 - 258   T || _
 - 258   F || T
 6114E3   258   F || F
 - 259       return PARSE_INVALID;
  260    *nr_slots = slots;
  261    alias_checksum = ds->alias_checksum;
  262 
  263    slot = slots;
9300E3 - 264    while (1) {
  265       int offset;
  266 
  267       slot--;
  268       offset = slot * 13;
  269       fat16_towchar(*unicode + offset, ds->name0_4, 5);
  270       fat16_towchar(*unicode + offset + 5, ds->name5_10, 6);
  271       fat16_towchar(*unicode + offset + 11, ds->name11_12, 2);
  272 
6114E3 3186E3   273       if (ds->id & 0x40)
  274          (*unicode)[offset + 13] = 0;
9300E3 - 275       if (fat_get_entry(dir, pos, bh, de) < 0)
 - 276          return PARSE_EOF;
6114E3 3186E3   277       if (slot == 0)
6114E3    278          break;
  279       ds = (struct msdos_dir_slot *)*de;
3186E3 - 280       if (ds->attr != ATTR_EXT)
 - 281          return PARSE_NOT_LONGNAME;
3186E3 - 282       if ((ds->id & ~0x40) != slot)
 - 283          goto parse_long;
3186E3 - 284       if (ds->alias_checksum != alias_checksum)
 - 285          goto parse_long;
  286    }
6114E3 - 287    if ((*de)->name[0] == DELETED_FLAG)
 - 288       return PARSE_INVALID;
6114E3 - 289    if ((*de)->attr == ATTR_EXT)
 - 290       goto parse_long;
6114E3 - 291    if (IS_FREE((*de)->name) || ((*de)->attr & ATTR_VOLUME))
 - 291   (T || _) || (_)
 - 291   (F || T) || (_)
 - 291   (F || F) || (T)
 6114E3   291   (F || F) || (F)
 - 292       return PARSE_INVALID;
6114E3 - 293    if (fat_checksum((*de)->name) != alias_checksum)
  294       *nr_slots = 0;
  295 
6114E3    296    return 0;
  297 }
  298 
  299 /*
  300  * Return values: negative -> error, 0 -> not found, positive -> found,
  301  * value is the total amount of slots, including the shortname entry.
  302  */
 
215222   303 int fat_search_long(struct inode *inode, const unsigned char *name,
  304           int name_len, struct fat_slot_info *sinfo)
  305 {
  306    struct super_block *sb = inode->i_sb;
  307    struct msdos_sb_info *sbi = MSDOS_SB(sb);
  308    struct buffer_head *bh = NULL;
  309    struct msdos_dir_entry *de;
  310    struct nls_table *nls_io = sbi->nls_io;
  311    struct nls_table *nls_disk = sbi->nls_disk;
  312    wchar_t bufuname[14];
  313    unsigned char xlate_len, nr_slots;
  314    wchar_t *unicode = NULL;
  315    unsigned char work[8], bufname[260];   /* 256 + 4 */
  316    int uni_xlate = sbi->options.unicode_xlate;
  317    int utf8 = sbi->options.utf8;
  318    int anycase = (sbi->options.name_check != 's');
  319    unsigned short opt_shortname = sbi->options.shortname;
  320    loff_t cpos = 0;
  321    int chl, i, j, last_u, err;
  322 
  323    err = -ENOENT;
8760E4 - 324    while(1) {
8760E4   325       if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
   326          goto EODir;
  327 parse_record:
  328       nr_slots = 0;
6719E3 8088E4   329       if (de->name[0] == DELETED_FLAG)
6719E3    330          continue;
433 8088E4   331       if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
433    331     T && (T)
 7486E4   331     T && (F)
 6025E3   331     F && (_)
433    332          continue;
2264 8088E4   333       if (de->attr != ATTR_EXT && IS_FREE(de->name))
2264    333     T && (T || _)
 - 333     T && (F || T)
 7486E4   333     T && (F || F)
 6025E3   333     F && (_ || _)
2264    334          continue;
6025E3 7486E4   335       if (de->attr == ATTR_EXT) {
  336          int status = fat_parse_long(inode, &cpos, &bh, &de,
  337                       &unicode, &nr_slots);
6025E3 - 338          if (status < 0)
 - 339             return status;
6025E3 - 340          else if (status == PARSE_INVALID)
 - 341             continue;
6025E3 - 342          else if (status == PARSE_NOT_LONGNAME)
 - 343             goto parse_record;
6025E3 - 344          else if (status == PARSE_EOF)
 - 345             goto EODir;
  346       }
  347 
  348       memcpy(work, de->name, sizeof(de->name));
  349       /* see namei.c, msdos_format_name */
8088E4 - 350       if (work[0] == 0x05)
  351          work[0] = 0xE5;
6470E5 8088E4   352       for (i = 0, j = 0, last_u = 0; i < 8;) {
6470E5 - 353          if (!work[i]) break;
 - 353       break
  354          chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
  355                   &bufuname[j++], opt_shortname,
  356                   de->lcase & CASE_LOWER_BASE);
6470E5 - 357          if (chl <= 1) {
5417E5 1053E5   358             if (work[i] != ' ')
  359                last_u = j;
    360          } else {
  361             last_u = j;
  362          }
  363          i += chl;
  364       }
  365       j = last_u;
  366       fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
2426E5 8088E4   367       for (i = 0; i < 3;) {
2426E5 - 368          if (!de->ext[i]) break;
 - 368       break
  369          chl = fat_shortname2uni(nls_disk, &de->ext[i], 3 - i,
  370                   &bufuname[j++], opt_shortname,
  371                   de->lcase & CASE_LOWER_EXT);
2426E5 - 372          if (chl <= 1) {
2238E5 1882E4   373             if (de->ext[i] != ' ')
  374                last_u = j;
    375          } else {
  376             last_u = j;
  377          }
  378          i += chl;
  379       }
8088E4 - 380       if (!last_u)
 - 381          continue;
  382 
  383       bufuname[last_u] = 0x0000;
  384       xlate_len = utf8
    385          ?utf8_wcstombs(bufname, bufuname, sizeof(bufname))
8088E4 - 385   ternary-?: utf8
  386          :uni16_to_x8(bufname, bufuname, uni_xlate, nls_io);
1925E4 6163E4   387       if (xlate_len == name_len)
  388          if ((!anycase && !memcmp(name, bufname, xlate_len)) ||
  389              (anycase && !nls_strnicmp(nls_io, name, bufname,
148747 1910E4   390                         xlate_len)))
 - 390       (T && T) || (_ && _)
 - 390       (T && F) || (T && T)
148747    390       (F && _) || (T && T)
 - 390       (T && F) || (T && F)
 - 390       (T && F) || (F && _)
 1910E4   390       (F && _) || (T && F)
 - 390       (F && _) || (F && _)
148747    391             goto Found;
  392 
6003E3 7473E4   393       if (nr_slots) {
  394          xlate_len = utf8
    395             ?utf8_wcstombs(bufname, unicode, sizeof(bufname))
6003E3 - 395     ternary-?: utf8
  396             :uni16_to_x8(bufname, unicode, uni_xlate, nls_io);
4539E3 1463E3   397          if (xlate_len != name_len)
4539E3    398             continue;
  399          if ((!anycase && !memcmp(name, bufname, xlate_len)) ||
  400              (anycase && !nls_strnicmp(nls_io, name, bufname,
66471 1397E3   401                         xlate_len)))
 - 401       (T && T) || (_ && _)
 - 401       (T && F) || (T && T)
66471    401       (F && _) || (T && T)
 - 401       (T && F) || (T && F)
 - 401       (T && F) || (F && _)
 1397E3   401       (F && _) || (T && F)
 - 401       (F && _) || (F && _)
66471    402             goto Found;
  403       }
  404    }
  405 
  406 Found:
  407    nr_slots++;   /* include the de */
  408    sinfo->slot_off = cpos - nr_slots * sizeof(*de);
  409    sinfo->nr_slots = nr_slots;
  410    sinfo->de = de;
  411    sinfo->bh = bh;
  412    sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
  413    err = 0;
  414 EODir:
154312 60910   415    if (unicode)
  416       free_page((unsigned long)unicode);
  417 
215222    418    return err;
  419 }
  420 
  421 EXPORT_SYMBOL_GPL(fat_search_long);
  422 
  423 struct fat_ioctl_filldir_callback {
  424    struct dirent __user *dirent;
  425    int result;
  426    /* for dir ioctl */
  427    const char *longname;
  428    int long_len;
  429    const char *shortname;
  430    int short_len;
  431 };
  432 
 
41160   433 static int __fat_readdir(struct inode *inode, struct file *filp, void *dirent,
  434           filldir_t filldir, int short_only, int both)
  435 {
  436    struct super_block *sb = inode->i_sb;
  437    struct msdos_sb_info *sbi = MSDOS_SB(sb);
  438    struct buffer_head *bh;
  439    struct msdos_dir_entry *de;
  440    struct nls_table *nls_io = sbi->nls_io;
  441    struct nls_table *nls_disk = sbi->nls_disk;
  442    unsigned char long_slots;
  443    const char *fill_name;
  444    int fill_len;
  445    wchar_t bufuname[14];
  446    wchar_t *unicode = NULL;
  447    unsigned char c, work[8], bufname[56], *ptname = bufname;
  448    unsigned long lpos, dummy, *furrfu = &lpos;
  449