Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 1 | /* |
Martin Brandenburg | 382f458 | 2017-04-25 15:37:59 -0400 | [diff] [blame^] | 2 | * Copyright 2017 Omnibond Systems, L.L.C. |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 3 | */ |
| 4 | |
| 5 | #include "protocol.h" |
Mike Marshall | 575e946 | 2015-12-04 12:56:14 -0500 | [diff] [blame] | 6 | #include "orangefs-kernel.h" |
| 7 | #include "orangefs-bufmap.h" |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 8 | |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 9 | /* |
Martin Brandenburg | 382f458 | 2017-04-25 15:37:59 -0400 | [diff] [blame^] | 10 | * There can be up to 512 directory entries. Each entry is encoded as |
| 11 | * follows: |
| 12 | * 4 bytes: string size (n) |
| 13 | * n bytes: string |
| 14 | * 1 byte: trailing zero |
| 15 | * padding to 8 bytes |
| 16 | * 16 bytes: khandle |
| 17 | * padding to 8 bytes |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 18 | */ |
Martin Brandenburg | 382f458 | 2017-04-25 15:37:59 -0400 | [diff] [blame^] | 19 | #define MAX_DIRECTORY ((4 + 257 + 3 + 16)*512) |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 20 | |
Martin Brandenburg | 382f458 | 2017-04-25 15:37:59 -0400 | [diff] [blame^] | 21 | struct orangefs_dir { |
| 22 | __u64 token; |
| 23 | void *directory; |
| 24 | size_t i, len; |
| 25 | int error; |
| 26 | }; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 27 | |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 28 | /* |
Martin Brandenburg | 382f458 | 2017-04-25 15:37:59 -0400 | [diff] [blame^] | 29 | * The userspace component sends several directory entries of the |
| 30 | * following format. The first four bytes are the string length not |
| 31 | * including a trailing zero byte. This is followed by the string and a |
| 32 | * trailing zero padded to the next four byte boundry. This is followed |
| 33 | * by the sixteen byte khandle padded to the next eight byte boundry. |
| 34 | * |
| 35 | * The trailer_buf starts with a struct orangefs_readdir_response_s |
| 36 | * which must be skipped to get to the directory data. |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 37 | */ |
Martin Brandenburg | 382f458 | 2017-04-25 15:37:59 -0400 | [diff] [blame^] | 38 | |
| 39 | static int orangefs_dir_more(struct orangefs_inode_s *oi, |
| 40 | struct orangefs_dir *od, struct dentry *dentry) |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 41 | { |
Martin Brandenburg | 382f458 | 2017-04-25 15:37:59 -0400 | [diff] [blame^] | 42 | const size_t offset = |
| 43 | sizeof(struct orangefs_readdir_response_s); |
| 44 | struct orangefs_readdir_response_s *resp; |
| 45 | struct orangefs_kernel_op_s *op; |
| 46 | int bufi, r; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 47 | |
Martin Brandenburg | 382f458 | 2017-04-25 15:37:59 -0400 | [diff] [blame^] | 48 | op = op_alloc(ORANGEFS_VFS_OP_READDIR); |
| 49 | if (!op) { |
| 50 | od->error = -ENOMEM; |
| 51 | return -ENOMEM; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 52 | } |
| 53 | |
Martin Brandenburg | ee3b8d3 | 2016-02-17 12:55:42 -0500 | [diff] [blame] | 54 | /* |
Martin Brandenburg | 382f458 | 2017-04-25 15:37:59 -0400 | [diff] [blame^] | 55 | * Despite the badly named field, readdir does not use shared |
| 56 | * memory. However, there are a limited number of readdir |
| 57 | * slots, which must be allocated here. This flag simply tells |
| 58 | * the op scheduler to return the op here for retry. |
Martin Brandenburg | ee3b8d3 | 2016-02-17 12:55:42 -0500 | [diff] [blame] | 59 | */ |
Martin Brandenburg | 382f458 | 2017-04-25 15:37:59 -0400 | [diff] [blame^] | 60 | op->uses_shared_memory = 1; |
| 61 | op->upcall.req.readdir.refn = oi->refn; |
| 62 | op->upcall.req.readdir.token = od->token; |
| 63 | op->upcall.req.readdir.max_dirent_count = |
Martin Brandenburg | 7d22148 | 2016-01-04 15:05:28 -0500 | [diff] [blame] | 64 | ORANGEFS_MAX_DIRENT_COUNT_READDIR; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 65 | |
Martin Brandenburg | 382f458 | 2017-04-25 15:37:59 -0400 | [diff] [blame^] | 66 | again: |
| 67 | bufi = orangefs_readdir_index_get(); |
| 68 | if (bufi < 0) { |
| 69 | op_release(op); |
| 70 | od->error = bufi; |
| 71 | return bufi; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 72 | } |
| 73 | |
Martin Brandenburg | 382f458 | 2017-04-25 15:37:59 -0400 | [diff] [blame^] | 74 | op->upcall.req.readdir.buf_index = bufi; |
| 75 | |
| 76 | r = service_operation(op, "orangefs_readdir", |
| 77 | get_interruptible_flag(dentry->d_inode)); |
| 78 | |
| 79 | orangefs_readdir_index_put(bufi); |
| 80 | |
| 81 | if (op_state_purged(op)) { |
| 82 | if (r == -EAGAIN) { |
| 83 | vfree(op->downcall.trailer_buf); |
| 84 | goto again; |
| 85 | } else if (r == -EIO) { |
| 86 | vfree(op->downcall.trailer_buf); |
| 87 | op_release(op); |
| 88 | od->error = r; |
| 89 | return r; |
| 90 | } |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 91 | } |
| 92 | |
Martin Brandenburg | 382f458 | 2017-04-25 15:37:59 -0400 | [diff] [blame^] | 93 | if (r < 0) { |
| 94 | vfree(op->downcall.trailer_buf); |
| 95 | op_release(op); |
| 96 | od->error = r; |
| 97 | return r; |
| 98 | } else if (op->downcall.status) { |
| 99 | vfree(op->downcall.trailer_buf); |
| 100 | op_release(op); |
| 101 | od->error = op->downcall.status; |
| 102 | return op->downcall.status; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 103 | } |
| 104 | |
Martin Brandenburg | 382f458 | 2017-04-25 15:37:59 -0400 | [diff] [blame^] | 105 | resp = (struct orangefs_readdir_response_s *) |
| 106 | op->downcall.trailer_buf; |
| 107 | od->token = resp->token; |
| 108 | |
| 109 | if (od->len + op->downcall.trailer_size - offset <= |
| 110 | MAX_DIRECTORY) { |
| 111 | memcpy(od->directory + od->len, |
| 112 | op->downcall.trailer_buf + offset, |
| 113 | op->downcall.trailer_size - offset); |
| 114 | od->len += op->downcall.trailer_size - offset; |
| 115 | } else { |
| 116 | /* This limit was chosen based on protocol limits. */ |
| 117 | gossip_err("orangefs_dir_more: userspace sent too much data\n"); |
| 118 | vfree(op->downcall.trailer_buf); |
| 119 | op_release(op); |
| 120 | od->error = -EIO; |
| 121 | return -EIO; |
Al Viro | 9f5e2f7 | 2016-02-16 19:54:13 -0500 | [diff] [blame] | 122 | } |
| 123 | |
Martin Brandenburg | 382f458 | 2017-04-25 15:37:59 -0400 | [diff] [blame^] | 124 | vfree(op->downcall.trailer_buf); |
| 125 | op_release(op); |
| 126 | return 0; |
| 127 | } |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 128 | |
Martin Brandenburg | 382f458 | 2017-04-25 15:37:59 -0400 | [diff] [blame^] | 129 | static int orangefs_dir_fill(struct orangefs_inode_s *oi, |
| 130 | struct orangefs_dir *od, struct dentry *dentry, |
| 131 | struct dir_context *ctx) |
| 132 | { |
| 133 | struct orangefs_khandle *khandle; |
| 134 | __u32 *len, padlen; |
| 135 | char *s; |
| 136 | while (od->i < od->len) { |
| 137 | if (od->len < od->i + sizeof *len) |
| 138 | goto eio; |
| 139 | len = od->directory + od->i; |
Mike Marshall | 88309aa | 2015-09-23 16:48:40 -0400 | [diff] [blame] | 140 | /* |
Martin Brandenburg | 382f458 | 2017-04-25 15:37:59 -0400 | [diff] [blame^] | 141 | * len is the size of the string itself. padlen is the |
| 142 | * total size of the encoded string. |
Mike Marshall | 88309aa | 2015-09-23 16:48:40 -0400 | [diff] [blame] | 143 | */ |
Martin Brandenburg | 382f458 | 2017-04-25 15:37:59 -0400 | [diff] [blame^] | 144 | padlen = (sizeof *len + *len + 1) + |
| 145 | (4 - (sizeof *len + *len + 1)%8)%8; |
| 146 | if (od->len < od->i + padlen + sizeof *khandle) |
| 147 | goto eio; |
| 148 | s = od->directory + od->i + sizeof *len; |
| 149 | if (s[*len] != 0) |
| 150 | goto eio; |
| 151 | khandle = od->directory + od->i + padlen; |
| 152 | |
| 153 | if (!dir_emit(ctx, s, *len, |
| 154 | orangefs_khandle_to_ino(khandle), DT_UNKNOWN)) |
| 155 | return 0; |
| 156 | od->i += padlen + sizeof *khandle; |
| 157 | od->i = od->i + (8 - od->i%8)%8; |
| 158 | ctx->pos = 2 + od->i; |
| 159 | } |
| 160 | BUG_ON(od->i > od->len); |
| 161 | return 0; |
| 162 | eio: |
| 163 | gossip_err("orangefs_dir_fill: userspace returns corrupt data\n"); |
| 164 | od->error = -EIO; |
| 165 | return -EIO; |
| 166 | } |
| 167 | |
| 168 | static int orangefs_dir_iterate(struct file *file, |
| 169 | struct dir_context *ctx) |
| 170 | { |
| 171 | struct orangefs_inode_s *oi; |
| 172 | struct orangefs_dir *od; |
| 173 | struct dentry *dentry; |
| 174 | int r; |
| 175 | |
| 176 | dentry = file->f_path.dentry; |
| 177 | oi = ORANGEFS_I(dentry->d_inode); |
| 178 | od = file->private_data; |
| 179 | |
| 180 | if (od->error) |
| 181 | return od->error; |
| 182 | |
| 183 | if (ctx->pos == 0) { |
| 184 | if (!dir_emit_dot(file, ctx)) |
| 185 | return 0; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 186 | ctx->pos++; |
Martin Brandenburg | 382f458 | 2017-04-25 15:37:59 -0400 | [diff] [blame^] | 187 | } |
| 188 | if (ctx->pos == 1) { |
| 189 | if (!dir_emit_dotdot(file, ctx)) |
| 190 | return 0; |
| 191 | ctx->pos++; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 192 | } |
| 193 | |
Martin Brandenburg | 382f458 | 2017-04-25 15:37:59 -0400 | [diff] [blame^] | 194 | r = 0; |
| 195 | |
| 196 | if (od->i < od->len) { |
| 197 | r = orangefs_dir_fill(oi, od, dentry, ctx); |
| 198 | if (r) |
| 199 | return r; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 200 | } |
| 201 | |
Martin Brandenburg | 382f458 | 2017-04-25 15:37:59 -0400 | [diff] [blame^] | 202 | if (od->token != ORANGEFS_READDIR_END) { |
| 203 | r = orangefs_dir_more(oi, od, dentry); |
| 204 | if (r) |
| 205 | return r; |
| 206 | r = orangefs_dir_fill(oi, od, dentry, ctx); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 207 | } |
| 208 | |
Martin Brandenburg | 382f458 | 2017-04-25 15:37:59 -0400 | [diff] [blame^] | 209 | return r; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 210 | } |
| 211 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 212 | static int orangefs_dir_open(struct inode *inode, struct file *file) |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 213 | { |
Martin Brandenburg | 382f458 | 2017-04-25 15:37:59 -0400 | [diff] [blame^] | 214 | struct orangefs_dir *od; |
| 215 | file->private_data = kmalloc(sizeof(struct orangefs_dir), |
| 216 | GFP_KERNEL); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 217 | if (!file->private_data) |
| 218 | return -ENOMEM; |
Martin Brandenburg | 382f458 | 2017-04-25 15:37:59 -0400 | [diff] [blame^] | 219 | od = file->private_data; |
| 220 | od->token = ORANGEFS_READDIR_START; |
| 221 | /* |
| 222 | * XXX: It seems wasteful to allocate such a large buffer for |
| 223 | * each request. Most will be much smaller. |
| 224 | */ |
| 225 | od->directory = alloc_pages_exact(MAX_DIRECTORY, GFP_KERNEL); |
| 226 | if (!od->directory) { |
| 227 | kfree(file->private_data); |
| 228 | return -ENOMEM; |
| 229 | } |
| 230 | od->i = 0; |
| 231 | od->len = 0; |
| 232 | od->error = 0; |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 233 | return 0; |
| 234 | } |
| 235 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 236 | static int orangefs_dir_release(struct inode *inode, struct file *file) |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 237 | { |
Martin Brandenburg | 382f458 | 2017-04-25 15:37:59 -0400 | [diff] [blame^] | 238 | struct orangefs_dir *od = file->private_data; |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 239 | orangefs_flush_inode(inode); |
Martin Brandenburg | 382f458 | 2017-04-25 15:37:59 -0400 | [diff] [blame^] | 240 | free_pages_exact(od->directory, MAX_DIRECTORY); |
| 241 | kfree(od); |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 242 | return 0; |
| 243 | } |
| 244 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 245 | const struct file_operations orangefs_dir_operations = { |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 246 | .read = generic_read_dir, |
Martin Brandenburg | 382f458 | 2017-04-25 15:37:59 -0400 | [diff] [blame^] | 247 | .iterate = orangefs_dir_iterate, |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 248 | .open = orangefs_dir_open, |
Martin Brandenburg | 382f458 | 2017-04-25 15:37:59 -0400 | [diff] [blame^] | 249 | .release = orangefs_dir_release |
Mike Marshall | 5db11c2 | 2015-07-17 10:38:12 -0400 | [diff] [blame] | 250 | }; |