VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / fs / reiserfs / dir.c
1 /*
2  * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
3  */
4
5 #include <linux/config.h>
6 #include <linux/string.h>
7 #include <linux/errno.h>
8 #include <linux/fs.h>
9 #include <linux/reiserfs_fs.h>
10 #include <linux/stat.h>
11 #include <linux/smp_lock.h>
12 #include <linux/buffer_head.h>
13 #include <asm/uaccess.h>
14
15 extern struct key  MIN_KEY;
16
17 static int reiserfs_readdir (struct file *, void *, filldir_t);
18 int reiserfs_dir_fsync(struct file *filp, struct dentry *dentry, int datasync) ;
19
20 struct file_operations reiserfs_dir_operations = {
21     .read       = generic_read_dir,
22     .readdir    = reiserfs_readdir,
23     .fsync      = reiserfs_dir_fsync,
24     .ioctl      = reiserfs_ioctl,
25 };
26
27 int reiserfs_dir_fsync(struct file *filp, struct dentry *dentry, int datasync) {
28   struct inode *inode = dentry->d_inode;
29   reiserfs_write_lock(inode->i_sb);
30   reiserfs_commit_for_inode(inode) ;
31   reiserfs_write_unlock(inode->i_sb) ;
32   return 0 ;
33 }
34
35
36 #define store_ih(where,what) copy_item_head (where, what)
37
38 //
39 static int reiserfs_readdir (struct file * filp, void * dirent, filldir_t filldir)
40 {
41     struct inode *inode = filp->f_dentry->d_inode;
42     struct cpu_key pos_key;     /* key of current position in the directory (key of directory entry) */
43     INITIALIZE_PATH (path_to_entry);
44     struct buffer_head * bh;
45     int item_num, entry_num;
46     const struct key * rkey;
47     struct item_head * ih, tmp_ih;
48     int search_res;
49     char * local_buf;
50     loff_t next_pos;
51     char small_buf[32] ; /* avoid kmalloc if we can */
52     struct reiserfs_dir_entry de;
53     int ret = 0;
54
55     reiserfs_write_lock(inode->i_sb);
56
57     reiserfs_check_lock_depth(inode->i_sb, "readdir") ;
58
59     /* form key for search the next directory entry using f_pos field of
60        file structure */
61     make_cpu_key (&pos_key, inode, (filp->f_pos) ? (filp->f_pos) : DOT_OFFSET,
62                   TYPE_DIRENTRY, 3);
63     next_pos = cpu_key_k_offset (&pos_key);
64
65     /*  reiserfs_warning (inode->i_sb, "reiserfs_readdir 1: f_pos = %Ld", filp->f_pos);*/
66
67     path_to_entry.reada = PATH_READA;
68     while (1) {
69     research:
70         /* search the directory item, containing entry with specified key */
71         search_res = search_by_entry_key (inode->i_sb, &pos_key, &path_to_entry, &de);
72         if (search_res == IO_ERROR) {
73             // FIXME: we could just skip part of directory which could
74             // not be read
75             ret = -EIO;
76             goto out;
77         }
78         entry_num = de.de_entry_num;
79         bh = de.de_bh;
80         item_num = de.de_item_num;
81         ih = de.de_ih;
82         store_ih (&tmp_ih, ih);
83                 
84         /* we must have found item, that is item of this directory, */
85         RFALSE( COMP_SHORT_KEYS (&(ih->ih_key), &pos_key),
86                 "vs-9000: found item %h does not match to dir we readdir %K",
87                 ih, &pos_key);
88         RFALSE( item_num > B_NR_ITEMS (bh) - 1,
89                 "vs-9005 item_num == %d, item amount == %d", 
90                 item_num, B_NR_ITEMS (bh));
91       
92         /* and entry must be not more than number of entries in the item */
93         RFALSE( I_ENTRY_COUNT (ih) < entry_num,
94                 "vs-9010: entry number is too big %d (%d)", 
95                 entry_num, I_ENTRY_COUNT (ih));
96
97         if (search_res == POSITION_FOUND || entry_num < I_ENTRY_COUNT (ih)) {
98             /* go through all entries in the directory item beginning from the entry, that has been found */
99             struct reiserfs_de_head * deh = B_I_DEH (bh, ih) + entry_num;
100
101             for (; entry_num < I_ENTRY_COUNT (ih); entry_num ++, deh ++) {
102                 int d_reclen;
103                 char * d_name;
104                 off_t d_off;
105                 ino_t d_ino;
106
107                 if (!de_visible (deh))
108                     /* it is hidden entry */
109                     continue;
110                 d_reclen = entry_length (bh, ih, entry_num);
111                 d_name = B_I_DEH_ENTRY_FILE_NAME (bh, ih, deh);
112                 if (!d_name[d_reclen - 1])
113                     d_reclen = strlen (d_name);
114         
115                 if (d_reclen > REISERFS_MAX_NAME(inode->i_sb->s_blocksize)){
116                     /* too big to send back to VFS */
117                     continue ;
118                 }
119
120                 /* Ignore the .reiserfs_priv entry */
121                 if (reiserfs_xattrs (inode->i_sb) &&
122                     !old_format_only(inode->i_sb) &&
123                     filp->f_dentry == inode->i_sb->s_root &&
124                     REISERFS_SB(inode->i_sb)->priv_root &&
125                     REISERFS_SB(inode->i_sb)->priv_root->d_inode &&
126                     deh_objectid(deh) == le32_to_cpu (INODE_PKEY(REISERFS_SB(inode->i_sb)->priv_root->d_inode)->k_objectid)) {
127                   continue;
128                 }
129
130                 d_off = deh_offset (deh);
131                 filp->f_pos = d_off ;
132                 d_ino = deh_objectid (deh);
133                 if (d_reclen <= 32) {
134                   local_buf = small_buf ;
135                 } else {
136                     local_buf = reiserfs_kmalloc(d_reclen, GFP_NOFS, inode->i_sb) ;
137                     if (!local_buf) {
138                         pathrelse (&path_to_entry);
139                         ret = -ENOMEM ;
140                         goto out;
141                     }
142                     if (item_moved (&tmp_ih, &path_to_entry)) {
143                         reiserfs_kfree(local_buf, d_reclen, inode->i_sb) ;
144                         goto research;
145                     }
146                 }
147                 // Note, that we copy name to user space via temporary
148                 // buffer (local_buf) because filldir will block if
149                 // user space buffer is swapped out. At that time
150                 // entry can move to somewhere else
151                 memcpy (local_buf, d_name, d_reclen);
152                 if (filldir (dirent, local_buf, d_reclen, d_off, d_ino, 
153                              DT_UNKNOWN) < 0) {
154                     if (local_buf != small_buf) {
155                         reiserfs_kfree(local_buf, d_reclen, inode->i_sb) ;
156                     }
157                     goto end;
158                 }
159                 if (local_buf != small_buf) {
160                     reiserfs_kfree(local_buf, d_reclen, inode->i_sb) ;
161                 }
162
163                 // next entry should be looked for with such offset
164                 next_pos = deh_offset (deh) + 1;
165
166                 if (item_moved (&tmp_ih, &path_to_entry)) {
167                     goto research;
168                 }
169             } /* for */
170         }
171
172         if (item_num != B_NR_ITEMS (bh) - 1)
173             // end of directory has been reached
174             goto end;
175
176         /* item we went through is last item of node. Using right
177            delimiting key check is it directory end */
178         rkey = get_rkey (&path_to_entry, inode->i_sb);
179         if (! comp_le_keys (rkey, &MIN_KEY)) {
180             /* set pos_key to key, that is the smallest and greater
181                that key of the last entry in the item */
182             set_cpu_key_k_offset (&pos_key, next_pos);
183             continue;
184         }
185
186         if ( COMP_SHORT_KEYS (rkey, &pos_key)) {
187             // end of directory has been reached
188             goto end;
189         }
190         
191         /* directory continues in the right neighboring block */
192         set_cpu_key_k_offset (&pos_key, le_key_k_offset (KEY_FORMAT_3_5, rkey));
193
194     } /* while */
195
196
197  end:
198     filp->f_pos = next_pos;
199     pathrelse (&path_to_entry);
200     reiserfs_check_path(&path_to_entry) ;
201  out:
202     reiserfs_write_unlock(inode->i_sb);
203     return ret;
204 }
205
206 /* compose directory item containing "." and ".." entries (entries are
207    not aligned to 4 byte boundary) */
208 /* the last four params are LE */
209 void make_empty_dir_item_v1 (char * body, __u32 dirid, __u32 objid,
210                              __u32 par_dirid, __u32 par_objid)
211 {
212     struct reiserfs_de_head * deh;
213
214     memset (body, 0, EMPTY_DIR_SIZE_V1);
215     deh = (struct reiserfs_de_head *)body;
216     
217     /* direntry header of "." */
218     put_deh_offset( &(deh[0]), DOT_OFFSET );
219     /* these two are from make_le_item_head, and are are LE */
220     deh[0].deh_dir_id = dirid;
221     deh[0].deh_objectid = objid;
222     deh[0].deh_state = 0; /* Endian safe if 0 */
223     put_deh_location( &(deh[0]), EMPTY_DIR_SIZE_V1 - strlen( "." ));
224     mark_de_visible(&(deh[0]));
225   
226     /* direntry header of ".." */
227     put_deh_offset( &(deh[1]), DOT_DOT_OFFSET);
228     /* key of ".." for the root directory */
229     /* these two are from the inode, and are are LE */
230     deh[1].deh_dir_id = par_dirid;
231     deh[1].deh_objectid = par_objid;
232     deh[1].deh_state = 0; /* Endian safe if 0 */
233     put_deh_location( &(deh[1]), deh_location( &(deh[0]) ) - strlen( ".." ) );
234     mark_de_visible(&(deh[1]));
235
236     /* copy ".." and "." */
237     memcpy (body + deh_location( &(deh[0]) ), ".", 1);
238     memcpy (body + deh_location( &(deh[1]) ), "..", 2);
239 }
240
241 /* compose directory item containing "." and ".." entries */
242 void make_empty_dir_item (char * body, __u32 dirid, __u32 objid,
243                           __u32 par_dirid, __u32 par_objid)
244 {
245     struct reiserfs_de_head * deh;
246
247     memset (body, 0, EMPTY_DIR_SIZE);
248     deh = (struct reiserfs_de_head *)body;
249     
250     /* direntry header of "." */
251     put_deh_offset( &(deh[0]), DOT_OFFSET );
252     /* these two are from make_le_item_head, and are are LE */
253     deh[0].deh_dir_id = dirid;
254     deh[0].deh_objectid = objid;
255     deh[0].deh_state = 0; /* Endian safe if 0 */
256     put_deh_location( &(deh[0]), EMPTY_DIR_SIZE - ROUND_UP( strlen( "." ) ) );
257     mark_de_visible(&(deh[0]));
258   
259     /* direntry header of ".." */
260     put_deh_offset( &(deh[1]), DOT_DOT_OFFSET );
261     /* key of ".." for the root directory */
262     /* these two are from the inode, and are are LE */
263     deh[1].deh_dir_id = par_dirid;
264     deh[1].deh_objectid = par_objid;
265     deh[1].deh_state = 0; /* Endian safe if 0 */
266     put_deh_location( &(deh[1]), deh_location( &(deh[0])) - ROUND_UP( strlen( ".." ) ) );
267     mark_de_visible(&(deh[1]));
268
269     /* copy ".." and "." */
270     memcpy (body + deh_location( &(deh[0]) ), ".", 1);
271     memcpy (body + deh_location( &(deh[1]) ), "..", 2);
272 }