The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/fs/fat/dir.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    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/fs.h>
   17 #include <linux/msdos_fs.h>
   18 #include <linux/nls.h>
   19 #include <linux/kernel.h>
   20 #include <linux/errno.h>
   21 #include <linux/stat.h>
   22 #include <linux/string.h>
   23 #include <linux/ioctl.h>
   24 #include <linux/dirent.h>
   25 #include <linux/mm.h>
   26 #include <linux/ctype.h>
   27 
   28 #include <asm/uaccess.h>
   29 
   30 #define PRINTK(X)
   31 
   32 struct file_operations fat_dir_operations = {
   33         read:           generic_read_dir,
   34         readdir:        fat_readdir,
   35         ioctl:          fat_dir_ioctl,
   36         fsync:          file_fsync,
   37 };
   38 
   39 /*
   40  * Convert Unicode 16 to UTF8, translated Unicode, or ASCII.
   41  * If uni_xlate is enabled and we can't get a 1:1 conversion, use a
   42  * colon as an escape character since it is normally invalid on the vfat
   43  * filesystem. The following four characters are the hexadecimal digits
   44  * of Unicode value. This lets us do a full dump and restore of Unicode
   45  * filenames. We could get into some trouble with long Unicode names,
   46  * but ignore that right now.
   47  * Ahem... Stack smashing in ring 0 isn't fun. Fixed.
   48  */
   49 static int
   50 uni16_to_x8(unsigned char *ascii, wchar_t *uni, int uni_xlate,
   51             struct nls_table *nls)
   52 {
   53         wchar_t *ip, ec;
   54         unsigned char *op, nc;
   55         int charlen;
   56         int k;
   57 
   58         ip = uni;
   59         op = ascii;
   60 
   61         while (*ip) {
   62                 ec = *ip++;
   63                 if ( (charlen = nls->uni2char(ec, op, NLS_MAX_CHARSET_SIZE)) > 0) {
   64                         op += charlen;
   65                 } else {
   66                         if (uni_xlate == 1) {
   67                                 *op = ':';
   68                                 for (k = 4; k > 0; k--) {
   69                                         nc = ec & 0xF;
   70                                         op[k] = nc > 9  ? nc + ('a' - 10)
   71                                                         : nc + '';
   72                                         ec >>= 4;
   73                                 }
   74                                 op += 5;
   75                         } else {
   76                                 *op++ = '?';
   77                         }
   78                 }
   79                 /* We have some slack there, so it's OK */
   80                 if (op>ascii+256) {
   81                         op = ascii + 256;
   82                         break;
   83                 }
   84         }
   85         *op = 0;
   86         return (op - ascii);
   87 }
   88 
   89 #if 0
   90 static void dump_de(struct msdos_dir_entry *de)
   91 {
   92         int i;
   93         unsigned char *p = (unsigned char *) de;
   94         printk("[");
   95 
   96         for (i = 0; i < 32; i++, p++) {
   97                 printk("%02x ", *p);
   98         }
   99         printk("]\n");
  100 }
  101 #endif
  102 
  103 static inline unsigned char
  104 fat_tolower(struct nls_table *t, unsigned char c)
  105 {
  106         unsigned char nc = t->charset2lower[c];
  107 
  108         return nc ? nc : c;
  109 }
  110 
  111 static inline int
  112 fat_short2uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
  113 {
  114         int charlen;
  115 
  116         charlen = t->char2uni(c, clen, uni);
  117         if (charlen < 0) {
  118                 *uni = 0x003f;  /* a question mark */
  119                 charlen = 1;
  120         }
  121         return charlen;
  122 }
  123 
  124 static inline int
  125 fat_short2lower_uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
  126 {
  127         int charlen;
  128         wchar_t wc;
  129 
  130         charlen = t->char2uni(c, clen, &wc);
  131         if (charlen < 0) {
  132                 *uni = 0x003f;  /* a question mark */
  133                 charlen = 1;
  134         } else if (charlen <= 1) {
  135                 unsigned char nc = t->charset2lower[*c];
  136                 
  137                 if (!nc)
  138                         nc = *c;
  139                 
  140                 if ( (charlen = t->char2uni(&nc, 1, uni)) < 0) {
  141                         *uni = 0x003f;  /* a question mark */
  142                         charlen = 1;
  143                 }
  144         } else
  145                 *uni = wc;
  146         
  147         return charlen;
  148 }
  149 
  150 static int
  151 fat_strnicmp(struct nls_table *t, const unsigned char *s1,
  152                                         const unsigned char *s2, int len)
  153 {
  154         while(len--)
  155                 if (fat_tolower(t, *s1++) != fat_tolower(t, *s2++))
  156                         return 1;
  157 
  158         return 0;
  159 }
  160 
  161 static inline int
  162 fat_shortname2uni(struct nls_table *nls, char *buf, int buf_size,
  163                   wchar_t *uni_buf, unsigned short opt, int lower)
  164 {
  165         int len = 0;
  166 
  167         if (opt & VFAT_SFN_DISPLAY_LOWER)
  168                 len =  fat_short2lower_uni(nls, buf, buf_size, uni_buf);
  169         else if (opt & VFAT_SFN_DISPLAY_WIN95)
  170                 len = fat_short2uni(nls, buf, buf_size, uni_buf);
  171         else if (opt & VFAT_SFN_DISPLAY_WINNT) {
  172                 if (lower)
  173                         len = fat_short2lower_uni(nls, buf, buf_size, uni_buf);
  174                 else 
  175                         len = fat_short2uni(nls, buf, buf_size, uni_buf);
  176         } else
  177                 len = fat_short2uni(nls, buf, buf_size, uni_buf);
  178 
  179         return len;
  180 }
  181 
  182 /*
  183  * Return values: negative -> error, 0 -> not found, positive -> found,
  184  * value is the total amount of slots, including the shortname entry.
  185  */
  186 int fat_search_long(struct inode *inode, const char *name, int name_len,
  187                         int anycase, loff_t *spos, loff_t *lpos)
  188 {
  189         struct super_block *sb = inode->i_sb;
  190         struct buffer_head *bh = NULL;
  191         struct msdos_dir_entry *de;
  192         struct nls_table *nls_io = MSDOS_SB(sb)->nls_io;
  193         struct nls_table *nls_disk = MSDOS_SB(sb)->nls_disk;
  194         wchar_t bufuname[14];
  195         unsigned char xlate_len, long_slots;
  196         wchar_t *unicode = NULL;
  197         char work[8], bufname[260];     /* 256 + 4 */
  198         int uni_xlate = MSDOS_SB(sb)->options.unicode_xlate;
  199         int utf8 = MSDOS_SB(sb)->options.utf8;
  200         unsigned short opt_shortname = MSDOS_SB(sb)->options.shortname;
  201         int ino, chl, i, j, last_u, res = 0;
  202         loff_t cpos = 0;
  203 
  204         while(1) {
  205                 if (fat_get_entry(inode,&cpos,&bh,&de,&ino) == -1)
  206                         goto EODir;
  207 parse_record:
  208                 long_slots = 0;
  209                 if (de->name[0] == (__s8) DELETED_FLAG)
  210                         continue;
  211                 if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
  212                         continue;
  213                 if (de->attr != ATTR_EXT && IS_FREE(de->name))
  214                         continue;
  215                 if (de->attr == ATTR_EXT) {
  216                         struct msdos_dir_slot *ds;
  217                         unsigned char id;
  218                         unsigned char slot;
  219                         unsigned char slots;
  220                         unsigned char sum;
  221                         unsigned char alias_checksum;
  222 
  223                         if (!unicode) {
  224                                 unicode = (wchar_t *)
  225                                         __get_free_page(GFP_KERNEL);
  226                                 if (!unicode) {
  227                                         fat_brelse(sb, bh);
  228                                         return -ENOMEM;
  229                                 }
  230                         }
  231 parse_long:
  232                         slots = 0;
  233                         ds = (struct msdos_dir_slot *) de;
  234                         id = ds->id;
  235                         if (!(id & 0x40))
  236                                 continue;
  237                         slots = id & ~0x40;
  238                         if (slots > 20 || !slots)       /* ceil(256 * 2 / 26) */
  239                                 continue;
  240                         long_slots = slots;
  241                         alias_checksum = ds->alias_checksum;
  242 
  243                         slot = slots;
  244                         while (1) {
  245                                 int offset;
  246 
  247                                 slot--;
  248                                 offset = slot * 13;
  249                                 fat16_towchar(unicode + offset, ds->name0_4, 5);
  250                                 fat16_towchar(unicode + offset + 5, ds->name5_10, 6);
  251                                 fat16_towchar(unicode + offset + 11, ds->name11_12, 2);
  252 
  253                                 if (ds->id & 0x40) {
  254                                         unicode[offset + 13] = 0;
  255                                 }
  256                                 if (fat_get_entry(inode,&cpos,&bh,&de,&ino)<0)
  257                                         goto EODir;
  258                                 if (slot == 0)
  259                                         break;
  260                                 ds = (struct msdos_dir_slot *) de;
  261                                 if (ds->attr !=  ATTR_EXT)
  262                                         goto parse_record;
  263                                 if ((ds->id & ~0x40) != slot)
  264                                         goto parse_long;
  265                                 if (ds->alias_checksum != alias_checksum)
  266                                         goto parse_long;
  267                         }
  268                         if (de->name[0] == (__s8) DELETED_FLAG)
  269                                 continue;
  270                         if (de->attr ==  ATTR_EXT)
  271                                 goto parse_long;
  272                         if (IS_FREE(de->name) || (de->attr & ATTR_VOLUME))
  273                                 continue;
  274                         for (sum = 0, i = 0; i < 11; i++)
  275                                 sum = (((sum&1)<<7)|((sum&0xfe)>>1)) + de->name[i];
  276                         if (sum != alias_checksum)
  277                                 long_slots = 0;
  278                 }
  279 
  280                 for (i = 0; i < 8; i++) {
  281                         /* see namei.c, msdos_format_name */
  282                         if (de->name[i] == 0x05)
  283                                 work[i] = 0xE5;
  284                         else
  285                                 work[i] = de->name[i];
  286                 }
  287                 for (i = 0, j = 0, last_u = 0; i < 8;) {
  288                         if (!work[i]) break;
  289                         chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
  290                                                 &bufuname[j++], opt_shortname,
  291                                                 de->lcase & CASE_LOWER_BASE);
  292                         if (chl <= 1) {
  293                                 if (work[i] != ' ')
  294                                         last_u = j;
  295                         } else {
  296                                 last_u = j;
  297                         }
  298                         i += chl;
  299                 }
  300                 j = last_u;
  301                 fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
  302                 for (i = 0; i < 3;) {
  303                         if (!de->ext[i]) break;
  304                         chl = fat_shortname2uni(nls_disk, &de->ext[i], 3 - i,
  305                                                 &bufuname[j++], opt_shortname,
  306                                                 de->lcase & CASE_LOWER_EXT);
  307                         if (chl <= 1) {
  308                                 if (de->ext[i] != ' ')
  309                                         last_u = j;
  310                         } else {
  311                                 last_u = j;
  312                         }
  313                         i += chl;
  314                 }
  315                 if (!last_u)
  316                         continue;
  317 
  318                 bufuname[last_u] = 0x0000;
  319                 xlate_len = utf8
  320                         ?utf8_wcstombs(bufname, bufuname, sizeof(bufname))
  321                         :uni16_to_x8(bufname, bufuname, uni_xlate, nls_io);
  322                 if (xlate_len == name_len)
  323                         if ((!anycase && !memcmp(name, bufname, xlate_len)) ||
  324                             (anycase && !fat_strnicmp(nls_io, name, bufname,
  325                                                                 xlate_len)))
  326                                 goto Found;
  327 
  328                 if (long_slots) {
  329                         xlate_len = utf8
  330                                 ?utf8_wcstombs(bufname, unicode, sizeof(bufname))
  331                                 :uni16_to_x8(bufname, unicode, uni_xlate, nls_io);
  332                         if (xlate_len != name_len)
  333                                 continue;
  334                         if ((!anycase && !memcmp(name, bufname, xlate_len)) ||
  335                             (anycase && !fat_strnicmp(nls_io, name, bufname,
  336                                                                 xlate_len)))
  337                                 goto Found;
  338                 }
  339         }
  340 
  341 Found:
  342         res = long_slots + 1;
  343         *spos = cpos - sizeof(struct msdos_dir_entry);
  344         *lpos = cpos - res*sizeof(struct msdos_dir_entry);
  345 EODir:
  346         fat_brelse(sb, bh);
  347         if (unicode) {
  348                 free_page((unsigned long) unicode);
  349         }
  350         return res;
  351 }
  352 
  353 static int fat_readdirx(struct inode *inode, struct file *filp, void *dirent,
  354                         filldir_t filldir, int shortnames, int both)
  355 {
  356         struct super_block *sb = inode->i_sb;
  357         struct buffer_head *bh;
  358         struct msdos_dir_entry *de;
  359         struct nls_table *nls_io = MSDOS_SB(sb)->nls_io;
  360         struct nls_table *nls_disk = MSDOS_SB(sb)->nls_disk;
  361         wchar_t bufuname[14];
  362         unsigned char long_slots;
  363         wchar_t *unicode = NULL;
  364         char c, work[8], bufname[56], *ptname = bufname;
  365         unsigned long lpos, dummy, *furrfu = &lpos;
  366         int uni_xlate = MSDOS_SB(sb)->options.unicode_xlate;
  367         int isvfat = MSDOS_SB(sb)->options.isvfat;
  368         int utf8 = MSDOS_SB(sb)->options.utf8;
  369         int nocase = MSDOS_SB(sb)->options.nocase;
  370         unsigned short opt_shortname = MSDOS_SB(sb)->options.shortname;
  371         int ino, inum, chi, chl, i, i2, j, last, last_u, dotoffset = 0;
  372         loff_t cpos;
  373 
  374         cpos = filp->f_pos;
  375 /* Fake . and .. for the root directory. */
  376         if (inode->i_ino == MSDOS_ROOT_INO) {
  377                 while (cpos < 2) {
  378                         if (filldir(dirent, "..", cpos+1, cpos, MSDOS_ROOT_INO, DT_DIR) < 0)
  379                                 return 0;
  380                         cpos++;
  381                         filp->f_pos++;
  382                 }
  383                 if (cpos == 2) {
  384                         dummy = 2;
  385                         furrfu = &dummy;
  386                         cpos = 0;
  387                 }
  388         }
  389         if (cpos & (sizeof(struct msdos_dir_entry)-1))
  390                 return -ENOENT;
  391 
  392         bh = NULL;
  393 GetNew:
  394         long_slots = 0;
  395         if (fat_get_entry(inode,&cpos,&bh,&de,&ino) == -1)
  396                 goto EODir;
  397         /* Check for long filename entry */
  398         if (isvfat) {
  399                 if (de->name[0] == (__s8) DELETED_FLAG)
  400                         goto RecEnd;
  401                 if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
  402                         goto RecEnd;
  403                 if (de->attr != ATTR_EXT && IS_FREE(de->name))
  404                         goto RecEnd;
  405         } else {
  406                 if ((de->attr & ATTR_VOLUME) || IS_FREE(de->name))
  407                         goto RecEnd;
  408         }
  409 
  410         if (isvfat && de->attr == ATTR_EXT) {
  411                 struct msdos_dir_slot *ds;
  412                 unsigned char id;
  413                 unsigned char slot;
  414                 unsigned char slots;
  415                 unsigned char sum;
  416                 unsigned char alias_checksum;
  417 
  418                 if (!unicode) {
  419                         unicode = (wchar_t *)
  420                                 __get_free_page(GFP_KERNEL);
  421                         if (!unicode) {
  422                                 filp->f_pos = cpos;
  423                                 fat_brelse(sb, bh);
  424                                 return -ENOMEM;
  425                         }
  426                 }
  427 ParseLong:
  428                 slots = 0;
  429                 ds = (struct msdos_dir_slot *) de;
  430                 id = ds->id;
  431                 if (!(id & 0x40))
  432                         goto RecEnd;
  433                 slots = id & ~0x40;
  434                 if (slots > 20 || !slots)       /* ceil(256 * 2 / 26) */
  435                         goto RecEnd;
  436                 long_slots = slots;
  437                 alias_checksum = ds->alias_checksum;
  438 
  439                 slot = slots;
  440                 while (1) {
  441                         int offset;
  442 
  443                         slot--;
  444                         offset = slot * 13;
  445                         fat16_towchar(unicode + offset, ds->name0_4, 5);
  446                         fat16_towchar(unicode + offset + 5, ds->name5_10, 6);
  447                         fat16_towchar(unicode + offset + 11, ds->name11_12, 2);
  448 
  449                         if (ds->id & 0x40) {
  450                                 unicode[offset + 13] = 0;
  451                         }
  452                         if (fat_get_entry(inode,&cpos,&bh,&de,&ino) == -1)
  453                                 goto EODir;
  454                         if (slot == 0)
  455                                 break;
  456                         ds = (struct msdos_dir_slot *) de;
  457                         if (ds->attr !=  ATTR_EXT)
  458                                 goto RecEnd;    /* XXX */
  459                         if ((ds->id & ~0x40) != slot)
  460                                 goto ParseLong;
  461                         if (ds->alias_checksum != alias_checksum)
  462                                 goto ParseLong;
  463                 }
  464                 if (de->name[0] == (__s8) DELETED_FLAG)
  465                         goto RecEnd;
  466                 if (de->attr ==  ATTR_EXT)
  467                         goto ParseLong;
  468                 if (IS_FREE(de->name) || (de->attr & ATTR_VOLUME))
  469                         goto RecEnd;
  470                 for (sum = 0, i = 0; i < 11; i++)
  471                         sum = (((sum&1)<<7)|((sum&0xfe)>>1)) + de->name[i];
  472                 if (sum != alias_checksum)
  473                         long_slots = 0;
  474         }
  475 
  476         if ((de->attr & ATTR_HIDDEN) && MSDOS_SB(sb)->options.dotsOK) {
  477                 *ptname++ = '.';
  478                 dotoffset = 1;
  479         }
  480 
  481         for (i = 0; i < 8; i++) {
  482                 /* see namei.c, msdos_format_name */
  483                 if (de->name[i] == 0x05)
  484                         work[i] = 0xE5;
  485                 else
  486                         work[i] = de->name[i];
  487         }
  488         for (i = 0, j = 0, last = 0, last_u = 0; i < 8;) {
  489                 if (!(c = work[i])) break;
  490                 chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
  491                                         &bufuname[j++], opt_shortname,
  492                                         de->lcase & CASE_LOWER_BASE);
  493                 if (chl <= 1) {
  494                         ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
  495                         if (c != ' ') {
  496                                 last = i;
  497                                 last_u = j;
  498                         }
  499                 } else {
  500                         last_u = j;
  501                         for (chi = 0; chi < chl && i < 8; chi++) {
  502                                 ptname[i] = work[i];
  503                                 i++; last = i;
  504                         }
  505                 }
  506         }
  507         i = last;
  508         j = last_u;
  509         fat_short2uni(nls_disk, ".", 1, &bufuname[j++]);
  510         ptname[i++] = '.';
  511         for (i2 = 0; i2 < 3;) {
  512                 if (!(c = de->ext[i2])) break;
  513                 chl = fat_shortname2uni(nls_disk, &de->ext[i2], 3 - i2,
  514                                         &bufuname[j++], opt_shortname,
  515                                         de->lcase & CASE_LOWER_EXT);
  516                 if (chl <= 1) {
  517                         i2++;
  518                         ptname[i++] = (!nocase && c>='A' && c<='Z') ? c+32 : c;
  519                         if (c != ' ') {
  520                                 last = i;
  521                                 last_u = j;
  522                         }
  523                 } else {
  524                         last_u = j;
  525                         for (chi = 0; chi < chl && i2 < 3; chi++) {
  526                                 ptname[i++] = de->ext[i2++];
  527                                 last = i;
  528                         }
  529                 }
  530         }
  531         if (!last)
  532                 goto RecEnd;
  533 
  534         i = last + dotoffset;
  535         j = last_u;
  536 
  537         lpos = cpos - (long_slots+1)*sizeof(struct msdos_dir_entry);
  538         if (!memcmp(de->name,MSDOS_DOT,11))
  539                 inum = inode->i_ino;
  540         else if (!memcmp(de->name,MSDOS_DOTDOT,11)) {
  541 /*              inum = fat_parent_ino(inode,0); */
  542                 inum = filp->f_dentry->d_parent->d_inode->i_ino;
  543         } else {
  544                 struct inode *tmp = fat_iget(sb, ino);
  545                 if (tmp) {
  546                         inum = tmp->i_ino;
  547                         iput(tmp);
  548                 } else
  549                         inum = iunique(sb, MSDOS_ROOT_INO);
  550         }
  551 
  552         if (isvfat) {
  553                 bufuname[j] = 0x0000;
  554                 i = utf8 ? utf8_wcstombs(bufname, bufuname, sizeof(bufname))
  555                          : uni16_to_x8(bufname, bufuname, uni_xlate, nls_io);
  556         }
  557 
  558         if (!long_slots||shortnames) {
  559                 if (both)
  560                         bufname[i] = '\0';
  561                 if (filldir(dirent, bufname, i, *furrfu, inum,
  562                             (de->attr & ATTR_DIR) ? DT_DIR : DT_REG) < 0)
  563                         goto FillFailed;
  564         } else {
  565                 char longname[275];
  566                 int long_len = utf8
  567                         ? utf8_wcstombs(longname, unicode, sizeof(longname))
  568                         : uni16_to_x8(longname, unicode, uni_xlate,
  569                                       nls_io);
  570                 if (both) {
  571                         memcpy(&longname[long_len+1], bufname, i);
  572                         long_len += i;
  573                 }
  574                 if (filldir(dirent, longname, long_len, *furrfu, inum,
  575                             (de->attr & ATTR_DIR) ? DT_DIR : DT_REG) < 0)
  576                         goto FillFailed;
  577         }
  578 
  579 RecEnd:
  580         furrfu = &lpos;
  581         filp->f_pos = cpos;
  582         goto GetNew;
  583 EODir:
  584         filp->f_pos = cpos;
  585 FillFailed:
  586         if (bh)
  587                 fat_brelse(sb, bh);
  588         if (unicode) {
  589                 free_page((unsigned long) unicode);
  590         }
  591         return 0;
  592 }
  593 
  594 int fat_readdir(struct file *filp, void *dirent, filldir_t filldir)
  595 {
  596         struct inode *inode = filp->f_dentry->d_inode;
  597         return fat_readdirx(inode, filp, dirent, filldir, 0, 0);
  598 }
  599 
  600 static int vfat_ioctl_fill(
  601         void * buf,
  602         const char * name,
  603         int name_len,
  604         loff_t offset,
  605         ino_t ino,
  606         unsigned int d_type)
  607 {
  608         struct dirent *d1 = (struct dirent *)buf;
  609         struct dirent *d2 = d1 + 1;
  610         int len, slen;
  611         int dotdir;
  612 
  613         get_user(len, &d1->d_reclen);
  614         if (len != 0) {
  615                 return -1;
  616         }
  617 
  618         if ((name_len == 1 && name[0] == '.') ||
  619             (name_len == 2 && name[0] == '.' && name[1] == '.')) {
  620                 dotdir = 1;
  621                 len = name_len;
  622         } else {
  623                 dotdir = 0;
  624                 len = strlen(name);
  625         }
  626         if (len != name_len) {
  627                 copy_to_user(d2->d_name, name, len);
  628                 put_user(0, d2->d_name + len);
  629                 put_user(len, &d2->d_reclen);
  630                 put_user(ino, &d2->d_ino);
  631                 put_user(offset, &d2->d_off);
  632                 slen = name_len - len;
  633                 copy_to_user(d1->d_name, name+len+1, slen);
  634                 put_user(0, d1->d_name+slen);
  635                 put_user(slen, &d1->d_reclen);
  636         } else {
  637                 put_user(0, d2->d_name);
  638                 put_user(0, &d2->d_reclen);
  639                 copy_to_user(d1->d_name, name, len);
  640                 put_user(0, d1->d_name+len);
  641                 put_user(len, &d1->d_reclen);
  642         }
  643         PRINTK(("FAT d1=%p d2=%p len=%d, name_len=%d\n",
  644                 d1, d2, len, name_len));
  645 
  646         return 0;
  647 }
  648 
  649 int fat_dir_ioctl(struct inode * inode, struct file * filp,
  650                   unsigned int cmd, unsigned long arg)
  651 {
  652         int err;
  653         /*
  654          * We want to provide an interface for Samba to be able
  655          * to get the short filename for a given long filename.
  656          * Samba should use this ioctl instead of readdir() to
  657          * get the information it needs.
  658          */
  659         switch (cmd) {
  660         case VFAT_IOCTL_READDIR_BOTH: {
  661                 struct dirent *d1 = (struct dirent *)arg;
  662                 err = verify_area(VERIFY_WRITE, d1, sizeof(struct dirent[2]));
  663                 if (err)
  664                         return err;
  665                 put_user(0, &d1->d_reclen);
  666                 return fat_readdirx(inode,filp,(void *)arg,
  667                                     vfat_ioctl_fill, 0, 1);
  668         }
  669         case VFAT_IOCTL_READDIR_SHORT: {
  670                 struct dirent *d1 = (struct dirent *)arg;
  671                 put_user(0, &d1->d_reclen);
  672                 err = verify_area(VERIFY_WRITE, d1, sizeof(struct dirent[2]));
  673                 if (err)
  674                         return err;
  675                 return fat_readdirx(inode,filp,(void *)arg,
  676                                     vfat_ioctl_fill, 1, 1);
  677         }
  678         default:
  679                 /* forward ioctl to CVF extension */
  680                if (MSDOS_SB(inode->i_sb)->cvf_format &&
  681                    MSDOS_SB(inode->i_sb)->cvf_format->cvf_dir_ioctl)
  682                        return MSDOS_SB(inode->i_sb)->cvf_format
  683                                ->cvf_dir_ioctl(inode,filp,cmd,arg);
  684                 return -EINVAL;
  685         }
  686 
  687         return 0;
  688 }
  689 
  690 /***** See if directory is empty */
  691 int fat_dir_empty(struct inode *dir)
  692 {
  693         loff_t pos;
  694         struct buffer_head *bh;
  695         struct msdos_dir_entry *de;
  696         int ino,result = 0;
  697 
  698         pos = 0;
  699         bh = NULL;
  700         while (fat_get_entry(dir,&pos,&bh,&de,&ino) > -1) {
  701                 /* Ignore vfat longname entries */
  702                 if (de->attr == ATTR_EXT)
  703                         continue;
  704                 if (!IS_FREE(de->name) && 
  705                     strncmp(de->name,MSDOS_DOT   , MSDOS_NAME) &&
  706                     strncmp(de->name,MSDOS_DOTDOT, MSDOS_NAME)) {
  707                         result = -ENOTEMPTY;
  708                         break;
  709                 }
  710         }
  711         if (bh)
  712                 fat_brelse(dir->i_sb, bh);
  713 
  714         return result;
  715 }
  716 
  717 /* This assumes that size of cluster is above the 32*slots */
  718 
  719 int fat_add_entries(struct inode *dir,int slots, struct buffer_head **bh,
  720                   struct msdos_dir_entry **de, int *ino)
  721 {
  722         struct super_block *sb = dir->i_sb;
  723         loff_t offset, curr;
  724         int row;
  725         struct buffer_head *new_bh;
  726 
  727         offset = curr = 0;
  728         *bh = NULL;
  729         row = 0;
  730         while (fat_get_entry(dir,&curr,bh,de,ino) > -1) {
  731                 if (IS_FREE((*de)->name)) {
  732                         if (++row == slots)
  733                                 return offset;
  734                 } else {
  735                         row = 0;
  736                         offset = curr;
  737                 }
  738         }
  739         if ((dir->i_ino == MSDOS_ROOT_INO) && (MSDOS_SB(sb)->fat_bits != 32)) 
  740                 return -ENOSPC;
  741         new_bh = fat_extend_dir(dir);
  742         if (!new_bh)
  743                 return -ENOSPC;
  744         fat_brelse(sb, new_bh);
  745         do fat_get_entry(dir,&curr,bh,de,ino); while (++row<slots);
  746         return offset;
  747 }
  748 
  749 int fat_new_dir(struct inode *dir, struct inode *parent, int is_vfat)
  750 {
  751         struct super_block *sb = dir->i_sb;
  752         struct buffer_head *bh;
  753         struct msdos_dir_entry *de;
  754         __u16 date, time;
  755 
  756         if ((bh = fat_extend_dir(dir)) == NULL) return -ENOSPC;
  757         /* zeroed out, so... */
  758         fat_date_unix2dos(dir->i_mtime,&time,&date);
  759         de = (struct msdos_dir_entry*)&bh->b_data[0];
  760         memcpy(de[0].name,MSDOS_DOT,MSDOS_NAME);
  761         memcpy(de[1].name,MSDOS_DOTDOT,MSDOS_NAME);
  762         de[0].attr = de[1].attr = ATTR_DIR;
  763         de[0].time = de[1].time = CT_LE_W(time);
  764         de[0].date = de[1].date = CT_LE_W(date);
  765         if (is_vfat) {  /* extra timestamps */
  766                 de[0].ctime = de[1].ctime = CT_LE_W(time);
  767                 de[0].adate = de[0].cdate =
  768                         de[1].adate = de[1].cdate = CT_LE_W(date);
  769         }
  770         de[0].start = CT_LE_W(MSDOS_I(dir)->i_logstart);
  771         de[0].starthi = CT_LE_W(MSDOS_I(dir)->i_logstart>>16);
  772         de[1].start = CT_LE_W(MSDOS_I(parent)->i_logstart);
  773         de[1].starthi = CT_LE_W(MSDOS_I(parent)->i_logstart>>16);
  774         fat_mark_buffer_dirty(sb, bh);
  775         fat_brelse(sb, bh);
  776         dir->i_atime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  777         mark_inode_dirty(dir);
  778 
  779         return 0;
  780 }
  781 
  782 /*
  783  * Overrides for Emacs so that we follow Linus's tabbing style.
  784  * Emacs will notice this stuff at the end of the file and automatically
  785  * adjust the settings for this buffer only.  This must remain at the end
  786  * of the file.
  787  * ---------------------------------------------------------------------------
  788  * Local variables:
  789  * c-indent-level: 8
  790  * c-brace-imaginary-offset: 0
  791  * c-brace-offset: -8
  792  * c-argdecl-indent: 8
  793  * c-label-offset: -8
  794  * c-continued-statement-offset: 8
  795  * c-continued-brace-offset: 0
  796  * End:
  797  */

Cache object: 03c719648e72949e49429c3ae973fcaa


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.