Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2001 Clemson University and The University of Chicago |
| 3 | * |
| 4 | * See COPYING in top-level directory. |
| 5 | */ |
| 6 | |
| 7 | #include "protocol.h" |
Mike Marshall | 575e946 | 2015-12-04 12:56:14 -0500 | [diff] [blame] | 8 | #include "orangefs-kernel.h" |
| 9 | #include "orangefs-bufmap.h" |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 10 | |
| 11 | struct readdir_handle_s { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 12 | struct orangefs_readdir_response_s readdir_response; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 13 | void *dents_buf; |
| 14 | }; |
| 15 | |
| 16 | /* |
Mike Marshall | 1808f8c | 2016-01-15 13:10:52 -0500 | [diff] [blame] | 17 | * decode routine used by kmod to deal with the blob sent from |
| 18 | * userspace for readdirs. The blob contains zero or more of these |
| 19 | * sub-blobs: |
| 20 | * __u32 - represents length of the character string that follows. |
| 21 | * string - between 1 and ORANGEFS_NAME_MAX bytes long. |
| 22 | * padding - (if needed) to cause the __u32 plus the string to be |
| 23 | * eight byte aligned. |
| 24 | * khandle - sizeof(khandle) bytes. |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 25 | */ |
Al Viro | 8092895 | 2015-10-09 18:11:10 -0400 | [diff] [blame] | 26 | static long decode_dirents(char *ptr, size_t size, |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 27 | struct orangefs_readdir_response_s *readdir) |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 28 | { |
| 29 | int i; |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 30 | struct orangefs_readdir_response_s *rd = |
| 31 | (struct orangefs_readdir_response_s *) ptr; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 32 | char *buf = ptr; |
Mike Marshall | 1808f8c | 2016-01-15 13:10:52 -0500 | [diff] [blame] | 33 | int khandle_size = sizeof(struct orangefs_khandle); |
| 34 | size_t offset = offsetof(struct orangefs_readdir_response_s, |
| 35 | dirent_array); |
| 36 | /* 8 reflects eight byte alignment */ |
| 37 | int smallest_blob = khandle_size + 8; |
| 38 | __u32 len; |
| 39 | int aligned_len; |
| 40 | int sizeof_u32 = sizeof(__u32); |
| 41 | long ret; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 42 | |
Mike Marshall | 1808f8c | 2016-01-15 13:10:52 -0500 | [diff] [blame] | 43 | gossip_debug(GOSSIP_DIR_DEBUG, "%s: size:%zu:\n", __func__, size); |
| 44 | |
| 45 | /* size is = offset on empty dirs, > offset on non-empty dirs... */ |
| 46 | if (size < offset) { |
| 47 | gossip_err("%s: size:%zu: offset:%zu:\n", |
| 48 | __func__, |
| 49 | size, |
| 50 | offset); |
| 51 | ret = -EINVAL; |
| 52 | goto out; |
| 53 | } |
| 54 | |
| 55 | if ((size == offset) && (readdir->orangefs_dirent_outcount != 0)) { |
| 56 | gossip_err("%s: size:%zu: dirent_outcount:%d:\n", |
| 57 | __func__, |
| 58 | size, |
| 59 | readdir->orangefs_dirent_outcount); |
| 60 | ret = -EINVAL; |
| 61 | goto out; |
| 62 | } |
Al Viro | 8092895 | 2015-10-09 18:11:10 -0400 | [diff] [blame] | 63 | |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 64 | readdir->token = rd->token; |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 65 | readdir->orangefs_dirent_outcount = rd->orangefs_dirent_outcount; |
| 66 | readdir->dirent_array = kcalloc(readdir->orangefs_dirent_outcount, |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 67 | sizeof(*readdir->dirent_array), |
| 68 | GFP_KERNEL); |
Mike Marshall | 1808f8c | 2016-01-15 13:10:52 -0500 | [diff] [blame] | 69 | if (readdir->dirent_array == NULL) { |
| 70 | gossip_err("%s: kcalloc failed.\n", __func__); |
| 71 | ret = -ENOMEM; |
| 72 | goto out; |
| 73 | } |
Al Viro | 8092895 | 2015-10-09 18:11:10 -0400 | [diff] [blame] | 74 | |
Mike Marshall | 1808f8c | 2016-01-15 13:10:52 -0500 | [diff] [blame] | 75 | buf += offset; |
| 76 | size -= offset; |
Al Viro | 8092895 | 2015-10-09 18:11:10 -0400 | [diff] [blame] | 77 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 78 | for (i = 0; i < readdir->orangefs_dirent_outcount; i++) { |
Mike Marshall | 1808f8c | 2016-01-15 13:10:52 -0500 | [diff] [blame] | 79 | if (size < smallest_blob) { |
| 80 | gossip_err("%s: size:%zu: smallest_blob:%d:\n", |
| 81 | __func__, |
| 82 | size, |
| 83 | smallest_blob); |
| 84 | ret = -EINVAL; |
| 85 | goto free; |
| 86 | } |
Al Viro | 8092895 | 2015-10-09 18:11:10 -0400 | [diff] [blame] | 87 | |
| 88 | len = *(__u32 *)buf; |
Mike Marshall | 1808f8c | 2016-01-15 13:10:52 -0500 | [diff] [blame] | 89 | if ((len < 1) || (len > ORANGEFS_NAME_MAX)) { |
| 90 | gossip_err("%s: len:%d:\n", __func__, len); |
| 91 | ret = -EINVAL; |
| 92 | goto free; |
| 93 | } |
Al Viro | 8092895 | 2015-10-09 18:11:10 -0400 | [diff] [blame] | 94 | |
Mike Marshall | 1808f8c | 2016-01-15 13:10:52 -0500 | [diff] [blame] | 95 | gossip_debug(GOSSIP_DIR_DEBUG, |
| 96 | "%s: size:%zu: len:%d:\n", |
| 97 | __func__, |
| 98 | size, |
| 99 | len); |
| 100 | |
| 101 | readdir->dirent_array[i].d_name = buf + sizeof_u32; |
Al Viro | 9be68b0 | 2015-10-09 17:43:15 -0400 | [diff] [blame] | 102 | readdir->dirent_array[i].d_length = len; |
Al Viro | 8092895 | 2015-10-09 18:11:10 -0400 | [diff] [blame] | 103 | |
Martin Brandenburg | 7d22148 | 2016-01-04 15:05:28 -0500 | [diff] [blame] | 104 | /* |
Mike Marshall | 1808f8c | 2016-01-15 13:10:52 -0500 | [diff] [blame] | 105 | * Calculate "aligned" length of this string and its |
| 106 | * associated __u32 descriptor. |
Martin Brandenburg | 7d22148 | 2016-01-04 15:05:28 -0500 | [diff] [blame] | 107 | */ |
Mike Marshall | 1808f8c | 2016-01-15 13:10:52 -0500 | [diff] [blame] | 108 | aligned_len = ((sizeof_u32 + len + 1) + 7) & ~7; |
| 109 | gossip_debug(GOSSIP_DIR_DEBUG, |
| 110 | "%s: aligned_len:%d:\n", |
| 111 | __func__, |
| 112 | aligned_len); |
Al Viro | 8092895 | 2015-10-09 18:11:10 -0400 | [diff] [blame] | 113 | |
Mike Marshall | 1808f8c | 2016-01-15 13:10:52 -0500 | [diff] [blame] | 114 | /* |
| 115 | * The end of the blob should coincide with the end |
| 116 | * of the last sub-blob. |
| 117 | */ |
| 118 | if (size < aligned_len + khandle_size) { |
| 119 | gossip_err("%s: ran off the end of the blob.\n", |
| 120 | __func__); |
| 121 | ret = -EINVAL; |
| 122 | goto free; |
| 123 | } |
| 124 | size -= aligned_len + khandle_size; |
| 125 | |
| 126 | buf += aligned_len; |
Al Viro | 8092895 | 2015-10-09 18:11:10 -0400 | [diff] [blame] | 127 | |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 128 | readdir->dirent_array[i].khandle = |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 129 | *(struct orangefs_khandle *) buf; |
Mike Marshall | 1808f8c | 2016-01-15 13:10:52 -0500 | [diff] [blame] | 130 | buf += khandle_size; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 131 | } |
Mike Marshall | 1808f8c | 2016-01-15 13:10:52 -0500 | [diff] [blame] | 132 | ret = buf - ptr; |
| 133 | gossip_debug(GOSSIP_DIR_DEBUG, "%s: returning:%ld:\n", __func__, ret); |
| 134 | goto out; |
| 135 | |
| 136 | free: |
Al Viro | 8092895 | 2015-10-09 18:11:10 -0400 | [diff] [blame] | 137 | kfree(readdir->dirent_array); |
| 138 | readdir->dirent_array = NULL; |
Mike Marshall | 1808f8c | 2016-01-15 13:10:52 -0500 | [diff] [blame] | 139 | |
| 140 | out: |
| 141 | return ret; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | static long readdir_handle_ctor(struct readdir_handle_s *rhandle, void *buf, |
Martin Brandenburg | ee3b8d3 | 2016-02-17 12:55:42 -0500 | [diff] [blame^] | 145 | size_t size) |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 146 | { |
| 147 | long ret; |
| 148 | |
| 149 | if (buf == NULL) { |
| 150 | gossip_err |
| 151 | ("Invalid NULL buffer specified in readdir_handle_ctor\n"); |
| 152 | return -ENOMEM; |
| 153 | } |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 154 | rhandle->dents_buf = buf; |
Al Viro | 8092895 | 2015-10-09 18:11:10 -0400 | [diff] [blame] | 155 | ret = decode_dirents(buf, size, &rhandle->readdir_response); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 156 | if (ret < 0) { |
| 157 | gossip_err("Could not decode readdir from buffer %ld\n", ret); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 158 | gossip_debug(GOSSIP_DIR_DEBUG, "vfree %p\n", buf); |
| 159 | vfree(buf); |
| 160 | rhandle->dents_buf = NULL; |
| 161 | } |
| 162 | return ret; |
| 163 | } |
| 164 | |
Al Viro | 82d37f1 | 2016-02-13 21:04:51 -0500 | [diff] [blame] | 165 | static void readdir_handle_dtor(struct readdir_handle_s *rhandle) |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 166 | { |
| 167 | if (rhandle == NULL) |
| 168 | return; |
| 169 | |
| 170 | /* kfree(NULL) is safe */ |
| 171 | kfree(rhandle->readdir_response.dirent_array); |
| 172 | rhandle->readdir_response.dirent_array = NULL; |
| 173 | |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 174 | if (rhandle->dents_buf) { |
| 175 | gossip_debug(GOSSIP_DIR_DEBUG, "vfree %p\n", |
| 176 | rhandle->dents_buf); |
| 177 | vfree(rhandle->dents_buf); |
| 178 | rhandle->dents_buf = NULL; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * Read directory entries from an instance of an open directory. |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 184 | */ |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 185 | static int orangefs_readdir(struct file *file, struct dir_context *ctx) |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 186 | { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 187 | struct orangefs_bufmap *bufmap = NULL; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 188 | int ret = 0; |
| 189 | int buffer_index; |
Mike Marshall | 88309aa | 2015-09-23 16:48:40 -0400 | [diff] [blame] | 190 | /* |
| 191 | * ptoken supports Orangefs' distributed directory logic, added |
| 192 | * in 2.9.2. |
| 193 | */ |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 194 | __u64 *ptoken = file->private_data; |
| 195 | __u64 pos = 0; |
| 196 | ino_t ino = 0; |
| 197 | struct dentry *dentry = file->f_path.dentry; |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 198 | struct orangefs_kernel_op_s *new_op = NULL; |
| 199 | struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(dentry->d_inode); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 200 | int buffer_full = 0; |
| 201 | struct readdir_handle_s rhandle; |
| 202 | int i = 0; |
| 203 | int len = 0; |
| 204 | ino_t current_ino = 0; |
| 205 | char *current_entry = NULL; |
| 206 | long bytes_decoded; |
| 207 | |
Mike Marshall | 88309aa | 2015-09-23 16:48:40 -0400 | [diff] [blame] | 208 | gossip_debug(GOSSIP_DIR_DEBUG, |
| 209 | "%s: ctx->pos:%lld, ptoken = %llu\n", |
| 210 | __func__, |
| 211 | lld(ctx->pos), |
| 212 | llu(*ptoken)); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 213 | |
| 214 | pos = (__u64) ctx->pos; |
| 215 | |
| 216 | /* are we done? */ |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 217 | if (pos == ORANGEFS_READDIR_END) { |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 218 | gossip_debug(GOSSIP_DIR_DEBUG, |
| 219 | "Skipping to termination path\n"); |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | gossip_debug(GOSSIP_DIR_DEBUG, |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 224 | "orangefs_readdir called on %s (pos=%llu)\n", |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 225 | dentry->d_name.name, llu(pos)); |
| 226 | |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 227 | rhandle.dents_buf = NULL; |
| 228 | memset(&rhandle.readdir_response, 0, sizeof(rhandle.readdir_response)); |
| 229 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 230 | new_op = op_alloc(ORANGEFS_VFS_OP_READDIR); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 231 | if (!new_op) |
| 232 | return -ENOMEM; |
| 233 | |
Martin Brandenburg | ee3b8d3 | 2016-02-17 12:55:42 -0500 | [diff] [blame^] | 234 | /* |
| 235 | * Only the indices are shared. No memory is actually shared, but the |
| 236 | * mechanism is used. |
| 237 | */ |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 238 | new_op->uses_shared_memory = 1; |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 239 | new_op->upcall.req.readdir.refn = orangefs_inode->refn; |
Martin Brandenburg | 7d22148 | 2016-01-04 15:05:28 -0500 | [diff] [blame] | 240 | new_op->upcall.req.readdir.max_dirent_count = |
| 241 | ORANGEFS_MAX_DIRENT_COUNT_READDIR; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 242 | |
| 243 | gossip_debug(GOSSIP_DIR_DEBUG, |
| 244 | "%s: upcall.req.readdir.refn.khandle: %pU\n", |
| 245 | __func__, |
| 246 | &new_op->upcall.req.readdir.refn.khandle); |
| 247 | |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 248 | new_op->upcall.req.readdir.token = *ptoken; |
| 249 | |
| 250 | get_new_buffer_index: |
Martin Brandenburg | 7d22148 | 2016-01-04 15:05:28 -0500 | [diff] [blame] | 251 | ret = orangefs_readdir_index_get(&bufmap, &buffer_index); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 252 | if (ret < 0) { |
Martin Brandenburg | 7d22148 | 2016-01-04 15:05:28 -0500 | [diff] [blame] | 253 | gossip_lerr("orangefs_readdir: orangefs_readdir_index_get() failure (%d)\n", |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 254 | ret); |
| 255 | goto out_free_op; |
| 256 | } |
| 257 | new_op->upcall.req.readdir.buf_index = buffer_index; |
| 258 | |
| 259 | ret = service_operation(new_op, |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 260 | "orangefs_readdir", |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 261 | get_interruptible_flag(dentry->d_inode)); |
| 262 | |
| 263 | gossip_debug(GOSSIP_DIR_DEBUG, |
| 264 | "Readdir downcall status is %d. ret:%d\n", |
| 265 | new_op->downcall.status, |
| 266 | ret); |
| 267 | |
Martin Brandenburg | ee3b8d3 | 2016-02-17 12:55:42 -0500 | [diff] [blame^] | 268 | orangefs_readdir_index_put(buffer_index); |
| 269 | |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 270 | if (ret == -EAGAIN && op_state_purged(new_op)) { |
Martin Brandenburg | ee3b8d3 | 2016-02-17 12:55:42 -0500 | [diff] [blame^] | 271 | /* Client-core indices are invalid after it restarted. */ |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 272 | gossip_debug(GOSSIP_DIR_DEBUG, |
| 273 | "%s: Getting new buffer_index for retry of readdir..\n", |
| 274 | __func__); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 275 | goto get_new_buffer_index; |
| 276 | } |
| 277 | |
| 278 | if (ret == -EIO && op_state_purged(new_op)) { |
| 279 | gossip_err("%s: Client is down. Aborting readdir call.\n", |
| 280 | __func__); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 281 | goto out_free_op; |
| 282 | } |
| 283 | |
| 284 | if (ret < 0 || new_op->downcall.status != 0) { |
| 285 | gossip_debug(GOSSIP_DIR_DEBUG, |
| 286 | "Readdir request failed. Status:%d\n", |
| 287 | new_op->downcall.status); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 288 | if (ret >= 0) |
| 289 | ret = new_op->downcall.status; |
| 290 | goto out_free_op; |
| 291 | } |
| 292 | |
| 293 | bytes_decoded = |
| 294 | readdir_handle_ctor(&rhandle, |
| 295 | new_op->downcall.trailer_buf, |
Martin Brandenburg | ee3b8d3 | 2016-02-17 12:55:42 -0500 | [diff] [blame^] | 296 | new_op->downcall.trailer_size); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 297 | if (bytes_decoded < 0) { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 298 | gossip_err("orangefs_readdir: Could not decode trailer buffer into a readdir response %d\n", |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 299 | ret); |
| 300 | ret = bytes_decoded; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 301 | goto out_free_op; |
| 302 | } |
| 303 | |
| 304 | if (bytes_decoded != new_op->downcall.trailer_size) { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 305 | gossip_err("orangefs_readdir: # bytes decoded (%ld) " |
Mike Marshall | 88309aa | 2015-09-23 16:48:40 -0400 | [diff] [blame] | 306 | "!= trailer size (%ld)\n", |
| 307 | bytes_decoded, |
| 308 | (long)new_op->downcall.trailer_size); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 309 | ret = -EINVAL; |
| 310 | goto out_destroy_handle; |
| 311 | } |
| 312 | |
Mike Marshall | 88309aa | 2015-09-23 16:48:40 -0400 | [diff] [blame] | 313 | /* |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 314 | * orangefs doesn't actually store dot and dot-dot, but |
Mike Marshall | 88309aa | 2015-09-23 16:48:40 -0400 | [diff] [blame] | 315 | * we need to have them represented. |
| 316 | */ |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 317 | if (pos == 0) { |
| 318 | ino = get_ino_from_khandle(dentry->d_inode); |
| 319 | gossip_debug(GOSSIP_DIR_DEBUG, |
| 320 | "%s: calling dir_emit of \".\" with pos = %llu\n", |
| 321 | __func__, |
| 322 | llu(pos)); |
| 323 | ret = dir_emit(ctx, ".", 1, ino, DT_DIR); |
Mike Marshall | 88309aa | 2015-09-23 16:48:40 -0400 | [diff] [blame] | 324 | pos += 1; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | if (pos == 1) { |
| 328 | ino = get_parent_ino_from_dentry(dentry); |
| 329 | gossip_debug(GOSSIP_DIR_DEBUG, |
| 330 | "%s: calling dir_emit of \"..\" with pos = %llu\n", |
| 331 | __func__, |
| 332 | llu(pos)); |
| 333 | ret = dir_emit(ctx, "..", 2, ino, DT_DIR); |
Mike Marshall | 88309aa | 2015-09-23 16:48:40 -0400 | [diff] [blame] | 334 | pos += 1; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 335 | } |
| 336 | |
Mike Marshall | 88309aa | 2015-09-23 16:48:40 -0400 | [diff] [blame] | 337 | /* |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 338 | * we stored ORANGEFS_ITERATE_NEXT in ctx->pos last time around |
Mike Marshall | 88309aa | 2015-09-23 16:48:40 -0400 | [diff] [blame] | 339 | * to prevent "finding" dot and dot-dot on any iteration |
| 340 | * other than the first. |
| 341 | */ |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 342 | if (ctx->pos == ORANGEFS_ITERATE_NEXT) |
Mike Marshall | 88309aa | 2015-09-23 16:48:40 -0400 | [diff] [blame] | 343 | ctx->pos = 0; |
| 344 | |
| 345 | for (i = ctx->pos; |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 346 | i < rhandle.readdir_response.orangefs_dirent_outcount; |
Mike Marshall | 88309aa | 2015-09-23 16:48:40 -0400 | [diff] [blame] | 347 | i++) { |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 348 | len = rhandle.readdir_response.dirent_array[i].d_length; |
| 349 | current_entry = rhandle.readdir_response.dirent_array[i].d_name; |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 350 | current_ino = orangefs_khandle_to_ino( |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 351 | &(rhandle.readdir_response.dirent_array[i].khandle)); |
| 352 | |
| 353 | gossip_debug(GOSSIP_DIR_DEBUG, |
Mike Marshall | 88309aa | 2015-09-23 16:48:40 -0400 | [diff] [blame] | 354 | "calling dir_emit for %s with len %d" |
| 355 | ", ctx->pos %ld\n", |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 356 | current_entry, |
| 357 | len, |
Mike Marshall | 88309aa | 2015-09-23 16:48:40 -0400 | [diff] [blame] | 358 | (unsigned long)ctx->pos); |
| 359 | /* |
| 360 | * type is unknown. We don't return object type |
| 361 | * in the dirent_array. This leaves getdents |
| 362 | * clueless about type. |
| 363 | */ |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 364 | ret = |
| 365 | dir_emit(ctx, current_entry, len, current_ino, DT_UNKNOWN); |
Mike Marshall | 88309aa | 2015-09-23 16:48:40 -0400 | [diff] [blame] | 366 | if (!ret) |
| 367 | break; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 368 | ctx->pos++; |
Mike Marshall | 88309aa | 2015-09-23 16:48:40 -0400 | [diff] [blame] | 369 | gossip_debug(GOSSIP_DIR_DEBUG, |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 370 | "%s: ctx->pos:%lld\n", |
| 371 | __func__, |
| 372 | lld(ctx->pos)); |
| 373 | |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 374 | } |
| 375 | |
Mike Marshall | 5480494 | 2015-10-05 13:44:24 -0400 | [diff] [blame] | 376 | /* |
Mike Marshall | 88309aa | 2015-09-23 16:48:40 -0400 | [diff] [blame] | 377 | * we ran all the way through the last batch, set up for |
| 378 | * getting another batch... |
| 379 | */ |
| 380 | if (ret) { |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 381 | *ptoken = rhandle.readdir_response.token; |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 382 | ctx->pos = ORANGEFS_ITERATE_NEXT; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | /* |
| 386 | * Did we hit the end of the directory? |
| 387 | */ |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 388 | if (rhandle.readdir_response.token == ORANGEFS_READDIR_END && |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 389 | !buffer_full) { |
Mike Marshall | 88309aa | 2015-09-23 16:48:40 -0400 | [diff] [blame] | 390 | gossip_debug(GOSSIP_DIR_DEBUG, |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 391 | "End of dir detected; setting ctx->pos to ORANGEFS_READDIR_END.\n"); |
| 392 | ctx->pos = ORANGEFS_READDIR_END; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 393 | } |
| 394 | |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 395 | out_destroy_handle: |
Al Viro | 82d37f1 | 2016-02-13 21:04:51 -0500 | [diff] [blame] | 396 | readdir_handle_dtor(&rhandle); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 397 | out_free_op: |
| 398 | op_release(new_op); |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 399 | gossip_debug(GOSSIP_DIR_DEBUG, "orangefs_readdir returning %d\n", ret); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 400 | return ret; |
| 401 | } |
| 402 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 403 | static int orangefs_dir_open(struct inode *inode, struct file *file) |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 404 | { |
| 405 | __u64 *ptoken; |
| 406 | |
| 407 | file->private_data = kmalloc(sizeof(__u64), GFP_KERNEL); |
| 408 | if (!file->private_data) |
| 409 | return -ENOMEM; |
| 410 | |
| 411 | ptoken = file->private_data; |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 412 | *ptoken = ORANGEFS_READDIR_START; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 413 | return 0; |
| 414 | } |
| 415 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 416 | static int orangefs_dir_release(struct inode *inode, struct file *file) |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 417 | { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 418 | orangefs_flush_inode(inode); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 419 | kfree(file->private_data); |
| 420 | return 0; |
| 421 | } |
| 422 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 423 | /** ORANGEFS implementation of VFS directory operations */ |
| 424 | const struct file_operations orangefs_dir_operations = { |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 425 | .read = generic_read_dir, |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 426 | .iterate = orangefs_readdir, |
| 427 | .open = orangefs_dir_open, |
| 428 | .release = orangefs_dir_release, |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 429 | }; |