ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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("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 ("reiserfs_readdir 1: f_pos = %Ld\n", filp->f_pos);*/
66
67     while (1) {
68     research:
69         /* search the directory item, containing entry with specified key */
70         search_res = search_by_entry_key (inode->i_sb, &pos_key, &path_to_entry, &de);
71         if (search_res == IO_ERROR) {
72             // FIXME: we could just skip part of directory which could
73             // not be read
74             ret = -EIO;
75             goto out;
76         }
77         entry_num = de.de_entry_num;
78         bh = de.de_bh;
79         item_num = de.de_item_num;
80         ih = de.de_ih;
81         store_ih (&tmp_ih, ih);
82                 
83         /* we must have found item, that is item of this directory, */
84         RFALSE( COMP_SHORT_KEYS (&(ih->ih_key), &pos_key),
85                 "vs-9000: found item %h does not match to dir we readdir %K",
86                 ih, &pos_key);
87         RFALSE( item_num > B_NR_ITEMS (bh) - 1,
88                 "vs-9005 item_num == %d, item amount == %d", 
89                 item_num, B_NR_ITEMS (bh));
90       
91         /* and entry must be not more than number of entries in the item */
92         RFALSE( I_ENTRY_COUNT (ih) < entry_num,
93                 "vs-9010: entry number is too big %d (%d)", 
94                 entry_num, I_ENTRY_COUNT (ih));
95
96         if (search_res == POSITION_FOUND || entry_num < I_ENTRY_COUNT (ih)) {
97             /* go through all entries in the directory item beginning from the entry, that has been found */
98             struct reiserfs_de_head * deh = B_I_DEH (bh, ih) + entry_num;
99
100             for (; entry_num < I_ENTRY_COUNT (ih); entry_num ++, deh ++) {
101                 int d_reclen;
102                 char * d_name;
103                 off_t d_off;
104                 ino_t d_ino;
105
106                 if (!de_visible (deh))
107                     /* it is hidden entry */
108                     continue;
109                 d_reclen = entry_length (bh, ih, entry_num);
110                 d_name = B_I_DEH_ENTRY_FILE_NAME (bh, ih, deh);
111                 if (!d_name[d_reclen - 1])
112                     d_reclen = strlen (d_name);
113         
114                 if (d_reclen > REISERFS_MAX_NAME(inode->i_sb->s_blocksize)){
115                     /* too big to send back to VFS */
116                     continue ;
117                 }
118                 d_off = deh_offset (deh);
119                 filp->f_pos = d_off ;
120                 d_ino = deh_objectid (deh);
121                 if (d_reclen <= 32) {
122                   local_buf = small_buf ;
123                 } else {
124                     local_buf = reiserfs_kmalloc(d_reclen, GFP_NOFS, inode->i_sb) ;
125                     if (!local_buf) {
126                         pathrelse (&path_to_entry);
127                         ret = -ENOMEM ;
128                         goto out;
129                     }
130                     if (item_moved (&tmp_ih, &path_to_entry)) {
131                         reiserfs_kfree(local_buf, d_reclen, inode->i_sb) ;
132                         goto research;
133                     }
134                 }
135                 // Note, that we copy name to user space via temporary
136                 // buffer (local_buf) because filldir will block if
137                 // user space buffer is swapped out. At that time
138                 // entry can move to somewhere else
139                 memcpy (local_buf, d_name, d_reclen);
140                 if (filldir (dirent, local_buf, d_reclen, d_off, d_ino, 
141                              DT_UNKNOWN) < 0) {
142                     if (local_buf != small_buf) {
143                         reiserfs_kfree(local_buf, d_reclen, inode->i_sb) ;
144                     }
145                     goto end;
146                 }
147                 if (local_buf != small_buf) {
148                     reiserfs_kfree(local_buf, d_reclen, inode->i_sb) ;
149                 }
150
151                 // next entry should be looked for with such offset
152                 next_pos = deh_offset (deh) + 1;
153
154                 if (item_moved (&tmp_ih, &path_to_entry)) {
155                     goto research;
156                 }
157             } /* for */
158         }
159
160         if (item_num != B_NR_ITEMS (bh) - 1)
161             // end of directory has been reached
162             goto end;
163
164         /* item we went through is last item of node. Using right
165            delimiting key check is it directory end */
166         rkey = get_rkey (&path_to_entry, inode->i_sb);
167         if (! comp_le_keys (rkey, &MIN_KEY)) {
168             /* set pos_key to key, that is the smallest and greater
169                that key of the last entry in the item */
170             set_cpu_key_k_offset (&pos_key, next_pos);
171             continue;
172         }
173
174         if ( COMP_SHORT_KEYS (rkey, &pos_key)) {
175             // end of directory has been reached
176             goto end;
177         }
178         
179         /* directory continues in the right neighboring block */
180         set_cpu_key_k_offset (&pos_key, le_key_k_offset (KEY_FORMAT_3_5, rkey));
181
182     } /* while */
183
184
185  end:
186     filp->f_pos = next_pos;
187     pathrelse (&path_to_entry);
188     reiserfs_check_path(&path_to_entry) ;
189  out:
190     reiserfs_write_unlock(inode->i_sb);
191     return ret;
192 }
193
194 /* compose directory item containing "." and ".." entries (entries are
195    not aligned to 4 byte boundary) */
196 /* the last four params are LE */
197 void make_empty_dir_item_v1 (char * body, __u32 dirid, __u32 objid,
198                              __u32 par_dirid, __u32 par_objid)
199 {
200     struct reiserfs_de_head * deh;
201
202     memset (body, 0, EMPTY_DIR_SIZE_V1);
203     deh = (struct reiserfs_de_head *)body;
204     
205     /* direntry header of "." */
206     put_deh_offset( &(deh[0]), DOT_OFFSET );
207     /* these two are from make_le_item_head, and are are LE */
208     deh[0].deh_dir_id = dirid;
209     deh[0].deh_objectid = objid;
210     deh[0].deh_state = 0; /* Endian safe if 0 */
211     put_deh_location( &(deh[0]), EMPTY_DIR_SIZE_V1 - strlen( "." ));
212     mark_de_visible(&(deh[0]));
213   
214     /* direntry header of ".." */
215     put_deh_offset( &(deh[1]), DOT_DOT_OFFSET);
216     /* key of ".." for the root directory */
217     /* these two are from the inode, and are are LE */
218     deh[1].deh_dir_id = par_dirid;
219     deh[1].deh_objectid = par_objid;
220     deh[1].deh_state = 0; /* Endian safe if 0 */
221     put_deh_location( &(deh[1]), deh_location( &(deh[0]) ) - strlen( ".." ) );
222     mark_de_visible(&(deh[1]));
223
224     /* copy ".." and "." */
225     memcpy (body + deh_location( &(deh[0]) ), ".", 1);
226     memcpy (body + deh_location( &(deh[1]) ), "..", 2);
227 }
228
229 /* compose directory item containing "." and ".." entries */
230 void make_empty_dir_item (char * body, __u32 dirid, __u32 objid,
231                           __u32 par_dirid, __u32 par_objid)
232 {
233     struct reiserfs_de_head * deh;
234
235     memset (body, 0, EMPTY_DIR_SIZE);
236     deh = (struct reiserfs_de_head *)body;
237     
238     /* direntry header of "." */
239     put_deh_offset( &(deh[0]), DOT_OFFSET );
240     /* these two are from make_le_item_head, and are are LE */
241     deh[0].deh_dir_id = dirid;
242     deh[0].deh_objectid = objid;
243     deh[0].deh_state = 0; /* Endian safe if 0 */
244     put_deh_location( &(deh[0]), EMPTY_DIR_SIZE - ROUND_UP( strlen( "." ) ) );
245     mark_de_visible(&(deh[0]));
246   
247     /* direntry header of ".." */
248     put_deh_offset( &(deh[1]), DOT_DOT_OFFSET );
249     /* key of ".." for the root directory */
250     /* these two are from the inode, and are are LE */
251     deh[1].deh_dir_id = par_dirid;
252     deh[1].deh_objectid = par_objid;
253     deh[1].deh_state = 0; /* Endian safe if 0 */
254     put_deh_location( &(deh[1]), deh_location( &(deh[0])) - ROUND_UP( strlen( ".." ) ) );
255     mark_de_visible(&(deh[1]));
256
257     /* copy ".." and "." */
258     memcpy (body + deh_location( &(deh[0]) ), ".", 1);
259     memcpy (body + deh_location( &(deh[1]) ), "..", 2);
260 }