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/hfs/dir_nat.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/hfs/dir_nat.c
    3  *
    4  * Copyright (C) 1995-1997  Paul H. Hargrove
    5  * This file may be distributed under the terms of the GNU General Public License.
    6  *
    7  * This file contains the inode_operations and file_operations
    8  * structures for HFS directories.
    9  *
   10  * Based on the minix file system code, (C) 1991, 1992 by Linus Torvalds
   11  *
   12  * The source code distributions of Netatalk, versions 1.3.3b2 and
   13  * 1.4b2, were used as a specification of the location and format of
   14  * files used by Netatalk's afpd.  No code from Netatalk appears in
   15  * hfs_fs.  hfs_fs is not a work ``derived'' from Netatalk in the
   16  * sense of intellectual property law.
   17  *
   18  * "XXX" in a comment is a note to myself to consider changing something.
   19  *
   20  * In function preconditions the term "valid" applied to a pointer to
   21  * a structure means that the pointer is non-NULL and the structure it
   22  * points to has all fields initialized to consistent values.
   23  */
   24 
   25 #include "hfs.h"
   26 #include <linux/hfs_fs_sb.h>
   27 #include <linux/hfs_fs_i.h>
   28 #include <linux/hfs_fs.h>
   29 
   30 /*================ Forward declarations ================*/
   31 
   32 static struct dentry *nat_lookup(struct inode *, struct dentry *);
   33 static int nat_readdir(struct file *, void *, filldir_t);
   34 static int nat_rmdir(struct inode *, struct dentry *);
   35 static int nat_hdr_unlink(struct inode *, struct dentry *);
   36 static int nat_hdr_rename(struct inode *, struct dentry *,
   37                           struct inode *, struct dentry *);
   38 
   39 /*================ Global variables ================*/
   40 
   41 #define DOT_LEN                 1
   42 #define DOT_DOT_LEN             2
   43 #define DOT_APPLEDOUBLE_LEN     12
   44 #define DOT_PARENT_LEN          7
   45 #define ROOTINFO_LEN            8
   46 
   47 const struct hfs_name hfs_nat_reserved1[] = {
   48         {DOT_LEN,               "."},
   49         {DOT_DOT_LEN,           ".."},
   50         {DOT_APPLEDOUBLE_LEN,   ".AppleDouble"},
   51         {DOT_PARENT_LEN,        ".Parent"},
   52         {0,                     ""},
   53 };
   54 
   55 const struct hfs_name hfs_nat_reserved2[] = {
   56         {ROOTINFO_LEN,                  "RootInfo"},
   57 };
   58 
   59 #define DOT             (&hfs_nat_reserved1[0])
   60 #define DOT_DOT         (&hfs_nat_reserved1[1])
   61 #define DOT_APPLEDOUBLE (&hfs_nat_reserved1[2])
   62 #define DOT_PARENT      (&hfs_nat_reserved1[3])
   63 #define ROOTINFO        (&hfs_nat_reserved2[0])
   64 
   65 struct file_operations hfs_nat_dir_operations = {
   66         read:           generic_read_dir,
   67         readdir:        nat_readdir,
   68         fsync:          file_fsync,
   69 };
   70 
   71 struct inode_operations hfs_nat_ndir_inode_operations = {
   72         create:         hfs_create,
   73         lookup:         nat_lookup,
   74         unlink:         hfs_unlink,
   75         mkdir:          hfs_mkdir,
   76         rmdir:          nat_rmdir,
   77         rename:         hfs_rename,
   78         setattr:        hfs_notify_change,
   79 };
   80 
   81 struct inode_operations hfs_nat_hdir_inode_operations = {
   82         create:         hfs_create,
   83         lookup:         nat_lookup,
   84         unlink:         nat_hdr_unlink,
   85         rename:         nat_hdr_rename,
   86         setattr:        hfs_notify_change,
   87 };
   88 
   89 /*================ File-local functions ================*/
   90 
   91 /*
   92  * nat_lookup()
   93  *
   94  * This is the lookup() entry in the inode_operations structure for
   95  * HFS directories in the Netatalk scheme.  The purpose is to generate
   96  * the inode corresponding to an entry in a directory, given the inode
   97  * for the directory and the name (and its length) of the entry.
   98  */
   99 static struct dentry *nat_lookup(struct inode * dir, struct dentry *dentry)
  100 {
  101         ino_t dtype;
  102         struct hfs_name cname;
  103         struct hfs_cat_entry *entry;
  104         struct hfs_cat_key key;
  105         struct inode *inode = NULL;
  106 
  107         dentry->d_op = &hfs_dentry_operations;
  108         entry = HFS_I(dir)->entry;
  109         dtype = HFS_ITYPE(dir->i_ino);
  110 
  111         /* Perform name-mangling */
  112         hfs_nameout(dir, &cname, dentry->d_name.name, dentry->d_name.len);
  113 
  114         /* no need to check for "."  or ".." */
  115 
  116         /* Check for ".AppleDouble" if in a normal directory,
  117            and for ".Parent" in ".AppleDouble". */
  118         if (dtype==HFS_NAT_NDIR) {
  119                 /* Check for ".AppleDouble" */
  120                 if (hfs_streq(cname.Name, cname.Len, 
  121                               DOT_APPLEDOUBLE->Name, DOT_APPLEDOUBLE_LEN)) {
  122                         ++entry->count; /* __hfs_iget() eats one */
  123                         inode = hfs_iget(entry, HFS_NAT_HDIR, dentry);
  124                         goto done;
  125                 }
  126         } else if (dtype==HFS_NAT_HDIR) {
  127                 if (hfs_streq(cname.Name, cname.Len, 
  128                               DOT_PARENT->Name, DOT_PARENT_LEN)) {
  129                         ++entry->count; /* __hfs_iget() eats one */
  130                         inode = hfs_iget(entry, HFS_NAT_HDR, dentry);
  131                         goto done;
  132                 }
  133 
  134                 if ((entry->cnid == htonl(HFS_ROOT_CNID)) &&
  135                     hfs_streq(cname.Name, cname.Len, 
  136                               ROOTINFO->Name, ROOTINFO_LEN)) {
  137                         ++entry->count; /* __hfs_iget() eats one */
  138                         inode = hfs_iget(entry, HFS_NAT_HDR, dentry);
  139                         goto done;
  140                 }
  141         }
  142 
  143         /* Do an hfs_iget() on the mangled name. */
  144         hfs_cat_build_key(entry->cnid, &cname, &key);
  145         inode = hfs_iget(hfs_cat_get(entry->mdb, &key), 
  146                          HFS_I(dir)->file_type, dentry);
  147 
  148         /* Don't return a header file for a directory other than .Parent */
  149         if (inode && (dtype == HFS_NAT_HDIR) &&
  150             (HFS_I(inode)->entry != entry) &&
  151             (HFS_I(inode)->entry->type == HFS_CDR_DIR)) {
  152                 iput(inode); /* this does an hfs_cat_put */
  153                 inode = NULL;
  154         }
  155 
  156 done:
  157         d_add(dentry, inode);
  158         return NULL;
  159 }
  160 
  161 /*
  162  * nat_readdir()
  163  *
  164  * This is the readdir() entry in the file_operations structure for
  165  * HFS directories in the netatalk scheme.  The purpose is to
  166  * enumerate the entries in a directory, given the inode of the
  167  * directory and a struct file which indicates the location in the
  168  * directory.  The struct file is updated so that the next call with
  169  * the same dir and filp will produce the next directory entry.  The
  170  * entries are returned in dirent, which is "filled-in" by calling
  171  * filldir().  This allows the same readdir() function be used for
  172  * different dirent formats.  We try to read in as many entries as we
  173  * can before filldir() refuses to take any more.
  174  *
  175  * Note that the Netatalk format doesn't have the problem with
  176  * metadata for covered directories that exists in the other formats,
  177  * since the metadata is contained within the directory.
  178  */
  179 static int nat_readdir(struct file * filp,
  180                        void * dirent, filldir_t filldir)
  181 {
  182         ino_t type;
  183         int skip_dirs;
  184         struct hfs_brec brec;
  185         struct hfs_cat_entry *entry;
  186         struct inode *dir = filp->f_dentry->d_inode;
  187 
  188         entry = HFS_I(dir)->entry;
  189         type = HFS_ITYPE(dir->i_ino);
  190         skip_dirs = (type == HFS_NAT_HDIR);
  191 
  192         if (filp->f_pos == 0) {
  193                 /* Entry 0 is for "." */
  194                 if (filldir(dirent, DOT->Name, DOT_LEN, 0, dir->i_ino,
  195                             DT_DIR)) {
  196                         return 0;
  197                 }
  198                 filp->f_pos = 1;
  199         }
  200 
  201         if (filp->f_pos == 1) {
  202                 /* Entry 1 is for ".." */
  203                 hfs_u32 cnid;
  204 
  205                 if (type == HFS_NAT_NDIR) {
  206                         cnid = hfs_get_nl(entry->key.ParID);
  207                 } else {
  208                         cnid = entry->cnid;
  209                 }
  210 
  211                 if (filldir(dirent, DOT_DOT->Name,
  212                             DOT_DOT_LEN, 1, ntohl(cnid), DT_DIR)) {
  213                         return 0;
  214                 }
  215                 filp->f_pos = 2;
  216         }
  217 
  218         if (filp->f_pos < (dir->i_size - 2)) {
  219                 hfs_u32 cnid;
  220                 hfs_u8 type;
  221 
  222                 if (hfs_cat_open(entry, &brec) ||
  223                     hfs_cat_next(entry, &brec, filp->f_pos - 2, &cnid, &type)) {
  224                         return 0;
  225                 }
  226                 while (filp->f_pos < (dir->i_size - 2)) {
  227                         if (hfs_cat_next(entry, &brec, 1, &cnid, &type)) {
  228                                 return 0;
  229                         }
  230                         if (!skip_dirs || (type != HFS_CDR_DIR)) {
  231                                 ino_t ino;
  232                                 unsigned int len;
  233                                 unsigned char tmp_name[HFS_NAMEMAX];
  234 
  235                                 ino = ntohl(cnid) | HFS_I(dir)->file_type;
  236                                 len = hfs_namein(dir, tmp_name,
  237                                     &((struct hfs_cat_key *)brec.key)->CName);
  238                                 if (filldir(dirent, tmp_name, len,
  239                                             filp->f_pos, ino, DT_UNKNOWN)) {
  240                                         hfs_cat_close(entry, &brec);
  241                                         return 0;
  242                                 }
  243                         }
  244                         ++filp->f_pos;
  245                 }
  246                 hfs_cat_close(entry, &brec);
  247         }
  248 
  249         if (filp->f_pos == (dir->i_size - 2)) {
  250                 if (type == HFS_NAT_NDIR) {
  251                         /* In normal dirs entry 2 is for ".AppleDouble" */
  252                         if (filldir(dirent, DOT_APPLEDOUBLE->Name,
  253                                     DOT_APPLEDOUBLE_LEN, filp->f_pos,
  254                                     ntohl(entry->cnid) | HFS_NAT_HDIR,
  255                                     DT_UNKNOWN)) {
  256                                 return 0;
  257                         }
  258                 } else if (type == HFS_NAT_HDIR) {
  259                         /* In .AppleDouble entry 2 is for ".Parent" */
  260                         if (filldir(dirent, DOT_PARENT->Name,
  261                                     DOT_PARENT_LEN, filp->f_pos,
  262                                     ntohl(entry->cnid) | HFS_NAT_HDR,
  263                                     DT_UNKNOWN)) {
  264                                 return 0;
  265                         }
  266                 }
  267                 ++filp->f_pos;
  268         }
  269 
  270         if (filp->f_pos == (dir->i_size - 1)) {
  271                 /* handle ROOT/.AppleDouble/RootInfo as the last entry. */
  272                 if ((entry->cnid == htonl(HFS_ROOT_CNID)) &&
  273                     (type == HFS_NAT_HDIR)) {
  274                         if (filldir(dirent, ROOTINFO->Name,
  275                                     ROOTINFO_LEN, filp->f_pos,
  276                                     ntohl(entry->cnid) | HFS_NAT_HDR,
  277                                     DT_UNKNOWN)) {
  278                                 return 0;
  279                         }
  280                 }
  281                 ++filp->f_pos;
  282         }
  283 
  284         return 0;
  285 }
  286 
  287 /* due to the dcache caching negative dentries for non-existent files,
  288  * we need to drop those entries when a file silently gets created.
  289  * as far as i can tell, the calls that need to do this are the file
  290  * related calls (create, rename, and mknod). the directory calls
  291  * should be immune. the relevant calls in dir.c call drop_dentry 
  292  * upon successful completion. */
  293 void hfs_nat_drop_dentry(struct dentry *dentry, const ino_t type)
  294 {
  295   struct dentry *de;
  296   
  297   switch (type) {
  298   case HFS_NAT_HDR: /* given .AppleDouble/name */
  299     /* look for name */
  300     de = hfs_lookup_dentry(dentry->d_parent->d_parent,
  301                            dentry->d_name.name, dentry->d_name.len);
  302 
  303     if (de) {
  304       if (!de->d_inode)
  305         d_drop(de);
  306       dput(de);
  307     }
  308     break;
  309   case HFS_NAT_DATA: /* given name */
  310     /* look for .AppleDouble/name */
  311     hfs_drop_special(dentry->d_parent, DOT_APPLEDOUBLE, dentry);
  312     break;
  313   }
  314 
  315 }
  316 
  317 /*
  318  * nat_rmdir()
  319  *
  320  * This is the rmdir() entry in the inode_operations structure for
  321  * Netatalk directories.  The purpose is to delete an existing
  322  * directory, given the inode for the parent directory and the name
  323  * (and its length) of the existing directory.
  324  *
  325  * We handle .AppleDouble and call hfs_rmdir() for all other cases.
  326  */
  327 static int nat_rmdir(struct inode *parent, struct dentry *dentry)
  328 {
  329         struct hfs_cat_entry *entry = HFS_I(parent)->entry;
  330         struct hfs_name cname;
  331         int error;
  332 
  333         hfs_nameout(parent, &cname, dentry->d_name.name, dentry->d_name.len);
  334         if (hfs_streq(cname.Name, cname.Len,
  335                       DOT_APPLEDOUBLE->Name, DOT_APPLEDOUBLE_LEN)) {
  336                 if (!HFS_SB(parent->i_sb)->s_afpd) {
  337                         /* Not in AFPD compatibility mode */
  338                         error = -EPERM;
  339                 } else if (entry->u.dir.files || entry->u.dir.dirs) {
  340                         /* AFPD compatible, but the directory is not empty */
  341                         error = -ENOTEMPTY;
  342                 } else {
  343                         /* AFPD compatible, so pretend to succeed */
  344                         error = 0;
  345                 }
  346         } else {
  347                 error = hfs_rmdir(parent, dentry);
  348         }
  349         return error;
  350 }
  351 
  352 /*
  353  * nat_hdr_unlink()
  354  *
  355  * This is the unlink() entry in the inode_operations structure for
  356  * Netatalk .AppleDouble directories.  The purpose is to delete an
  357  * existing file, given the inode for the parent directory and the name
  358  * (and its length) of the existing file.
  359  *
  360  * WE DON'T ACTUALLY DELETE HEADER THE FILE.
  361  * In non-afpd-compatible mode:
  362  *   We return -EPERM.
  363  * In afpd-compatible mode:
  364  *   We return success if the file exists or is .Parent.
  365  *   Otherwise we return -ENOENT.
  366  */
  367 static int nat_hdr_unlink(struct inode *dir, struct dentry *dentry)
  368 {
  369         struct hfs_cat_entry *entry = HFS_I(dir)->entry;
  370         int error = 0;
  371 
  372         if (!HFS_SB(dir->i_sb)->s_afpd) {
  373                 /* Not in AFPD compatibility mode */
  374                 error = -EPERM;
  375         } else {
  376                 struct hfs_name cname;
  377 
  378                 hfs_nameout(dir, &cname, dentry->d_name.name, 
  379                             dentry->d_name.len);
  380                 if (!hfs_streq(cname.Name, cname.Len,
  381                                DOT_PARENT->Name, DOT_PARENT_LEN)) {
  382                         struct hfs_cat_entry *victim;
  383                         struct hfs_cat_key key;
  384 
  385                         hfs_cat_build_key(entry->cnid, &cname, &key);
  386                         victim = hfs_cat_get(entry->mdb, &key);
  387 
  388                         if (victim) {
  389                                 /* pretend to succeed */
  390                                 hfs_cat_put(victim);
  391                         } else {
  392                                 error = -ENOENT;
  393                         }
  394                 }
  395         }
  396         return error;
  397 }
  398 
  399 /*
  400  * nat_hdr_rename()
  401  *
  402  * This is the rename() entry in the inode_operations structure for
  403  * Netatalk header directories.  The purpose is to rename an existing
  404  * file given the inode for the current directory and the name 
  405  * (and its length) of the existing file and the inode for the new
  406  * directory and the name (and its length) of the new file/directory.
  407  *
  408  * WE NEVER MOVE ANYTHING.
  409  * In non-afpd-compatible mode:
  410  *   We return -EPERM.
  411  * In afpd-compatible mode:
  412  *   If the source header doesn't exist, we return -ENOENT.
  413  *   If the destination is not a header directory we return -EPERM.
  414  *   We return success if the destination is also a header directory
  415  *    and the header exists or is ".Parent".
  416  */
  417 static int nat_hdr_rename(struct inode *old_dir, struct dentry *old_dentry,
  418                           struct inode *new_dir, struct dentry *new_dentry)
  419 {
  420         struct hfs_cat_entry *entry = HFS_I(old_dir)->entry;
  421         int error = 0;
  422 
  423         if (!HFS_SB(old_dir->i_sb)->s_afpd) {
  424                 /* Not in AFPD compatibility mode */
  425                 error = -EPERM;
  426         } else {
  427                 struct hfs_name cname;
  428 
  429                 hfs_nameout(old_dir, &cname, old_dentry->d_name.name,
  430                             old_dentry->d_name.len);
  431                 if (!hfs_streq(cname.Name, cname.Len, 
  432                                DOT_PARENT->Name, DOT_PARENT_LEN)) {
  433                         struct hfs_cat_entry *victim;
  434                         struct hfs_cat_key key;
  435 
  436                         hfs_cat_build_key(entry->cnid, &cname, &key);
  437                         victim = hfs_cat_get(entry->mdb, &key);
  438 
  439                         if (victim) {
  440                                 /* pretend to succeed */
  441                                 hfs_cat_put(victim);
  442                         } else {
  443                                 error = -ENOENT;
  444                         }
  445                 }
  446 
  447                 if (!error && (HFS_ITYPE(new_dir->i_ino) != HFS_NAT_HDIR)) {
  448                         error = -EPERM;
  449                 }
  450         }
  451         return error;
  452 }

Cache object: 62c95ae0055d3e3da69d30550ecc9921


[ 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.