1 /*
   2  * inode.h - Defines for NTFS inode handling. Part of the Linux-NTFS project.
   3  *
   4  * Copyright (c) 2001,2002 Anton Altaparmakov
   5  * Copyright (c) 2004-2007 Yura Pakhuchiy
   6  * Copyright (c) 2004-2005 Richard Russon
   7  *
   8  * This program/include file is free software; you can redistribute it and/or
   9  * modify it under the terms of the GNU General Public License as published
  10  * by the Free Software Foundation; either version 2 of the License, or
  11  * (at your option) any later version.
  12  *
  13  * This program/include file is distributed in the hope that it will be
  14  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  15  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16  * GNU General Public License for more details.
  17  *
  18  * You should have received a copy of the GNU General Public License
  19  * along with this program (in the main directory of the Linux-NTFS
  20  * distribution in the file COPYING); if not, write to the Free Software
  21  * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  22  */
  23 
  24 #ifndef _NTFS_INODE_H
  25 #define _NTFS_INODE_H
  26 
  27 /* Forward declaration */
  28 typedef struct _ntfs_inode ntfs_inode;
  29 
  30 #include "list.h"
  31 #include "types.h"
  32 #include "layout.h"
  33 #include "support.h"
  34 #include "volume.h"
  35 
  36 /**
  37  * enum ntfs_inode_state_bits -
  38  *
  39  * Defined bits for the state field in the ntfs_inode structure.
  40  * (f) = files only, (d) = directories only
  41  */
  42 typedef enum {
  43         NI_Dirty,               /* 1: Mft record needs to be written to disk. */
  44 
  45         /* Below fields only make sense for base inodes. */
  46         NI_AttrList,            /* 1: Mft record contains an attribute list. */
  47         NI_AttrListDirty,       /* 1: Attribute list needs to be written to the
  48                                       mft record and then to disk. */
  49         NI_FileNameDirty,       /* 1: FILE_NAME attributes need to be updated
  50                                       in the index. */
  51 } ntfs_inode_state_bits;
  52 
  53 #define  test_nino_flag(ni, flag)          test_bit(NI_##flag, (ni)->state)
  54 #define   set_nino_flag(ni, flag)           set_bit(NI_##flag, (ni)->state)
  55 #define clear_nino_flag(ni, flag)         clear_bit(NI_##flag, (ni)->state)
  56 
  57 #define test_and_set_nino_flag(ni, flag)        \
  58                                    test_and_set_bit(NI_##flag, (ni)->state)
  59 #define test_and_clear_nino_flag(ni, flag)      \
  60                                  test_and_clear_bit(NI_##flag, (ni)->state)
  61 
  62 #define NInoDirty(ni)                             test_nino_flag(ni, Dirty)
  63 #define NInoSetDirty(ni)                           set_nino_flag(ni, Dirty)
  64 #define NInoClearDirty(ni)                       clear_nino_flag(ni, Dirty)
  65 #define NInoTestAndSetDirty(ni)           test_and_set_nino_flag(ni, Dirty)
  66 #define NInoTestAndClearDirty(ni)       test_and_clear_nino_flag(ni, Dirty)
  67 
  68 #define NInoAttrList(ni)                          test_nino_flag(ni, AttrList)
  69 #define NInoSetAttrList(ni)                        set_nino_flag(ni, AttrList)
  70 #define NInoClearAttrList(ni)                    clear_nino_flag(ni, AttrList)
  71 
  72 
  73 #define  test_nino_al_flag(ni, flag)     test_nino_flag(ni, AttrList##flag)
  74 #define   set_nino_al_flag(ni, flag)      set_nino_flag(ni, AttrList##flag)
  75 #define clear_nino_al_flag(ni, flag)    clear_nino_flag(ni, AttrList##flag)
  76 
  77 #define test_and_set_nino_al_flag(ni, flag)     \
  78                                  test_and_set_nino_flag(ni, AttrList##flag)
  79 #define test_and_clear_nino_al_flag(ni, flag)   \
  80                                test_and_clear_nino_flag(ni, AttrList##flag)
  81 
  82 #define NInoAttrListDirty(ni)                       test_nino_al_flag(ni, Dirty)
  83 #define NInoAttrListSetDirty(ni)                     set_nino_al_flag(ni, Dirty)
  84 #define NInoAttrListClearDirty(ni)                 clear_nino_al_flag(ni, Dirty)
  85 #define NInoAttrListTestAndSetDirty(ni)     test_and_set_nino_al_flag(ni, Dirty)
  86 #define NInoAttrListTestAndClearDirty(ni) test_and_clear_nino_al_flag(ni, Dirty)
  87 
  88 #define NInoFileNameDirty(ni)                   \
  89                                           test_nino_flag(ni, FileNameDirty)
  90 #define NInoFileNameSetDirty(ni)                \
  91                                            set_nino_flag(ni, FileNameDirty)
  92 #define NInoFileNameClearDirty(ni)              \
  93                                          clear_nino_flag(ni, FileNameDirty)
  94 #define NInoFileNameTestAndSetDirty(ni)         \
  95                                   test_and_set_nino_flag(ni, FileNameDirty)
  96 #define NInoFileNameTestAndClearDirty(ni)       \
  97                                 test_and_clear_nino_flag(ni, FileNameDirty)
  98 
  99 /**
 100  * struct _ntfs_inode - The NTFS in-memory inode structure.
 101  *
 102  * It is just used as an extension to the fields already provided in the VFS
 103  * inode.
 104  */
 105 struct _ntfs_inode {
 106         u64 mft_no;             /* Inode / mft record number. */
 107         MFT_RECORD *mrec;       /* The actual mft record of the inode. */
 108         ntfs_volume *vol;       /* Pointer to the ntfs volume of this inode. */
 109         unsigned long state;    /* NTFS specific flags describing this inode.
 110                                    See ntfs_inode_state_bits above. */
 111         FILE_ATTR_FLAGS flags;  /* Flags describing the file.
 112                                    (Copy from STANDARD_INFORMATION) */
 113         /*
 114          * Attribute list support (for use by the attribute lookup functions).
 115          * Setup during ntfs_open_inode() for all inodes with attribute lists.
 116          * Only valid if NI_AttrList is set in state.
 117          */
 118         u32 attr_list_size;     /* Length of attribute list value in bytes. */
 119         u8 *attr_list;          /* Attribute list value itself. */
 120         /* Below fields are always valid. */
 121         s32 nr_extents;         /* For a base mft record, the number of
 122                                    attached extent inodes (0 if none), for
 123                                    extent records this is -1. */
 124         union {         /* This union is only used if nr_extents != 0. */
 125                 ntfs_inode **extent_nis;/* For nr_extents > 0, array of the
 126                                            ntfs inodes of the extent mft
 127                                            records belonging to this base
 128                                            inode which have been loaded. */
 129                 ntfs_inode *base_ni;    /* For nr_extents == -1, the ntfs
 130                                            inode of the base mft record. */
 131         } u;
 132 
 133         /* Below fields are valid only for base inode. */
 134 
 135         /*
 136          * These two fields are used to sync filename index and guaranteed to be
 137          * correct, however value in index itself maybe wrong (windows itself
 138          * do not update them properly).
 139          */
 140         s64 data_size;          /* Data size of unnamed DATA attribute. */
 141         s64 allocated_size;     /* Allocated size stored in the filename
 142                                    index. (NOTE: Equal to allocated size of
 143                                    the unnamed data attribute for normal or
 144                                    encrypted files and to compressed size
 145                                    of the unnamed data attribute for sparse or
 146                                    compressed files.) */
 147 
 148         /*
 149          * These four fields are copy of relevant fields from
 150          * STANDARD_INFORMATION attribute and used to sync it and FILE_NAME
 151          * attribute in the index.
 152          */
 153         time_t creation_time;
 154         time_t last_data_change_time;
 155         time_t last_mft_change_time;
 156         time_t last_access_time;
 157 
 158         /* These 2 fields are used to keep track of opened inodes. */
 159         struct list_head list_entry;    /* Keep pointers to the next/prev list
 160                                            entry. */
 161         int nr_references;              /* How many times this inode was
 162                                            opened.  We really close inode only
 163                                            when this reaches zero. */
 164 
 165         struct list_head attr_cache;    /* List of opened attributes. */
 166 };
 167 
 168 extern void __ntfs_inode_add_to_cache(ntfs_inode *ni);
 169 
 170 extern ntfs_inode *ntfs_inode_allocate(ntfs_volume *vol);
 171 
 172 extern ntfs_inode *ntfs_inode_open(ntfs_volume *vol, const MFT_REF mref);
 173 
 174 extern int ntfs_inode_close(ntfs_inode *ni);
 175 
 176 extern ntfs_inode *ntfs_extent_inode_open(ntfs_inode *base_ni,
 177                 const leMFT_REF mref);
 178 
 179 extern int ntfs_inode_attach_all_extents(ntfs_inode *ni);
 180 
 181 /**
 182  * ntfs_inode_mark_dirty - set the inode (and its base inode if it exists) dirty
 183  * @ni:         ntfs inode to set dirty
 184  *
 185  * Set the inode @ni dirty so it is written out later (at the latest at
 186  * ntfs_inode_close() time). If @ni is an extent inode, set the base inode
 187  * dirty, too.
 188  *
 189  * This function cannot fail.
 190  */
 191 static __inline__ void ntfs_inode_mark_dirty(ntfs_inode *ni)
 192 {
 193         NInoSetDirty(ni);
 194         if (ni->nr_extents == -1)
 195                 NInoSetDirty(ni->u.base_ni);
 196 }
 197 
 198 typedef enum {
 199         NTFS_UPDATE_ATIME = 1 << 0,
 200         NTFS_UPDATE_MTIME = 1 << 1,
 201         NTFS_UPDATE_CTIME = 1 << 2,
 202 } ntfs_time_update_flags;
 203 
 204 extern void ntfs_inode_update_times(ntfs_inode *ni,
 205                 ntfs_time_update_flags mask);
 206 
 207 extern int ntfs_inode_sync(ntfs_inode *ni);
 208 
 209 extern int ntfs_inode_add_attrlist(ntfs_inode *ni);
 210 
 211 extern int ntfs_inode_free_space(ntfs_inode *ni, int size);
 212 
 213 extern int ntfs_inode_badclus_bad(u64 mft_no, ATTR_RECORD *a);
 214 
 215 #endif /* defined _NTFS_INODE_H */