blob: 284373a57a082fa0e38422afbc49fa2e8519b0df [file] [log] [blame]
Mike Marshall5db11c22015-07-17 10:38:12 -04001/*
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 Marshall575e9462015-12-04 12:56:14 -05008#include "orangefs-kernel.h"
9#include "orangefs-bufmap.h"
Mike Marshall5db11c22015-07-17 10:38:12 -040010
Mike Marshall5db11c22015-07-17 10:38:12 -040011/*
Mike Marshall1808f8c2016-01-15 13:10:52 -050012 * decode routine used by kmod to deal with the blob sent from
13 * userspace for readdirs. The blob contains zero or more of these
14 * sub-blobs:
15 * __u32 - represents length of the character string that follows.
16 * string - between 1 and ORANGEFS_NAME_MAX bytes long.
17 * padding - (if needed) to cause the __u32 plus the string to be
18 * eight byte aligned.
19 * khandle - sizeof(khandle) bytes.
Mike Marshall5db11c22015-07-17 10:38:12 -040020 */
Al Viro80928952015-10-09 18:11:10 -040021static long decode_dirents(char *ptr, size_t size,
Yi Liu8bb8aef2015-11-24 15:12:14 -050022 struct orangefs_readdir_response_s *readdir)
Mike Marshall5db11c22015-07-17 10:38:12 -040023{
24 int i;
Yi Liu8bb8aef2015-11-24 15:12:14 -050025 struct orangefs_readdir_response_s *rd =
26 (struct orangefs_readdir_response_s *) ptr;
Mike Marshall5db11c22015-07-17 10:38:12 -040027 char *buf = ptr;
Mike Marshall1808f8c2016-01-15 13:10:52 -050028 int khandle_size = sizeof(struct orangefs_khandle);
29 size_t offset = offsetof(struct orangefs_readdir_response_s,
30 dirent_array);
31 /* 8 reflects eight byte alignment */
32 int smallest_blob = khandle_size + 8;
33 __u32 len;
34 int aligned_len;
35 int sizeof_u32 = sizeof(__u32);
36 long ret;
Mike Marshall5db11c22015-07-17 10:38:12 -040037
Mike Marshall1808f8c2016-01-15 13:10:52 -050038 gossip_debug(GOSSIP_DIR_DEBUG, "%s: size:%zu:\n", __func__, size);
39
40 /* size is = offset on empty dirs, > offset on non-empty dirs... */
41 if (size < offset) {
42 gossip_err("%s: size:%zu: offset:%zu:\n",
43 __func__,
44 size,
45 offset);
46 ret = -EINVAL;
47 goto out;
48 }
49
50 if ((size == offset) && (readdir->orangefs_dirent_outcount != 0)) {
51 gossip_err("%s: size:%zu: dirent_outcount:%d:\n",
52 __func__,
53 size,
54 readdir->orangefs_dirent_outcount);
55 ret = -EINVAL;
56 goto out;
57 }
Al Viro80928952015-10-09 18:11:10 -040058
Mike Marshall5db11c22015-07-17 10:38:12 -040059 readdir->token = rd->token;
Yi Liu8bb8aef2015-11-24 15:12:14 -050060 readdir->orangefs_dirent_outcount = rd->orangefs_dirent_outcount;
61 readdir->dirent_array = kcalloc(readdir->orangefs_dirent_outcount,
Mike Marshall5db11c22015-07-17 10:38:12 -040062 sizeof(*readdir->dirent_array),
63 GFP_KERNEL);
Mike Marshall1808f8c2016-01-15 13:10:52 -050064 if (readdir->dirent_array == NULL) {
65 gossip_err("%s: kcalloc failed.\n", __func__);
66 ret = -ENOMEM;
67 goto out;
68 }
Al Viro80928952015-10-09 18:11:10 -040069
Mike Marshall1808f8c2016-01-15 13:10:52 -050070 buf += offset;
71 size -= offset;
Al Viro80928952015-10-09 18:11:10 -040072
Yi Liu8bb8aef2015-11-24 15:12:14 -050073 for (i = 0; i < readdir->orangefs_dirent_outcount; i++) {
Mike Marshall1808f8c2016-01-15 13:10:52 -050074 if (size < smallest_blob) {
75 gossip_err("%s: size:%zu: smallest_blob:%d:\n",
76 __func__,
77 size,
78 smallest_blob);
79 ret = -EINVAL;
80 goto free;
81 }
Al Viro80928952015-10-09 18:11:10 -040082
83 len = *(__u32 *)buf;
Mike Marshall1808f8c2016-01-15 13:10:52 -050084 if ((len < 1) || (len > ORANGEFS_NAME_MAX)) {
85 gossip_err("%s: len:%d:\n", __func__, len);
86 ret = -EINVAL;
87 goto free;
88 }
Al Viro80928952015-10-09 18:11:10 -040089
Mike Marshall1808f8c2016-01-15 13:10:52 -050090 gossip_debug(GOSSIP_DIR_DEBUG,
91 "%s: size:%zu: len:%d:\n",
92 __func__,
93 size,
94 len);
95
96 readdir->dirent_array[i].d_name = buf + sizeof_u32;
Al Viro9be68b02015-10-09 17:43:15 -040097 readdir->dirent_array[i].d_length = len;
Al Viro80928952015-10-09 18:11:10 -040098
Martin Brandenburg7d221482016-01-04 15:05:28 -050099 /*
Mike Marshall1808f8c2016-01-15 13:10:52 -0500100 * Calculate "aligned" length of this string and its
101 * associated __u32 descriptor.
Martin Brandenburg7d221482016-01-04 15:05:28 -0500102 */
Mike Marshall1808f8c2016-01-15 13:10:52 -0500103 aligned_len = ((sizeof_u32 + len + 1) + 7) & ~7;
104 gossip_debug(GOSSIP_DIR_DEBUG,
105 "%s: aligned_len:%d:\n",
106 __func__,
107 aligned_len);
Al Viro80928952015-10-09 18:11:10 -0400108
Mike Marshall1808f8c2016-01-15 13:10:52 -0500109 /*
110 * The end of the blob should coincide with the end
111 * of the last sub-blob.
112 */
113 if (size < aligned_len + khandle_size) {
114 gossip_err("%s: ran off the end of the blob.\n",
115 __func__);
116 ret = -EINVAL;
117 goto free;
118 }
119 size -= aligned_len + khandle_size;
120
121 buf += aligned_len;
Al Viro80928952015-10-09 18:11:10 -0400122
Mike Marshall5db11c22015-07-17 10:38:12 -0400123 readdir->dirent_array[i].khandle =
Yi Liu8bb8aef2015-11-24 15:12:14 -0500124 *(struct orangefs_khandle *) buf;
Mike Marshall1808f8c2016-01-15 13:10:52 -0500125 buf += khandle_size;
Mike Marshall5db11c22015-07-17 10:38:12 -0400126 }
Mike Marshall1808f8c2016-01-15 13:10:52 -0500127 ret = buf - ptr;
128 gossip_debug(GOSSIP_DIR_DEBUG, "%s: returning:%ld:\n", __func__, ret);
129 goto out;
130
131free:
Al Viro80928952015-10-09 18:11:10 -0400132 kfree(readdir->dirent_array);
133 readdir->dirent_array = NULL;
Mike Marshall1808f8c2016-01-15 13:10:52 -0500134
135out:
136 return ret;
Mike Marshall5db11c22015-07-17 10:38:12 -0400137}
138
Mike Marshall5db11c22015-07-17 10:38:12 -0400139/*
140 * Read directory entries from an instance of an open directory.
Mike Marshall5db11c22015-07-17 10:38:12 -0400141 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500142static int orangefs_readdir(struct file *file, struct dir_context *ctx)
Mike Marshall5db11c22015-07-17 10:38:12 -0400143{
Mike Marshall5db11c22015-07-17 10:38:12 -0400144 int ret = 0;
145 int buffer_index;
Mike Marshall88309aa2015-09-23 16:48:40 -0400146 /*
147 * ptoken supports Orangefs' distributed directory logic, added
148 * in 2.9.2.
149 */
Mike Marshall5db11c22015-07-17 10:38:12 -0400150 __u64 *ptoken = file->private_data;
151 __u64 pos = 0;
152 ino_t ino = 0;
153 struct dentry *dentry = file->f_path.dentry;
Yi Liu8bb8aef2015-11-24 15:12:14 -0500154 struct orangefs_kernel_op_s *new_op = NULL;
155 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(dentry->d_inode);
Al Viro9f5e2f72016-02-16 19:54:13 -0500156 struct orangefs_readdir_response_s readdir_response;
157 void *dents_buf;
Mike Marshall5db11c22015-07-17 10:38:12 -0400158 int i = 0;
159 int len = 0;
160 ino_t current_ino = 0;
161 char *current_entry = NULL;
162 long bytes_decoded;
163
Mike Marshall88309aa2015-09-23 16:48:40 -0400164 gossip_debug(GOSSIP_DIR_DEBUG,
165 "%s: ctx->pos:%lld, ptoken = %llu\n",
166 __func__,
167 lld(ctx->pos),
168 llu(*ptoken));
Mike Marshall5db11c22015-07-17 10:38:12 -0400169
170 pos = (__u64) ctx->pos;
171
172 /* are we done? */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500173 if (pos == ORANGEFS_READDIR_END) {
Mike Marshall5db11c22015-07-17 10:38:12 -0400174 gossip_debug(GOSSIP_DIR_DEBUG,
175 "Skipping to termination path\n");
176 return 0;
177 }
178
179 gossip_debug(GOSSIP_DIR_DEBUG,
Al Virof66debf2016-08-07 12:20:01 -0400180 "orangefs_readdir called on %pd (pos=%llu)\n",
181 dentry, llu(pos));
Mike Marshall5db11c22015-07-17 10:38:12 -0400182
Al Viro9f5e2f72016-02-16 19:54:13 -0500183 memset(&readdir_response, 0, sizeof(readdir_response));
Mike Marshall5db11c22015-07-17 10:38:12 -0400184
Yi Liu8bb8aef2015-11-24 15:12:14 -0500185 new_op = op_alloc(ORANGEFS_VFS_OP_READDIR);
Mike Marshall5db11c22015-07-17 10:38:12 -0400186 if (!new_op)
187 return -ENOMEM;
188
Martin Brandenburgee3b8d32016-02-17 12:55:42 -0500189 /*
190 * Only the indices are shared. No memory is actually shared, but the
191 * mechanism is used.
192 */
Mike Marshall5db11c22015-07-17 10:38:12 -0400193 new_op->uses_shared_memory = 1;
Yi Liu8bb8aef2015-11-24 15:12:14 -0500194 new_op->upcall.req.readdir.refn = orangefs_inode->refn;
Martin Brandenburg7d221482016-01-04 15:05:28 -0500195 new_op->upcall.req.readdir.max_dirent_count =
196 ORANGEFS_MAX_DIRENT_COUNT_READDIR;
Mike Marshall5db11c22015-07-17 10:38:12 -0400197
198 gossip_debug(GOSSIP_DIR_DEBUG,
199 "%s: upcall.req.readdir.refn.khandle: %pU\n",
200 __func__,
201 &new_op->upcall.req.readdir.refn.khandle);
202
Mike Marshall5db11c22015-07-17 10:38:12 -0400203 new_op->upcall.req.readdir.token = *ptoken;
204
205get_new_buffer_index:
Al Virob8a99a82016-02-16 20:10:26 -0500206 buffer_index = orangefs_readdir_index_get();
207 if (buffer_index < 0) {
208 ret = buffer_index;
Martin Brandenburg7d221482016-01-04 15:05:28 -0500209 gossip_lerr("orangefs_readdir: orangefs_readdir_index_get() failure (%d)\n",
Mike Marshall5db11c22015-07-17 10:38:12 -0400210 ret);
211 goto out_free_op;
212 }
213 new_op->upcall.req.readdir.buf_index = buffer_index;
214
215 ret = service_operation(new_op,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500216 "orangefs_readdir",
Mike Marshall5db11c22015-07-17 10:38:12 -0400217 get_interruptible_flag(dentry->d_inode));
218
219 gossip_debug(GOSSIP_DIR_DEBUG,
220 "Readdir downcall status is %d. ret:%d\n",
221 new_op->downcall.status,
222 ret);
223
Martin Brandenburgee3b8d32016-02-17 12:55:42 -0500224 orangefs_readdir_index_put(buffer_index);
225
Mike Marshall5db11c22015-07-17 10:38:12 -0400226 if (ret == -EAGAIN && op_state_purged(new_op)) {
Martin Brandenburgee3b8d32016-02-17 12:55:42 -0500227 /* Client-core indices are invalid after it restarted. */
Mike Marshall5db11c22015-07-17 10:38:12 -0400228 gossip_debug(GOSSIP_DIR_DEBUG,
229 "%s: Getting new buffer_index for retry of readdir..\n",
230 __func__);
Mike Marshall5db11c22015-07-17 10:38:12 -0400231 goto get_new_buffer_index;
232 }
233
234 if (ret == -EIO && op_state_purged(new_op)) {
235 gossip_err("%s: Client is down. Aborting readdir call.\n",
236 __func__);
Martin Brandenburg641bb322016-03-28 17:18:27 -0400237 goto out_free_op;
Mike Marshall5db11c22015-07-17 10:38:12 -0400238 }
239
240 if (ret < 0 || new_op->downcall.status != 0) {
241 gossip_debug(GOSSIP_DIR_DEBUG,
242 "Readdir request failed. Status:%d\n",
243 new_op->downcall.status);
Mike Marshall5db11c22015-07-17 10:38:12 -0400244 if (ret >= 0)
245 ret = new_op->downcall.status;
Martin Brandenburg641bb322016-03-28 17:18:27 -0400246 goto out_free_op;
Mike Marshall5db11c22015-07-17 10:38:12 -0400247 }
248
Al Viro9f5e2f72016-02-16 19:54:13 -0500249 dents_buf = new_op->downcall.trailer_buf;
250 if (dents_buf == NULL) {
251 gossip_err("Invalid NULL buffer in readdir response\n");
252 ret = -ENOMEM;
Martin Brandenburg641bb322016-03-28 17:18:27 -0400253 goto out_free_op;
Al Viro9f5e2f72016-02-16 19:54:13 -0500254 }
255
256 bytes_decoded = decode_dirents(dents_buf, new_op->downcall.trailer_size,
257 &readdir_response);
Mike Marshall5db11c22015-07-17 10:38:12 -0400258 if (bytes_decoded < 0) {
Mike Marshall5db11c22015-07-17 10:38:12 -0400259 ret = bytes_decoded;
Al Viro9f5e2f72016-02-16 19:54:13 -0500260 gossip_err("Could not decode readdir from buffer %d\n", ret);
261 goto out_vfree;
Mike Marshall5db11c22015-07-17 10:38:12 -0400262 }
263
264 if (bytes_decoded != new_op->downcall.trailer_size) {
Yi Liu8bb8aef2015-11-24 15:12:14 -0500265 gossip_err("orangefs_readdir: # bytes decoded (%ld) "
Mike Marshall88309aa2015-09-23 16:48:40 -0400266 "!= trailer size (%ld)\n",
267 bytes_decoded,
268 (long)new_op->downcall.trailer_size);
Mike Marshall5db11c22015-07-17 10:38:12 -0400269 ret = -EINVAL;
270 goto out_destroy_handle;
271 }
272
Mike Marshall88309aa2015-09-23 16:48:40 -0400273 /*
Yi Liu8bb8aef2015-11-24 15:12:14 -0500274 * orangefs doesn't actually store dot and dot-dot, but
Mike Marshall88309aa2015-09-23 16:48:40 -0400275 * we need to have them represented.
276 */
Mike Marshall5db11c22015-07-17 10:38:12 -0400277 if (pos == 0) {
278 ino = get_ino_from_khandle(dentry->d_inode);
279 gossip_debug(GOSSIP_DIR_DEBUG,
280 "%s: calling dir_emit of \".\" with pos = %llu\n",
281 __func__,
282 llu(pos));
283 ret = dir_emit(ctx, ".", 1, ino, DT_DIR);
Mike Marshall88309aa2015-09-23 16:48:40 -0400284 pos += 1;
Mike Marshall5db11c22015-07-17 10:38:12 -0400285 }
286
287 if (pos == 1) {
288 ino = get_parent_ino_from_dentry(dentry);
289 gossip_debug(GOSSIP_DIR_DEBUG,
290 "%s: calling dir_emit of \"..\" with pos = %llu\n",
291 __func__,
292 llu(pos));
293 ret = dir_emit(ctx, "..", 2, ino, DT_DIR);
Mike Marshall88309aa2015-09-23 16:48:40 -0400294 pos += 1;
Mike Marshall5db11c22015-07-17 10:38:12 -0400295 }
296
Mike Marshall88309aa2015-09-23 16:48:40 -0400297 /*
Yi Liu8bb8aef2015-11-24 15:12:14 -0500298 * we stored ORANGEFS_ITERATE_NEXT in ctx->pos last time around
Mike Marshall88309aa2015-09-23 16:48:40 -0400299 * to prevent "finding" dot and dot-dot on any iteration
300 * other than the first.
301 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500302 if (ctx->pos == ORANGEFS_ITERATE_NEXT)
Mike Marshall88309aa2015-09-23 16:48:40 -0400303 ctx->pos = 0;
304
Mike Marshallcf07c0b2016-03-09 13:11:45 -0500305 gossip_debug(GOSSIP_DIR_DEBUG,
306 "%s: dirent_outcount:%d:\n",
307 __func__,
Al Viro9f5e2f72016-02-16 19:54:13 -0500308 readdir_response.orangefs_dirent_outcount);
Mike Marshall88309aa2015-09-23 16:48:40 -0400309 for (i = ctx->pos;
Al Viro9f5e2f72016-02-16 19:54:13 -0500310 i < readdir_response.orangefs_dirent_outcount;
Mike Marshall88309aa2015-09-23 16:48:40 -0400311 i++) {
Al Viro9f5e2f72016-02-16 19:54:13 -0500312 len = readdir_response.dirent_array[i].d_length;
313 current_entry = readdir_response.dirent_array[i].d_name;
Yi Liu8bb8aef2015-11-24 15:12:14 -0500314 current_ino = orangefs_khandle_to_ino(
Al Viro9f5e2f72016-02-16 19:54:13 -0500315 &readdir_response.dirent_array[i].khandle);
Mike Marshall5db11c22015-07-17 10:38:12 -0400316
317 gossip_debug(GOSSIP_DIR_DEBUG,
Mike Marshall88309aa2015-09-23 16:48:40 -0400318 "calling dir_emit for %s with len %d"
319 ", ctx->pos %ld\n",
Mike Marshall5db11c22015-07-17 10:38:12 -0400320 current_entry,
321 len,
Mike Marshall88309aa2015-09-23 16:48:40 -0400322 (unsigned long)ctx->pos);
323 /*
324 * type is unknown. We don't return object type
325 * in the dirent_array. This leaves getdents
326 * clueless about type.
327 */
Mike Marshall5db11c22015-07-17 10:38:12 -0400328 ret =
329 dir_emit(ctx, current_entry, len, current_ino, DT_UNKNOWN);
Mike Marshall88309aa2015-09-23 16:48:40 -0400330 if (!ret)
331 break;
Mike Marshall5db11c22015-07-17 10:38:12 -0400332 ctx->pos++;
Mike Marshall88309aa2015-09-23 16:48:40 -0400333 gossip_debug(GOSSIP_DIR_DEBUG,
Mike Marshall5db11c22015-07-17 10:38:12 -0400334 "%s: ctx->pos:%lld\n",
335 __func__,
336 lld(ctx->pos));
337
Mike Marshall5db11c22015-07-17 10:38:12 -0400338 }
339
Mike Marshall54804942015-10-05 13:44:24 -0400340 /*
Mike Marshall88309aa2015-09-23 16:48:40 -0400341 * we ran all the way through the last batch, set up for
342 * getting another batch...
343 */
344 if (ret) {
Al Viro9f5e2f72016-02-16 19:54:13 -0500345 *ptoken = readdir_response.token;
Yi Liu8bb8aef2015-11-24 15:12:14 -0500346 ctx->pos = ORANGEFS_ITERATE_NEXT;
Mike Marshall5db11c22015-07-17 10:38:12 -0400347 }
348
349 /*
350 * Did we hit the end of the directory?
351 */
Martin Brandenburge56f4982016-04-04 16:26:38 -0400352 if (readdir_response.token == ORANGEFS_READDIR_END) {
Mike Marshall88309aa2015-09-23 16:48:40 -0400353 gossip_debug(GOSSIP_DIR_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500354 "End of dir detected; setting ctx->pos to ORANGEFS_READDIR_END.\n");
355 ctx->pos = ORANGEFS_READDIR_END;
Mike Marshall5db11c22015-07-17 10:38:12 -0400356 }
357
Mike Marshall5db11c22015-07-17 10:38:12 -0400358out_destroy_handle:
Al Viro9f5e2f72016-02-16 19:54:13 -0500359 /* kfree(NULL) is safe */
360 kfree(readdir_response.dirent_array);
361out_vfree:
362 gossip_debug(GOSSIP_DIR_DEBUG, "vfree %p\n", dents_buf);
363 vfree(dents_buf);
Mike Marshall5db11c22015-07-17 10:38:12 -0400364out_free_op:
365 op_release(new_op);
Yi Liu8bb8aef2015-11-24 15:12:14 -0500366 gossip_debug(GOSSIP_DIR_DEBUG, "orangefs_readdir returning %d\n", ret);
Mike Marshall5db11c22015-07-17 10:38:12 -0400367 return ret;
368}
369
Yi Liu8bb8aef2015-11-24 15:12:14 -0500370static int orangefs_dir_open(struct inode *inode, struct file *file)
Mike Marshall5db11c22015-07-17 10:38:12 -0400371{
372 __u64 *ptoken;
373
374 file->private_data = kmalloc(sizeof(__u64), GFP_KERNEL);
375 if (!file->private_data)
376 return -ENOMEM;
377
378 ptoken = file->private_data;
Yi Liu8bb8aef2015-11-24 15:12:14 -0500379 *ptoken = ORANGEFS_READDIR_START;
Mike Marshall5db11c22015-07-17 10:38:12 -0400380 return 0;
381}
382
Yi Liu8bb8aef2015-11-24 15:12:14 -0500383static int orangefs_dir_release(struct inode *inode, struct file *file)
Mike Marshall5db11c22015-07-17 10:38:12 -0400384{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500385 orangefs_flush_inode(inode);
Mike Marshall5db11c22015-07-17 10:38:12 -0400386 kfree(file->private_data);
387 return 0;
388}
389
Yi Liu8bb8aef2015-11-24 15:12:14 -0500390/** ORANGEFS implementation of VFS directory operations */
391const struct file_operations orangefs_dir_operations = {
Mike Marshall5db11c22015-07-17 10:38:12 -0400392 .read = generic_read_dir,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500393 .iterate = orangefs_readdir,
394 .open = orangefs_dir_open,
395 .release = orangefs_dir_release,
Mike Marshall5db11c22015-07-17 10:38:12 -0400396};