LCOV - code coverage report
Current view: top level - lkbce/fs/isofs - export.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 89 89 100.0 %
Date: 2017-01-25 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /*
       2             :  * fs/isofs/export.c
       3             :  *
       4             :  *  (C) 2004  Paul Serice - The new inode scheme requires switching
       5             :  *                          from iget() to iget5_locked() which means
       6             :  *                          the NFS export operations have to be hand
       7             :  *                          coded because the default routines rely on
       8             :  *                          iget().
       9             :  *
      10             :  * The following files are helpful:
      11             :  *
      12             :  *     Documentation/filesystems/nfs/Exporting
      13             :  *     fs/exportfs/expfs.c.
      14             :  */
      15             : 
      16             : #include "isofs.h"
      17             : 
      18             : static struct dentry *
      19             : isofs_export_iget(struct super_block *sb,
      20             :                   unsigned long block,
      21             :                   unsigned long offset,
      22           2 :                   __u32 generation)
      23           2 : {
      24           2 :         struct inode *inode;
      25           2 : 
      26           6 :         if (block == 0)
      27           8 :                 return ERR_PTR(-ESTALE);
      28          12 :         inode = isofs_iget(sb, block, offset);
      29           8 :         if (IS_ERR(inode))
      30           6 :                 return ERR_CAST(inode);
      31           8 :         if (generation && inode->i_generation != generation) {
      32           2 :                 iput(inode);
      33           6 :                 return ERR_PTR(-ESTALE);
      34             :         }
      35           4 :         return d_obtain_alias(inode);
      36             : }
      37             : 
      38             : /* This function is surprisingly simple.  The trick is understanding
      39             :  * that "child" is always a directory. So, to find its parent, you
      40             :  * simply need to find its ".." entry, normalize its block and offset,
      41             :  * and return the underlying inode.  See the comments for
      42             :  * isofs_normalize_block_and_offset(). */
      43             : static struct dentry *isofs_export_get_parent(struct dentry *child)
      44             : {
      45           2 :         unsigned long parent_block = 0;
      46           2 :         unsigned long parent_offset = 0;
      47           2 :         struct inode *child_inode = child->d_inode;
      48           4 :         struct iso_inode_info *e_child_inode = ISOFS_I(child_inode);
      49           2 :         struct iso_directory_record *de = NULL;
      50           2 :         struct buffer_head * bh = NULL;
      51           2 :         struct dentry *rv = NULL;
      52           1 : 
      53           1 :         /* "child" must always be a directory. */
      54           4 :         if (!S_ISDIR(child_inode->i_mode)) {
      55           2 :                 printk(KERN_ERR "isofs: isofs_export_get_parent(): "
      56           1 :                        "child is not a directory!\n");
      57           4 :                 rv = ERR_PTR(-EACCES);
      58           2 :                 goto out;
      59           1 :         }
      60             : 
      61             :         /* It is an invariant that the directory offset is zero.  If
      62             :          * it is not zero, it means the directory failed to be
      63             :          * normalized for some reason. */
      64           2 :         if (e_child_inode->i_iget5_offset != 0) {
      65           1 :                 printk(KERN_ERR "isofs: isofs_export_get_parent(): "
      66             :                        "child directory not normalized!\n");
      67           3 :                 rv = ERR_PTR(-EACCES);
      68           1 :                 goto out;
      69             :         }
      70             : 
      71             :         /* The child inode has been normalized such that its
      72             :          * i_iget5_block value points to the "." entry.  Fortunately,
      73             :          * the ".." entry is located in the same block. */
      74           1 :         parent_block = e_child_inode->i_iget5_block;
      75             : 
      76             :         /* Get the block in question. */
      77           2 :         bh = sb_bread(child_inode->i_sb, parent_block);
      78           2 :         if (bh == NULL) {
      79           3 :                 rv = ERR_PTR(-EACCES);
      80           1 :                 goto out;
      81             :         }
      82             : 
      83             :         /* This is the "." entry. */
      84           2 :         de = (struct iso_directory_record*)bh->b_data;
      85             : 
      86             :         /* The ".." entry is always the second entry. */
      87           3 :         parent_offset = (unsigned long)isonum_711(de->length);
      88           1 :         de = (struct iso_directory_record*)(bh->b_data + parent_offset);
      89             : 
      90             :         /* Verify it is in fact the ".." entry. */
      91           6 :         if ((isonum_711(de->name_len) != 1) || (de->name[0] != 1)) {
      92           1 :                 printk(KERN_ERR "isofs: Unable to find the \"..\" "
      93             :                        "directory for NFS.\n");
      94           3 :                 rv = ERR_PTR(-EACCES);
      95           1 :                 goto out;
      96             :         }
      97             : 
      98             :         /* Normalize */
      99           3 :         isofs_normalize_block_and_offset(de, &parent_block, &parent_offset);
     100             : 
     101           7 :         rv = d_obtain_alias(isofs_iget(child_inode->i_sb, parent_block,
     102             :                                      parent_offset));
     103           1 :  out:
     104          10 :         if (bh)
     105          10 :                 brelse(bh);
     106           6 :         return rv;
     107             : }
     108             : 
     109             : static int
     110             : isofs_export_encode_fh(struct dentry *dentry,
     111             :                        __u32 *fh32,
     112             :                        int *max_len,
     113           1 :                        int connectable)
     114           1 : {
     115           2 :         struct inode * inode = dentry->d_inode;
     116           4 :         struct iso_inode_info * ei = ISOFS_I(inode);
     117           2 :         int len = *max_len;
     118           2 :         int type = 1;
     119           2 :         __u16 *fh16 = (__u16*)fh32;
     120           1 : 
     121             :         /*
     122             :          * WARNING: max_len is 5 for NFSv2.  Because of this
     123             :          * limitation, we use the lower 16 bits of fh32[1] to hold the
     124             :          * offset of the inode and the upper 16 bits of fh32[1] to
     125             :          * hold the offset of the parent.
     126             :          */
     127             : 
     128           6 :         if (len < 3 || (connectable && len < 5))
     129           1 :                 return 255;
     130             : 
     131           1 :         len = 3;
     132           2 :         fh32[0] = ei->i_iget5_block;
     133           2 :         fh16[2] = (__u16)ei->i_iget5_offset;  /* fh16 [sic] */
     134           1 :         fh32[2] = inode->i_generation;
     135           5 :         if (connectable && !S_ISDIR(inode->i_mode)) {
     136             :                 struct inode *parent;
     137             :                 struct iso_inode_info *eparent;
     138           2 :                 spin_lock(&dentry->d_lock);
     139           1 :                 parent = dentry->d_parent->d_inode;
     140           2 :                 eparent = ISOFS_I(parent);
     141           2 :                 fh32[3] = eparent->i_iget5_block;
     142           2 :                 fh16[3] = (__u16)eparent->i_iget5_offset;  /* fh16 [sic] */
     143           1 :                 fh32[4] = parent->i_generation;
     144           2 :                 spin_unlock(&dentry->d_lock);
     145           1 :                 len = 5;
     146           1 :                 type = 2;
     147             :         }
     148           2 :         *max_len = len;
     149           2 :         return type;
     150             : }
     151           1 : 
     152             : struct isofs_fid {
     153             :         u32 block;
     154             :         u16 offset;
     155             :         u16 parent_offset;
     156             :         u32 generation;
     157             :         u32 parent_block;
     158             :         u32 parent_generation;
     159             : };
     160             : 
     161             : static struct dentry *isofs_fh_to_dentry(struct super_block *sb,
     162             :         struct fid *fid, int fh_len, int fh_type)
     163             : {
     164           2 :         struct isofs_fid *ifid = (struct isofs_fid *)fid;
     165           1 : 
     166           4 :         if (fh_len < 3 || fh_type > 2)
     167           1 :                 return NULL;
     168             : 
     169           8 :         return isofs_export_iget(sb, ifid->block, ifid->offset,
     170             :                         ifid->generation);
     171             : }
     172             : 
     173             : static struct dentry *isofs_fh_to_parent(struct super_block *sb,
     174             :                 struct fid *fid, int fh_len, int fh_type)
     175             : {
     176           2 :         struct isofs_fid *ifid = (struct isofs_fid *)fid;
     177           1 : 
     178           2 :         if (fh_type != 2)
     179           1 :                 return NULL;
     180             : 
     181          14 :         return isofs_export_iget(sb,
     182           4 :                         fh_len > 2 ? ifid->parent_block : 0,
     183             :                         ifid->parent_offset,
     184             :                         fh_len > 4 ? ifid->parent_generation : 0);
     185             : }
     186             : 
     187           1 : const struct export_operations isofs_export_ops = {
     188             :         .encode_fh      = isofs_export_encode_fh,
     189             :         .fh_to_dentry   = isofs_fh_to_dentry,
     190             :         .fh_to_parent   = isofs_fh_to_parent,
     191             :         .get_parent     = isofs_export_get_parent,
     192             : };

Generated by: LCOV version 1.10