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