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