Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 2 | #include <linux/ceph/ceph_debug.h> |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 3 | |
| 4 | #include <linux/spinlock.h> |
| 5 | #include <linux/fs_struct.h> |
| 6 | #include <linux/namei.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 7 | #include <linux/slab.h> |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 8 | #include <linux/sched.h> |
Andreas Gruenbacher | 2cdeb1e | 2016-04-14 00:30:17 +0200 | [diff] [blame] | 9 | #include <linux/xattr.h> |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 10 | |
| 11 | #include "super.h" |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 12 | #include "mds_client.h" |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 13 | |
| 14 | /* |
| 15 | * Directory operations: readdir, lookup, create, link, unlink, |
| 16 | * rename, etc. |
| 17 | */ |
| 18 | |
| 19 | /* |
| 20 | * Ceph MDS operations are specified in terms of a base ino and |
| 21 | * relative path. Thus, the client can specify an operation on a |
| 22 | * specific inode (e.g., a getattr due to fstat(2)), or as a path |
| 23 | * relative to, say, the root directory. |
| 24 | * |
| 25 | * Normally, we limit ourselves to strict inode ops (no path component) |
| 26 | * or dentry operations (a single path component relative to an ino). The |
| 27 | * exception to this is open_root_dentry(), which will open the mount |
| 28 | * point by name. |
| 29 | */ |
| 30 | |
Sage Weil | 52dfb8a | 2010-08-03 10:25:30 -0700 | [diff] [blame] | 31 | const struct dentry_operations ceph_dentry_ops; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 32 | |
| 33 | /* |
| 34 | * Initialize ceph dentry state. |
| 35 | */ |
Al Viro | ad5cb12 | 2016-10-28 22:05:13 -0400 | [diff] [blame] | 36 | static int ceph_d_init(struct dentry *dentry) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 37 | { |
| 38 | struct ceph_dentry_info *di; |
| 39 | |
Geliang Tang | 99ec269 | 2016-03-13 15:26:29 +0800 | [diff] [blame] | 40 | di = kmem_cache_zalloc(ceph_dentry_cachep, GFP_KERNEL); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 41 | if (!di) |
| 42 | return -ENOMEM; /* oh well */ |
| 43 | |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 44 | di->dentry = dentry; |
| 45 | di->lease_session = NULL; |
Miklos Szeredi | 9b16f03c | 2016-06-22 16:35:04 +0200 | [diff] [blame] | 46 | di->time = jiffies; |
Sage Weil | 48d0cbd | 2011-07-26 11:30:15 -0700 | [diff] [blame] | 47 | dentry->d_fsdata = di; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 48 | ceph_dentry_lru_add(dentry); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 49 | return 0; |
| 50 | } |
| 51 | |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 52 | /* |
Yan, Zheng | f3c4ebe | 2016-04-29 11:27:30 +0800 | [diff] [blame] | 53 | * for f_pos for readdir: |
| 54 | * - hash order: |
| 55 | * (0xff << 52) | ((24 bits hash) << 28) | |
| 56 | * (the nth entry has hash collision); |
| 57 | * - frag+name order; |
| 58 | * ((frag value) << 28) | (the nth entry in frag); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 59 | */ |
Yan, Zheng | f3c4ebe | 2016-04-29 11:27:30 +0800 | [diff] [blame] | 60 | #define OFFSET_BITS 28 |
| 61 | #define OFFSET_MASK ((1 << OFFSET_BITS) - 1) |
| 62 | #define HASH_ORDER (0xffull << (OFFSET_BITS + 24)) |
| 63 | loff_t ceph_make_fpos(unsigned high, unsigned off, bool hash_order) |
| 64 | { |
| 65 | loff_t fpos = ((loff_t)high << 28) | (loff_t)off; |
| 66 | if (hash_order) |
| 67 | fpos |= HASH_ORDER; |
| 68 | return fpos; |
| 69 | } |
| 70 | |
| 71 | static bool is_hash_order(loff_t p) |
| 72 | { |
| 73 | return (p & HASH_ORDER) == HASH_ORDER; |
| 74 | } |
| 75 | |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 76 | static unsigned fpos_frag(loff_t p) |
| 77 | { |
Yan, Zheng | f3c4ebe | 2016-04-29 11:27:30 +0800 | [diff] [blame] | 78 | return p >> OFFSET_BITS; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 79 | } |
Yan, Zheng | f3c4ebe | 2016-04-29 11:27:30 +0800 | [diff] [blame] | 80 | |
| 81 | static unsigned fpos_hash(loff_t p) |
| 82 | { |
| 83 | return ceph_frag_value(fpos_frag(p)); |
| 84 | } |
| 85 | |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 86 | static unsigned fpos_off(loff_t p) |
| 87 | { |
Yan, Zheng | f3c4ebe | 2016-04-29 11:27:30 +0800 | [diff] [blame] | 88 | return p & OFFSET_MASK; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Yan, Zheng | 4d5f5df | 2014-02-13 19:40:26 +0800 | [diff] [blame] | 91 | static int fpos_cmp(loff_t l, loff_t r) |
| 92 | { |
| 93 | int v = ceph_frag_compare(fpos_frag(l), fpos_frag(r)); |
| 94 | if (v) |
| 95 | return v; |
| 96 | return (int)(fpos_off(l) - fpos_off(r)); |
| 97 | } |
| 98 | |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 99 | /* |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 100 | * make note of the last dentry we read, so we can |
| 101 | * continue at the same lexicographical point, |
| 102 | * regardless of what dir changes take place on the |
| 103 | * server. |
| 104 | */ |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 105 | static int note_last_dentry(struct ceph_dir_file_info *dfi, const char *name, |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 106 | int len, unsigned next_offset) |
| 107 | { |
| 108 | char *buf = kmalloc(len+1, GFP_KERNEL); |
| 109 | if (!buf) |
| 110 | return -ENOMEM; |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 111 | kfree(dfi->last_name); |
| 112 | dfi->last_name = buf; |
| 113 | memcpy(dfi->last_name, name, len); |
| 114 | dfi->last_name[len] = 0; |
| 115 | dfi->next_offset = next_offset; |
| 116 | dout("note_last_dentry '%s'\n", dfi->last_name); |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 117 | return 0; |
| 118 | } |
| 119 | |
Yan, Zheng | c530cd2 | 2016-04-28 17:43:35 +0800 | [diff] [blame] | 120 | |
| 121 | static struct dentry * |
| 122 | __dcache_find_get_entry(struct dentry *parent, u64 idx, |
| 123 | struct ceph_readdir_cache_control *cache_ctl) |
| 124 | { |
| 125 | struct inode *dir = d_inode(parent); |
| 126 | struct dentry *dentry; |
| 127 | unsigned idx_mask = (PAGE_SIZE / sizeof(struct dentry *)) - 1; |
| 128 | loff_t ptr_pos = idx * sizeof(struct dentry *); |
| 129 | pgoff_t ptr_pgoff = ptr_pos >> PAGE_SHIFT; |
| 130 | |
| 131 | if (ptr_pos >= i_size_read(dir)) |
| 132 | return NULL; |
| 133 | |
| 134 | if (!cache_ctl->page || ptr_pgoff != page_index(cache_ctl->page)) { |
| 135 | ceph_readdir_cache_release(cache_ctl); |
| 136 | cache_ctl->page = find_lock_page(&dir->i_data, ptr_pgoff); |
| 137 | if (!cache_ctl->page) { |
| 138 | dout(" page %lu not found\n", ptr_pgoff); |
| 139 | return ERR_PTR(-EAGAIN); |
| 140 | } |
| 141 | /* reading/filling the cache are serialized by |
| 142 | i_mutex, no need to use page lock */ |
| 143 | unlock_page(cache_ctl->page); |
| 144 | cache_ctl->dentries = kmap(cache_ctl->page); |
| 145 | } |
| 146 | |
| 147 | cache_ctl->index = idx & idx_mask; |
| 148 | |
| 149 | rcu_read_lock(); |
| 150 | spin_lock(&parent->d_lock); |
| 151 | /* check i_size again here, because empty directory can be |
| 152 | * marked as complete while not holding the i_mutex. */ |
| 153 | if (ceph_dir_is_complete_ordered(dir) && ptr_pos < i_size_read(dir)) |
| 154 | dentry = cache_ctl->dentries[cache_ctl->index]; |
| 155 | else |
| 156 | dentry = NULL; |
| 157 | spin_unlock(&parent->d_lock); |
| 158 | if (dentry && !lockref_get_not_dead(&dentry->d_lockref)) |
| 159 | dentry = NULL; |
| 160 | rcu_read_unlock(); |
| 161 | return dentry ? : ERR_PTR(-EAGAIN); |
| 162 | } |
| 163 | |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 164 | /* |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 165 | * When possible, we try to satisfy a readdir by peeking at the |
| 166 | * dcache. We make this work by carefully ordering dentries on |
Al Viro | 946e51f | 2014-10-26 19:19:16 -0400 | [diff] [blame] | 167 | * d_child when we initially get results back from the MDS, and |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 168 | * falling back to a "normal" sync readdir if any dentries in the dir |
| 169 | * are dropped. |
| 170 | * |
Yan, Zheng | 2f276c5 | 2013-03-13 19:44:32 +0800 | [diff] [blame] | 171 | * Complete dir indicates that we have all dentries in the dir. It is |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 172 | * defined IFF we hold CEPH_CAP_FILE_SHARED (which will be revoked by |
| 173 | * the MDS if/when the directory is modified). |
| 174 | */ |
Yan, Zheng | a30be7c | 2014-04-06 14:10:04 +0800 | [diff] [blame] | 175 | static int __dcache_readdir(struct file *file, struct dir_context *ctx, |
Yan, Zheng | 97aeb6b | 2017-11-27 10:47:46 +0800 | [diff] [blame] | 176 | int shared_gen) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 177 | { |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 178 | struct ceph_dir_file_info *dfi = file->private_data; |
Al Viro | b583043 | 2014-10-31 01:22:04 -0400 | [diff] [blame] | 179 | struct dentry *parent = file->f_path.dentry; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 180 | struct inode *dir = d_inode(parent); |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 181 | struct dentry *dentry, *last = NULL; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 182 | struct ceph_dentry_info *di; |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 183 | struct ceph_readdir_cache_control cache_ctl = {}; |
Yan, Zheng | c530cd2 | 2016-04-28 17:43:35 +0800 | [diff] [blame] | 184 | u64 idx = 0; |
| 185 | int err = 0; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 186 | |
Yan, Zheng | 97aeb6b | 2017-11-27 10:47:46 +0800 | [diff] [blame] | 187 | dout("__dcache_readdir %p v%u at %llx\n", dir, (unsigned)shared_gen, ctx->pos); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 188 | |
Yan, Zheng | c530cd2 | 2016-04-28 17:43:35 +0800 | [diff] [blame] | 189 | /* search start position */ |
| 190 | if (ctx->pos > 2) { |
| 191 | u64 count = div_u64(i_size_read(dir), sizeof(struct dentry *)); |
| 192 | while (count > 0) { |
| 193 | u64 step = count >> 1; |
| 194 | dentry = __dcache_find_get_entry(parent, idx + step, |
| 195 | &cache_ctl); |
| 196 | if (!dentry) { |
| 197 | /* use linar search */ |
| 198 | idx = 0; |
| 199 | break; |
| 200 | } |
| 201 | if (IS_ERR(dentry)) { |
| 202 | err = PTR_ERR(dentry); |
| 203 | goto out; |
| 204 | } |
| 205 | di = ceph_dentry(dentry); |
| 206 | spin_lock(&dentry->d_lock); |
| 207 | if (fpos_cmp(di->offset, ctx->pos) < 0) { |
| 208 | idx += step + 1; |
| 209 | count -= step + 1; |
| 210 | } else { |
| 211 | count = step; |
| 212 | } |
| 213 | spin_unlock(&dentry->d_lock); |
| 214 | dput(dentry); |
| 215 | } |
| 216 | |
| 217 | dout("__dcache_readdir %p cache idx %llu\n", dir, idx); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 220 | |
Yan, Zheng | c530cd2 | 2016-04-28 17:43:35 +0800 | [diff] [blame] | 221 | for (;;) { |
| 222 | bool emit_dentry = false; |
| 223 | dentry = __dcache_find_get_entry(parent, idx++, &cache_ctl); |
| 224 | if (!dentry) { |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 225 | dfi->file_info.flags |= CEPH_F_ATEND; |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 226 | err = 0; |
| 227 | break; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 228 | } |
Yan, Zheng | c530cd2 | 2016-04-28 17:43:35 +0800 | [diff] [blame] | 229 | if (IS_ERR(dentry)) { |
| 230 | err = PTR_ERR(dentry); |
| 231 | goto out; |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 232 | } |
| 233 | |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 234 | spin_lock(&dentry->d_lock); |
Yan, Zheng | 5495c2d | 2017-11-27 11:23:48 +0800 | [diff] [blame] | 235 | di = ceph_dentry(dentry); |
| 236 | if (d_unhashed(dentry) || |
| 237 | d_really_is_negative(dentry) || |
| 238 | di->lease_shared_gen != shared_gen) { |
| 239 | spin_unlock(&dentry->d_lock); |
| 240 | dput(dentry); |
| 241 | err = -EAGAIN; |
| 242 | goto out; |
| 243 | } |
| 244 | if (fpos_cmp(ctx->pos, di->offset) <= 0) { |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 245 | emit_dentry = true; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 246 | } |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 247 | spin_unlock(&dentry->d_lock); |
| 248 | |
| 249 | if (emit_dentry) { |
Yan, Zheng | f3c4ebe | 2016-04-29 11:27:30 +0800 | [diff] [blame] | 250 | dout(" %llx dentry %p %pd %p\n", di->offset, |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 251 | dentry, dentry, d_inode(dentry)); |
| 252 | ctx->pos = di->offset; |
| 253 | if (!dir_emit(ctx, dentry->d_name.name, |
| 254 | dentry->d_name.len, |
| 255 | ceph_translate_ino(dentry->d_sb, |
| 256 | d_inode(dentry)->i_ino), |
| 257 | d_inode(dentry)->i_mode >> 12)) { |
| 258 | dput(dentry); |
| 259 | err = 0; |
| 260 | break; |
| 261 | } |
| 262 | ctx->pos++; |
| 263 | |
| 264 | if (last) |
| 265 | dput(last); |
| 266 | last = dentry; |
| 267 | } else { |
| 268 | dput(dentry); |
| 269 | } |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 270 | } |
Yan, Zheng | c530cd2 | 2016-04-28 17:43:35 +0800 | [diff] [blame] | 271 | out: |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 272 | ceph_readdir_cache_release(&cache_ctl); |
| 273 | if (last) { |
| 274 | int ret; |
| 275 | di = ceph_dentry(last); |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 276 | ret = note_last_dentry(dfi, last->d_name.name, last->d_name.len, |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 277 | fpos_off(di->offset) + 1); |
| 278 | if (ret < 0) |
| 279 | err = ret; |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 280 | dput(last); |
Yan, Zheng | 84583cfb | 2017-07-06 11:12:21 +0800 | [diff] [blame] | 281 | /* last_name no longer match cache index */ |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 282 | if (dfi->readdir_cache_idx >= 0) { |
| 283 | dfi->readdir_cache_idx = -1; |
| 284 | dfi->dir_release_count = 0; |
Yan, Zheng | 84583cfb | 2017-07-06 11:12:21 +0800 | [diff] [blame] | 285 | } |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 286 | } |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 287 | return err; |
| 288 | } |
| 289 | |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 290 | static bool need_send_readdir(struct ceph_dir_file_info *dfi, loff_t pos) |
Yan, Zheng | f3c4ebe | 2016-04-29 11:27:30 +0800 | [diff] [blame] | 291 | { |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 292 | if (!dfi->last_readdir) |
Yan, Zheng | f3c4ebe | 2016-04-29 11:27:30 +0800 | [diff] [blame] | 293 | return true; |
| 294 | if (is_hash_order(pos)) |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 295 | return !ceph_frag_contains_value(dfi->frag, fpos_hash(pos)); |
Yan, Zheng | f3c4ebe | 2016-04-29 11:27:30 +0800 | [diff] [blame] | 296 | else |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 297 | return dfi->frag != fpos_frag(pos); |
Yan, Zheng | f3c4ebe | 2016-04-29 11:27:30 +0800 | [diff] [blame] | 298 | } |
| 299 | |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 300 | static int ceph_readdir(struct file *file, struct dir_context *ctx) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 301 | { |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 302 | struct ceph_dir_file_info *dfi = file->private_data; |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 303 | struct inode *inode = file_inode(file); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 304 | struct ceph_inode_info *ci = ceph_inode(inode); |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 305 | struct ceph_fs_client *fsc = ceph_inode_to_client(inode); |
| 306 | struct ceph_mds_client *mdsc = fsc->mdsc; |
Yan, Zheng | 8974eeb | 2016-04-28 15:17:40 +0800 | [diff] [blame] | 307 | int i; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 308 | int err; |
Yan, Zheng | b50c2de | 2017-04-24 11:56:50 +0800 | [diff] [blame] | 309 | unsigned frag = -1; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 310 | struct ceph_mds_reply_info_parsed *rinfo; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 311 | |
Yan, Zheng | 8974eeb | 2016-04-28 15:17:40 +0800 | [diff] [blame] | 312 | dout("readdir %p file %p pos %llx\n", inode, file, ctx->pos); |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 313 | if (dfi->file_info.flags & CEPH_F_ATEND) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 314 | return 0; |
| 315 | |
| 316 | /* always start with . and .. */ |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 317 | if (ctx->pos == 0) { |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 318 | dout("readdir off 0 -> '.'\n"); |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 319 | if (!dir_emit(ctx, ".", 1, |
Yehuda Sadeh | ad1fee9 | 2011-01-21 16:44:03 -0800 | [diff] [blame] | 320 | ceph_translate_ino(inode->i_sb, inode->i_ino), |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 321 | inode->i_mode >> 12)) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 322 | return 0; |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 323 | ctx->pos = 1; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 324 | } |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 325 | if (ctx->pos == 1) { |
Al Viro | b583043 | 2014-10-31 01:22:04 -0400 | [diff] [blame] | 326 | ino_t ino = parent_ino(file->f_path.dentry); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 327 | dout("readdir off 1 -> '..'\n"); |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 328 | if (!dir_emit(ctx, "..", 2, |
Yehuda Sadeh | ad1fee9 | 2011-01-21 16:44:03 -0800 | [diff] [blame] | 329 | ceph_translate_ino(inode->i_sb, ino), |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 330 | inode->i_mode >> 12)) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 331 | return 0; |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 332 | ctx->pos = 2; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | /* can we use the dcache? */ |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 336 | spin_lock(&ci->i_ceph_lock); |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 337 | if (ceph_test_mount_opt(fsc, DCACHE) && |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 338 | !ceph_test_mount_opt(fsc, NOASYNCREADDIR) && |
Sage Weil | a0dff78 | 2010-07-22 13:47:21 -0700 | [diff] [blame] | 339 | ceph_snap(inode) != CEPH_SNAPDIR && |
Yan, Zheng | 70db4f3 | 2014-10-21 18:09:56 -0700 | [diff] [blame] | 340 | __ceph_dir_is_complete_ordered(ci) && |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 341 | __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1)) { |
Yan, Zheng | 97aeb6b | 2017-11-27 10:47:46 +0800 | [diff] [blame] | 342 | int shared_gen = atomic_read(&ci->i_shared_gen); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 343 | spin_unlock(&ci->i_ceph_lock); |
Yan, Zheng | a30be7c | 2014-04-06 14:10:04 +0800 | [diff] [blame] | 344 | err = __dcache_readdir(file, ctx, shared_gen); |
Sage Weil | efa4c12 | 2010-10-18 14:04:31 -0700 | [diff] [blame] | 345 | if (err != -EAGAIN) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 346 | return err; |
Sage Weil | efa4c12 | 2010-10-18 14:04:31 -0700 | [diff] [blame] | 347 | } else { |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 348 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 349 | } |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 350 | |
| 351 | /* proceed with a normal readdir */ |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 352 | more: |
| 353 | /* do we have the correct frag content buffered? */ |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 354 | if (need_send_readdir(dfi, ctx->pos)) { |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 355 | struct ceph_mds_request *req; |
| 356 | int op = ceph_snap(inode) == CEPH_SNAPDIR ? |
| 357 | CEPH_MDS_OP_LSSNAP : CEPH_MDS_OP_READDIR; |
| 358 | |
| 359 | /* discard old result, if any */ |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 360 | if (dfi->last_readdir) { |
| 361 | ceph_mdsc_put_request(dfi->last_readdir); |
| 362 | dfi->last_readdir = NULL; |
Sage Weil | 393f662 | 2010-03-10 12:03:32 -0800 | [diff] [blame] | 363 | } |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 364 | |
Yan, Zheng | f3c4ebe | 2016-04-29 11:27:30 +0800 | [diff] [blame] | 365 | if (is_hash_order(ctx->pos)) { |
Yan, Zheng | b50c2de | 2017-04-24 11:56:50 +0800 | [diff] [blame] | 366 | /* fragtree isn't always accurate. choose frag |
| 367 | * based on previous reply when possible. */ |
| 368 | if (frag == (unsigned)-1) |
| 369 | frag = ceph_choose_frag(ci, fpos_hash(ctx->pos), |
| 370 | NULL, NULL); |
Yan, Zheng | f3c4ebe | 2016-04-29 11:27:30 +0800 | [diff] [blame] | 371 | } else { |
| 372 | frag = fpos_frag(ctx->pos); |
| 373 | } |
| 374 | |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 375 | dout("readdir fetching %llx.%llx frag %x offset '%s'\n", |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 376 | ceph_vinop(inode), frag, dfi->last_name); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 377 | req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS); |
| 378 | if (IS_ERR(req)) |
| 379 | return PTR_ERR(req); |
Yan, Zheng | 5400839 | 2014-03-29 13:41:15 +0800 | [diff] [blame] | 380 | err = ceph_alloc_readdir_reply_buffer(req, inode); |
| 381 | if (err) { |
| 382 | ceph_mdsc_put_request(req); |
| 383 | return err; |
| 384 | } |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 385 | /* hints to request -> mds selection code */ |
| 386 | req->r_direct_mode = USE_AUTH_MDS; |
Yan, Zheng | 5d37ca1 | 2017-07-26 12:48:08 +0800 | [diff] [blame] | 387 | if (op == CEPH_MDS_OP_READDIR) { |
| 388 | req->r_direct_hash = ceph_frag_value(frag); |
| 389 | __set_bit(CEPH_MDS_R_DIRECT_IS_HASH, &req->r_req_flags); |
Yan, Zheng | 87c91a9 | 2017-11-23 18:28:16 +0800 | [diff] [blame] | 390 | req->r_inode_drop = CEPH_CAP_FILE_EXCL; |
Yan, Zheng | 5d37ca1 | 2017-07-26 12:48:08 +0800 | [diff] [blame] | 391 | } |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 392 | if (dfi->last_name) { |
| 393 | req->r_path2 = kstrdup(dfi->last_name, GFP_KERNEL); |
Sanidhya Kashyap | a149bb9 | 2015-03-21 12:54:58 -0400 | [diff] [blame] | 394 | if (!req->r_path2) { |
| 395 | ceph_mdsc_put_request(req); |
| 396 | return -ENOMEM; |
| 397 | } |
Yan, Zheng | 7916254 | 2017-04-05 12:54:05 -0400 | [diff] [blame] | 398 | } else if (is_hash_order(ctx->pos)) { |
| 399 | req->r_args.readdir.offset_hash = |
| 400 | cpu_to_le32(fpos_hash(ctx->pos)); |
Sanidhya Kashyap | a149bb9 | 2015-03-21 12:54:58 -0400 | [diff] [blame] | 401 | } |
Yan, Zheng | 7916254 | 2017-04-05 12:54:05 -0400 | [diff] [blame] | 402 | |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 403 | req->r_dir_release_cnt = dfi->dir_release_count; |
| 404 | req->r_dir_ordered_cnt = dfi->dir_ordered_count; |
| 405 | req->r_readdir_cache_idx = dfi->readdir_cache_idx; |
| 406 | req->r_readdir_offset = dfi->next_offset; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 407 | req->r_args.readdir.frag = cpu_to_le32(frag); |
Yan, Zheng | 956d39d | 2016-04-27 17:48:30 +0800 | [diff] [blame] | 408 | req->r_args.readdir.flags = |
| 409 | cpu_to_le16(CEPH_READDIR_REPLY_BITFLAGS); |
Sanidhya Kashyap | a149bb9 | 2015-03-21 12:54:58 -0400 | [diff] [blame] | 410 | |
| 411 | req->r_inode = inode; |
| 412 | ihold(inode); |
| 413 | req->r_dentry = dget(file->f_path.dentry); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 414 | err = ceph_mdsc_do_request(mdsc, NULL, req); |
| 415 | if (err < 0) { |
| 416 | ceph_mdsc_put_request(req); |
| 417 | return err; |
| 418 | } |
Yan, Zheng | f3c4ebe | 2016-04-29 11:27:30 +0800 | [diff] [blame] | 419 | dout("readdir got and parsed readdir result=%d on " |
| 420 | "frag %x, end=%d, complete=%d, hash_order=%d\n", |
| 421 | err, frag, |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 422 | (int)req->r_reply_info.dir_end, |
Yan, Zheng | f3c4ebe | 2016-04-29 11:27:30 +0800 | [diff] [blame] | 423 | (int)req->r_reply_info.dir_complete, |
| 424 | (int)req->r_reply_info.hash_order); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 425 | |
Yan, Zheng | 81c6aea | 2013-09-18 09:44:13 +0800 | [diff] [blame] | 426 | rinfo = &req->r_reply_info; |
| 427 | if (le32_to_cpu(rinfo->dir_dir->frag) != frag) { |
| 428 | frag = le32_to_cpu(rinfo->dir_dir->frag); |
Yan, Zheng | f3c4ebe | 2016-04-29 11:27:30 +0800 | [diff] [blame] | 429 | if (!rinfo->hash_order) { |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 430 | dfi->next_offset = req->r_readdir_offset; |
Yan, Zheng | f3c4ebe | 2016-04-29 11:27:30 +0800 | [diff] [blame] | 431 | /* adjust ctx->pos to beginning of frag */ |
| 432 | ctx->pos = ceph_make_fpos(frag, |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 433 | dfi->next_offset, |
Yan, Zheng | f3c4ebe | 2016-04-29 11:27:30 +0800 | [diff] [blame] | 434 | false); |
| 435 | } |
Yan, Zheng | 81c6aea | 2013-09-18 09:44:13 +0800 | [diff] [blame] | 436 | } |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 437 | |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 438 | dfi->frag = frag; |
| 439 | dfi->last_readdir = req; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 440 | |
Jeff Layton | bc2de10d | 2017-02-01 13:49:09 -0500 | [diff] [blame] | 441 | if (test_bit(CEPH_MDS_R_DID_PREPOPULATE, &req->r_req_flags)) { |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 442 | dfi->readdir_cache_idx = req->r_readdir_cache_idx; |
| 443 | if (dfi->readdir_cache_idx < 0) { |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 444 | /* preclude from marking dir ordered */ |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 445 | dfi->dir_ordered_count = 0; |
Yan, Zheng | 8974eeb | 2016-04-28 15:17:40 +0800 | [diff] [blame] | 446 | } else if (ceph_frag_is_leftmost(frag) && |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 447 | dfi->next_offset == 2) { |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 448 | /* note dir version at start of readdir so |
| 449 | * we can tell if any dentries get dropped */ |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 450 | dfi->dir_release_count = req->r_dir_release_cnt; |
| 451 | dfi->dir_ordered_count = req->r_dir_ordered_cnt; |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 452 | } |
| 453 | } else { |
Chengguang Xu | 4c069a5 | 2018-01-30 16:29:17 +0800 | [diff] [blame] | 454 | dout("readdir !did_prepopulate\n"); |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 455 | /* disable readdir cache */ |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 456 | dfi->readdir_cache_idx = -1; |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 457 | /* preclude from marking dir complete */ |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 458 | dfi->dir_release_count = 0; |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 459 | } |
| 460 | |
Yan, Zheng | f3c4ebe | 2016-04-29 11:27:30 +0800 | [diff] [blame] | 461 | /* note next offset and last dentry name */ |
| 462 | if (rinfo->dir_nr > 0) { |
Yan, Zheng | 2a5beea | 2016-04-28 09:37:39 +0800 | [diff] [blame] | 463 | struct ceph_mds_reply_dir_entry *rde = |
| 464 | rinfo->dir_entries + (rinfo->dir_nr-1); |
Yan, Zheng | f3c4ebe | 2016-04-29 11:27:30 +0800 | [diff] [blame] | 465 | unsigned next_offset = req->r_reply_info.dir_end ? |
| 466 | 2 : (fpos_off(rde->offset) + 1); |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 467 | err = note_last_dentry(dfi, rde->name, rde->name_len, |
Yan, Zheng | f3c4ebe | 2016-04-29 11:27:30 +0800 | [diff] [blame] | 468 | next_offset); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 469 | if (err) |
| 470 | return err; |
Yan, Zheng | f3c4ebe | 2016-04-29 11:27:30 +0800 | [diff] [blame] | 471 | } else if (req->r_reply_info.dir_end) { |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 472 | dfi->next_offset = 2; |
Yan, Zheng | f3c4ebe | 2016-04-29 11:27:30 +0800 | [diff] [blame] | 473 | /* keep last name */ |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 477 | rinfo = &dfi->last_readdir->r_reply_info; |
Yan, Zheng | 8974eeb | 2016-04-28 15:17:40 +0800 | [diff] [blame] | 478 | dout("readdir frag %x num %d pos %llx chunk first %llx\n", |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 479 | dfi->frag, rinfo->dir_nr, ctx->pos, |
Yan, Zheng | 8974eeb | 2016-04-28 15:17:40 +0800 | [diff] [blame] | 480 | rinfo->dir_nr ? rinfo->dir_entries[0].offset : 0LL); |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 481 | |
Yan, Zheng | 8974eeb | 2016-04-28 15:17:40 +0800 | [diff] [blame] | 482 | i = 0; |
| 483 | /* search start position */ |
| 484 | if (rinfo->dir_nr > 0) { |
| 485 | int step, nr = rinfo->dir_nr; |
| 486 | while (nr > 0) { |
| 487 | step = nr >> 1; |
| 488 | if (rinfo->dir_entries[i + step].offset < ctx->pos) { |
| 489 | i += step + 1; |
| 490 | nr -= step + 1; |
| 491 | } else { |
| 492 | nr = step; |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | for (; i < rinfo->dir_nr; i++) { |
| 497 | struct ceph_mds_reply_dir_entry *rde = rinfo->dir_entries + i; |
Sage Weil | 3105c19 | 2010-11-18 09:15:07 -0800 | [diff] [blame] | 498 | struct ceph_vino vino; |
| 499 | ino_t ino; |
Yan, Zheng | b50c2de | 2017-04-24 11:56:50 +0800 | [diff] [blame] | 500 | u32 ftype; |
Sage Weil | 3105c19 | 2010-11-18 09:15:07 -0800 | [diff] [blame] | 501 | |
Yan, Zheng | 8974eeb | 2016-04-28 15:17:40 +0800 | [diff] [blame] | 502 | BUG_ON(rde->offset < ctx->pos); |
| 503 | |
| 504 | ctx->pos = rde->offset; |
| 505 | dout("readdir (%d/%d) -> %llx '%.*s' %p\n", |
| 506 | i, rinfo->dir_nr, ctx->pos, |
Yan, Zheng | 2a5beea | 2016-04-28 09:37:39 +0800 | [diff] [blame] | 507 | rde->name_len, rde->name, &rde->inode.in); |
Yan, Zheng | 8974eeb | 2016-04-28 15:17:40 +0800 | [diff] [blame] | 508 | |
Yan, Zheng | 2a5beea | 2016-04-28 09:37:39 +0800 | [diff] [blame] | 509 | BUG_ON(!rde->inode.in); |
| 510 | ftype = le32_to_cpu(rde->inode.in->mode) >> 12; |
| 511 | vino.ino = le64_to_cpu(rde->inode.in->ino); |
| 512 | vino.snap = le64_to_cpu(rde->inode.in->snapid); |
Sage Weil | 3105c19 | 2010-11-18 09:15:07 -0800 | [diff] [blame] | 513 | ino = ceph_vino_to_ino(vino); |
Yan, Zheng | 8974eeb | 2016-04-28 15:17:40 +0800 | [diff] [blame] | 514 | |
Yan, Zheng | 2a5beea | 2016-04-28 09:37:39 +0800 | [diff] [blame] | 515 | if (!dir_emit(ctx, rde->name, rde->name_len, |
| 516 | ceph_translate_ino(inode->i_sb, ino), ftype)) { |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 517 | dout("filldir stopping us...\n"); |
| 518 | return 0; |
| 519 | } |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 520 | ctx->pos++; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 523 | ceph_mdsc_put_request(dfi->last_readdir); |
| 524 | dfi->last_readdir = NULL; |
Yan, Zheng | b50c2de | 2017-04-24 11:56:50 +0800 | [diff] [blame] | 525 | |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 526 | if (dfi->next_offset > 2) { |
| 527 | frag = dfi->frag; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 528 | goto more; |
| 529 | } |
| 530 | |
| 531 | /* more frags? */ |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 532 | if (!ceph_frag_is_rightmost(dfi->frag)) { |
| 533 | frag = ceph_frag_next(dfi->frag); |
Yan, Zheng | f3c4ebe | 2016-04-29 11:27:30 +0800 | [diff] [blame] | 534 | if (is_hash_order(ctx->pos)) { |
| 535 | loff_t new_pos = ceph_make_fpos(ceph_frag_value(frag), |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 536 | dfi->next_offset, true); |
Yan, Zheng | f3c4ebe | 2016-04-29 11:27:30 +0800 | [diff] [blame] | 537 | if (new_pos > ctx->pos) |
| 538 | ctx->pos = new_pos; |
| 539 | /* keep last_name */ |
| 540 | } else { |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 541 | ctx->pos = ceph_make_fpos(frag, dfi->next_offset, |
| 542 | false); |
| 543 | kfree(dfi->last_name); |
| 544 | dfi->last_name = NULL; |
Yan, Zheng | f3c4ebe | 2016-04-29 11:27:30 +0800 | [diff] [blame] | 545 | } |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 546 | dout("readdir next frag is %x\n", frag); |
| 547 | goto more; |
| 548 | } |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 549 | dfi->file_info.flags |= CEPH_F_ATEND; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 550 | |
| 551 | /* |
| 552 | * if dir_release_count still matches the dir, no dentries |
| 553 | * were released during the whole readdir, and we should have |
| 554 | * the complete dir contents in our cache. |
| 555 | */ |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 556 | if (atomic64_read(&ci->i_release_count) == |
| 557 | dfi->dir_release_count) { |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 558 | spin_lock(&ci->i_ceph_lock); |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 559 | if (dfi->dir_ordered_count == |
| 560 | atomic64_read(&ci->i_ordered_count)) { |
Yan, Zheng | 70db4f3 | 2014-10-21 18:09:56 -0700 | [diff] [blame] | 561 | dout(" marking %p complete and ordered\n", inode); |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 562 | /* use i_size to track number of entries in |
| 563 | * readdir cache */ |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 564 | BUG_ON(dfi->readdir_cache_idx < 0); |
| 565 | i_size_write(inode, dfi->readdir_cache_idx * |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 566 | sizeof(struct dentry*)); |
| 567 | } else { |
Yan, Zheng | 70db4f3 | 2014-10-21 18:09:56 -0700 | [diff] [blame] | 568 | dout(" marking %p complete\n", inode); |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 569 | } |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 570 | __ceph_dir_set_complete(ci, dfi->dir_release_count, |
| 571 | dfi->dir_ordered_count); |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 572 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 573 | } |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 574 | |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 575 | dout("readdir %p file %p done.\n", inode, file); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 576 | return 0; |
| 577 | } |
| 578 | |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 579 | static void reset_readdir(struct ceph_dir_file_info *dfi) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 580 | { |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 581 | if (dfi->last_readdir) { |
| 582 | ceph_mdsc_put_request(dfi->last_readdir); |
| 583 | dfi->last_readdir = NULL; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 584 | } |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 585 | kfree(dfi->last_name); |
| 586 | dfi->last_name = NULL; |
| 587 | dfi->dir_release_count = 0; |
| 588 | dfi->readdir_cache_idx = -1; |
| 589 | dfi->next_offset = 2; /* compensate for . and .. */ |
| 590 | dfi->file_info.flags &= ~CEPH_F_ATEND; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Yan, Zheng | 8974eeb | 2016-04-28 15:17:40 +0800 | [diff] [blame] | 593 | /* |
| 594 | * discard buffered readdir content on seekdir(0), or seek to new frag, |
| 595 | * or seek prior to current chunk |
| 596 | */ |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 597 | static bool need_reset_readdir(struct ceph_dir_file_info *dfi, loff_t new_pos) |
Yan, Zheng | 8974eeb | 2016-04-28 15:17:40 +0800 | [diff] [blame] | 598 | { |
| 599 | struct ceph_mds_reply_info_parsed *rinfo; |
Yan, Zheng | f3c4ebe | 2016-04-29 11:27:30 +0800 | [diff] [blame] | 600 | loff_t chunk_offset; |
Yan, Zheng | 8974eeb | 2016-04-28 15:17:40 +0800 | [diff] [blame] | 601 | if (new_pos == 0) |
| 602 | return true; |
Yan, Zheng | f3c4ebe | 2016-04-29 11:27:30 +0800 | [diff] [blame] | 603 | if (is_hash_order(new_pos)) { |
| 604 | /* no need to reset last_name for a forward seek when |
| 605 | * dentries are sotred in hash order */ |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 606 | } else if (dfi->frag != fpos_frag(new_pos)) { |
Yan, Zheng | 8974eeb | 2016-04-28 15:17:40 +0800 | [diff] [blame] | 607 | return true; |
Yan, Zheng | f3c4ebe | 2016-04-29 11:27:30 +0800 | [diff] [blame] | 608 | } |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 609 | rinfo = dfi->last_readdir ? &dfi->last_readdir->r_reply_info : NULL; |
Yan, Zheng | 8974eeb | 2016-04-28 15:17:40 +0800 | [diff] [blame] | 610 | if (!rinfo || !rinfo->dir_nr) |
| 611 | return true; |
Yan, Zheng | f3c4ebe | 2016-04-29 11:27:30 +0800 | [diff] [blame] | 612 | chunk_offset = rinfo->dir_entries[0].offset; |
| 613 | return new_pos < chunk_offset || |
| 614 | is_hash_order(new_pos) != is_hash_order(chunk_offset); |
Yan, Zheng | 8974eeb | 2016-04-28 15:17:40 +0800 | [diff] [blame] | 615 | } |
| 616 | |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 617 | static loff_t ceph_dir_llseek(struct file *file, loff_t offset, int whence) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 618 | { |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 619 | struct ceph_dir_file_info *dfi = file->private_data; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 620 | struct inode *inode = file->f_mapping->host; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 621 | loff_t retval; |
| 622 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 623 | inode_lock(inode); |
Josef Bacik | 06222e4 | 2011-07-18 13:21:38 -0400 | [diff] [blame] | 624 | retval = -EINVAL; |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 625 | switch (whence) { |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 626 | case SEEK_CUR: |
| 627 | offset += file->f_pos; |
Josef Bacik | 06222e4 | 2011-07-18 13:21:38 -0400 | [diff] [blame] | 628 | case SEEK_SET: |
| 629 | break; |
Yan, Zheng | fdd4e15 | 2015-06-16 20:48:56 +0800 | [diff] [blame] | 630 | case SEEK_END: |
| 631 | retval = -EOPNOTSUPP; |
Josef Bacik | 06222e4 | 2011-07-18 13:21:38 -0400 | [diff] [blame] | 632 | default: |
| 633 | goto out; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 634 | } |
Josef Bacik | 06222e4 | 2011-07-18 13:21:38 -0400 | [diff] [blame] | 635 | |
Yan, Zheng | f049420 | 2014-02-27 16:26:24 +0800 | [diff] [blame] | 636 | if (offset >= 0) { |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 637 | if (need_reset_readdir(dfi, offset)) { |
Yan, Zheng | f3c4ebe | 2016-04-29 11:27:30 +0800 | [diff] [blame] | 638 | dout("dir_llseek dropping %p content\n", file); |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 639 | reset_readdir(dfi); |
Yan, Zheng | f3c4ebe | 2016-04-29 11:27:30 +0800 | [diff] [blame] | 640 | } else if (is_hash_order(offset) && offset > file->f_pos) { |
| 641 | /* for hash offset, we don't know if a forward seek |
| 642 | * is within same frag */ |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 643 | dfi->dir_release_count = 0; |
| 644 | dfi->readdir_cache_idx = -1; |
Yan, Zheng | f3c4ebe | 2016-04-29 11:27:30 +0800 | [diff] [blame] | 645 | } |
| 646 | |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 647 | if (offset != file->f_pos) { |
| 648 | file->f_pos = offset; |
| 649 | file->f_version = 0; |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 650 | dfi->file_info.flags &= ~CEPH_F_ATEND; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 651 | } |
| 652 | retval = offset; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 653 | } |
Josef Bacik | 06222e4 | 2011-07-18 13:21:38 -0400 | [diff] [blame] | 654 | out: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 655 | inode_unlock(inode); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 656 | return retval; |
| 657 | } |
| 658 | |
| 659 | /* |
Sage Weil | 468640e | 2011-07-26 11:28:11 -0700 | [diff] [blame] | 660 | * Handle lookups for the hidden .snap directory. |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 661 | */ |
Sage Weil | 468640e | 2011-07-26 11:28:11 -0700 | [diff] [blame] | 662 | int ceph_handle_snapdir(struct ceph_mds_request *req, |
| 663 | struct dentry *dentry, int err) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 664 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 665 | struct ceph_fs_client *fsc = ceph_sb_to_client(dentry->d_sb); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 666 | struct inode *parent = d_inode(dentry->d_parent); /* we hold i_mutex */ |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 667 | |
| 668 | /* .snap dir? */ |
| 669 | if (err == -ENOENT && |
Sage Weil | 455cec0 | 2011-03-03 13:44:35 -0800 | [diff] [blame] | 670 | ceph_snap(parent) == CEPH_NOSNAP && |
Sage Weil | 6b80518 | 2009-10-27 11:50:50 -0700 | [diff] [blame] | 671 | strcmp(dentry->d_name.name, |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 672 | fsc->mount_options->snapdir_name) == 0) { |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 673 | struct inode *inode = ceph_get_snapdir(parent); |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 674 | dout("ENOENT on snapdir %p '%pd', linking to snapdir %p\n", |
| 675 | dentry, dentry, inode); |
Sage Weil | 9358c6d | 2010-03-30 13:54:41 -0700 | [diff] [blame] | 676 | BUG_ON(!d_unhashed(dentry)); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 677 | d_add(dentry, inode); |
| 678 | err = 0; |
| 679 | } |
Sage Weil | 468640e | 2011-07-26 11:28:11 -0700 | [diff] [blame] | 680 | return err; |
| 681 | } |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 682 | |
Sage Weil | 468640e | 2011-07-26 11:28:11 -0700 | [diff] [blame] | 683 | /* |
| 684 | * Figure out final result of a lookup/open request. |
| 685 | * |
| 686 | * Mainly, make sure we return the final req->r_dentry (if it already |
| 687 | * existed) in place of the original VFS-provided dentry when they |
| 688 | * differ. |
| 689 | * |
| 690 | * Gracefully handle the case where the MDS replies with -ENOENT and |
| 691 | * no trace (which it may do, at its discretion, e.g., if it doesn't |
| 692 | * care to issue a lease on the negative dentry). |
| 693 | */ |
| 694 | struct dentry *ceph_finish_lookup(struct ceph_mds_request *req, |
| 695 | struct dentry *dentry, int err) |
| 696 | { |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 697 | if (err == -ENOENT) { |
| 698 | /* no trace? */ |
| 699 | err = 0; |
| 700 | if (!req->r_reply_info.head->is_dentry) { |
| 701 | dout("ENOENT and no trace, dentry %p inode %p\n", |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 702 | dentry, d_inode(dentry)); |
| 703 | if (d_really_is_positive(dentry)) { |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 704 | d_drop(dentry); |
| 705 | err = -ENOENT; |
| 706 | } else { |
| 707 | d_add(dentry, NULL); |
| 708 | } |
| 709 | } |
| 710 | } |
| 711 | if (err) |
| 712 | dentry = ERR_PTR(err); |
| 713 | else if (dentry != req->r_dentry) |
| 714 | dentry = dget(req->r_dentry); /* we got spliced */ |
| 715 | else |
| 716 | dentry = NULL; |
| 717 | return dentry; |
| 718 | } |
| 719 | |
Zhang Zhuoyu | 3b33f69 | 2016-03-25 05:18:39 -0400 | [diff] [blame] | 720 | static bool is_root_ceph_dentry(struct inode *inode, struct dentry *dentry) |
Sage Weil | 1d1de916 | 2009-12-02 11:54:25 -0800 | [diff] [blame] | 721 | { |
| 722 | return ceph_ino(inode) == CEPH_INO_ROOT && |
| 723 | strncmp(dentry->d_name.name, ".ceph", 5) == 0; |
| 724 | } |
| 725 | |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 726 | /* |
| 727 | * Look up a single dir entry. If there is a lookup intent, inform |
| 728 | * the MDS so that it gets our 'caps wanted' value in a single op. |
| 729 | */ |
| 730 | static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 731 | unsigned int flags) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 732 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 733 | struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb); |
| 734 | struct ceph_mds_client *mdsc = fsc->mdsc; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 735 | struct ceph_mds_request *req; |
| 736 | int op; |
Yan, Zheng | 315f240 | 2016-03-07 10:34:50 +0800 | [diff] [blame] | 737 | int mask; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 738 | int err; |
| 739 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 740 | dout("lookup %p dentry %p '%pd'\n", |
| 741 | dir, dentry, dentry); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 742 | |
| 743 | if (dentry->d_name.len > NAME_MAX) |
| 744 | return ERR_PTR(-ENAMETOOLONG); |
| 745 | |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 746 | /* can we conclude ENOENT locally? */ |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 747 | if (d_really_is_negative(dentry)) { |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 748 | struct ceph_inode_info *ci = ceph_inode(dir); |
| 749 | struct ceph_dentry_info *di = ceph_dentry(dentry); |
| 750 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 751 | spin_lock(&ci->i_ceph_lock); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 752 | dout(" dir %p flags are %d\n", dir, ci->i_ceph_flags); |
| 753 | if (strncmp(dentry->d_name.name, |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 754 | fsc->mount_options->snapdir_name, |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 755 | dentry->d_name.len) && |
Sage Weil | 1d1de916 | 2009-12-02 11:54:25 -0800 | [diff] [blame] | 756 | !is_root_ceph_dentry(dir, dentry) && |
Yan, Zheng | e2c3de0 | 2015-03-04 16:05:04 +0800 | [diff] [blame] | 757 | ceph_test_mount_opt(fsc, DCACHE) && |
Yan, Zheng | 2f276c5 | 2013-03-13 19:44:32 +0800 | [diff] [blame] | 758 | __ceph_dir_is_complete(ci) && |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 759 | (__ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1))) { |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 760 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 761 | dout(" dir %p complete, -ENOENT\n", dir); |
| 762 | d_add(dentry, NULL); |
Yan, Zheng | 97aeb6b | 2017-11-27 10:47:46 +0800 | [diff] [blame] | 763 | di->lease_shared_gen = atomic_read(&ci->i_shared_gen); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 764 | return NULL; |
| 765 | } |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 766 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | op = ceph_snap(dir) == CEPH_SNAPDIR ? |
| 770 | CEPH_MDS_OP_LOOKUPSNAP : CEPH_MDS_OP_LOOKUP; |
| 771 | req = ceph_mdsc_create_request(mdsc, op, USE_ANY_MDS); |
| 772 | if (IS_ERR(req)) |
Julia Lawall | 7e34bc5 | 2010-05-22 12:01:14 +0200 | [diff] [blame] | 773 | return ERR_CAST(req); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 774 | req->r_dentry = dget(dentry); |
| 775 | req->r_num_caps = 2; |
Yan, Zheng | 315f240 | 2016-03-07 10:34:50 +0800 | [diff] [blame] | 776 | |
| 777 | mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED; |
| 778 | if (ceph_security_xattr_wanted(dir)) |
| 779 | mask |= CEPH_CAP_XATTR_SHARED; |
| 780 | req->r_args.getattr.mask = cpu_to_le32(mask); |
| 781 | |
Jeff Layton | 3dd69aa | 2017-01-31 10:28:26 -0500 | [diff] [blame] | 782 | req->r_parent = dir; |
| 783 | set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 784 | err = ceph_mdsc_do_request(mdsc, NULL, req); |
Sage Weil | 468640e | 2011-07-26 11:28:11 -0700 | [diff] [blame] | 785 | err = ceph_handle_snapdir(req, dentry, err); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 786 | dentry = ceph_finish_lookup(req, dentry, err); |
| 787 | ceph_mdsc_put_request(req); /* will dput(dentry) */ |
| 788 | dout("lookup result=%p\n", dentry); |
| 789 | return dentry; |
| 790 | } |
| 791 | |
| 792 | /* |
| 793 | * If we do a create but get no trace back from the MDS, follow up with |
| 794 | * a lookup (the VFS expects us to link up the provided dentry). |
| 795 | */ |
| 796 | int ceph_handle_notrace_create(struct inode *dir, struct dentry *dentry) |
| 797 | { |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 798 | struct dentry *result = ceph_lookup(dir, dentry, 0); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 799 | |
| 800 | if (result && !IS_ERR(result)) { |
| 801 | /* |
| 802 | * We created the item, then did a lookup, and found |
| 803 | * it was already linked to another inode we already |
Yan, Zheng | 4d41cef | 2015-02-04 15:10:48 +0800 | [diff] [blame] | 804 | * had in our cache (and thus got spliced). To not |
| 805 | * confuse VFS (especially when inode is a directory), |
| 806 | * we don't link our dentry to that inode, return an |
| 807 | * error instead. |
| 808 | * |
| 809 | * This event should be rare and it happens only when |
| 810 | * we talk to old MDS. Recent MDS does not send traceless |
| 811 | * reply for request that creates new inode. |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 812 | */ |
Yan, Zheng | 5cba372 | 2015-02-02 11:27:56 +0800 | [diff] [blame] | 813 | d_drop(result); |
Yan, Zheng | 4d41cef | 2015-02-04 15:10:48 +0800 | [diff] [blame] | 814 | return -ESTALE; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 815 | } |
| 816 | return PTR_ERR(result); |
| 817 | } |
| 818 | |
| 819 | static int ceph_mknod(struct inode *dir, struct dentry *dentry, |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 820 | umode_t mode, dev_t rdev) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 821 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 822 | struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb); |
| 823 | struct ceph_mds_client *mdsc = fsc->mdsc; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 824 | struct ceph_mds_request *req; |
Yan, Zheng | b1ee94a | 2014-09-16 20:35:17 +0800 | [diff] [blame] | 825 | struct ceph_acls_info acls = {}; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 826 | int err; |
| 827 | |
| 828 | if (ceph_snap(dir) != CEPH_NOSNAP) |
| 829 | return -EROFS; |
| 830 | |
Luis Henriques | b7a2921 | 2018-01-05 10:47:19 +0000 | [diff] [blame] | 831 | if (ceph_quota_is_max_files_exceeded(dir)) |
| 832 | return -EDQUOT; |
| 833 | |
Yan, Zheng | b1ee94a | 2014-09-16 20:35:17 +0800 | [diff] [blame] | 834 | err = ceph_pre_init_acls(dir, &mode, &acls); |
| 835 | if (err < 0) |
| 836 | return err; |
| 837 | |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 838 | dout("mknod in dir %p dentry %p mode 0%ho rdev %d\n", |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 839 | dir, dentry, mode, rdev); |
| 840 | req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_MKNOD, USE_AUTH_MDS); |
| 841 | if (IS_ERR(req)) { |
Yan, Zheng | b1ee94a | 2014-09-16 20:35:17 +0800 | [diff] [blame] | 842 | err = PTR_ERR(req); |
| 843 | goto out; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 844 | } |
| 845 | req->r_dentry = dget(dentry); |
| 846 | req->r_num_caps = 2; |
Jeff Layton | 3dd69aa | 2017-01-31 10:28:26 -0500 | [diff] [blame] | 847 | req->r_parent = dir; |
| 848 | set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 849 | req->r_args.mknod.mode = cpu_to_le32(mode); |
| 850 | req->r_args.mknod.rdev = cpu_to_le32(rdev); |
Yan, Zheng | 222b7f9 | 2017-11-23 17:47:15 +0800 | [diff] [blame] | 851 | req->r_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_AUTH_EXCL; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 852 | req->r_dentry_unless = CEPH_CAP_FILE_EXCL; |
Yan, Zheng | b1ee94a | 2014-09-16 20:35:17 +0800 | [diff] [blame] | 853 | if (acls.pagelist) { |
| 854 | req->r_pagelist = acls.pagelist; |
| 855 | acls.pagelist = NULL; |
| 856 | } |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 857 | err = ceph_mdsc_do_request(mdsc, dir, req); |
| 858 | if (!err && !req->r_reply_info.head->is_dentry) |
| 859 | err = ceph_handle_notrace_create(dir, dentry); |
| 860 | ceph_mdsc_put_request(req); |
Yan, Zheng | b1ee94a | 2014-09-16 20:35:17 +0800 | [diff] [blame] | 861 | out: |
Guangliang Zhao | 7221fe4 | 2013-11-11 15:18:03 +0800 | [diff] [blame] | 862 | if (!err) |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 863 | ceph_init_inode_acls(d_inode(dentry), &acls); |
Yan, Zheng | b20a95a | 2014-02-11 12:55:05 +0800 | [diff] [blame] | 864 | else |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 865 | d_drop(dentry); |
Yan, Zheng | b1ee94a | 2014-09-16 20:35:17 +0800 | [diff] [blame] | 866 | ceph_release_acls_info(&acls); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 867 | return err; |
| 868 | } |
| 869 | |
Al Viro | 4acdaf2 | 2011-07-26 01:42:34 -0400 | [diff] [blame] | 870 | static int ceph_create(struct inode *dir, struct dentry *dentry, umode_t mode, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 871 | bool excl) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 872 | { |
Miklos Szeredi | 2d83bde | 2012-06-05 15:10:25 +0200 | [diff] [blame] | 873 | return ceph_mknod(dir, dentry, mode, 0); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | static int ceph_symlink(struct inode *dir, struct dentry *dentry, |
| 877 | const char *dest) |
| 878 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 879 | struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb); |
| 880 | struct ceph_mds_client *mdsc = fsc->mdsc; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 881 | struct ceph_mds_request *req; |
| 882 | int err; |
| 883 | |
| 884 | if (ceph_snap(dir) != CEPH_NOSNAP) |
| 885 | return -EROFS; |
| 886 | |
Luis Henriques | b7a2921 | 2018-01-05 10:47:19 +0000 | [diff] [blame] | 887 | if (ceph_quota_is_max_files_exceeded(dir)) |
| 888 | return -EDQUOT; |
| 889 | |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 890 | dout("symlink in dir %p dentry %p to '%s'\n", dir, dentry, dest); |
| 891 | req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SYMLINK, USE_AUTH_MDS); |
| 892 | if (IS_ERR(req)) { |
Yan, Zheng | b1ee94a | 2014-09-16 20:35:17 +0800 | [diff] [blame] | 893 | err = PTR_ERR(req); |
| 894 | goto out; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 895 | } |
Yan, Zheng | 687265e | 2015-06-13 17:27:05 +0800 | [diff] [blame] | 896 | req->r_path2 = kstrdup(dest, GFP_KERNEL); |
Sanidhya Kashyap | a149bb9 | 2015-03-21 12:54:58 -0400 | [diff] [blame] | 897 | if (!req->r_path2) { |
| 898 | err = -ENOMEM; |
| 899 | ceph_mdsc_put_request(req); |
| 900 | goto out; |
| 901 | } |
Jeff Layton | 3dd69aa | 2017-01-31 10:28:26 -0500 | [diff] [blame] | 902 | req->r_parent = dir; |
| 903 | set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 904 | req->r_dentry = dget(dentry); |
| 905 | req->r_num_caps = 2; |
Yan, Zheng | 222b7f9 | 2017-11-23 17:47:15 +0800 | [diff] [blame] | 906 | req->r_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_AUTH_EXCL; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 907 | req->r_dentry_unless = CEPH_CAP_FILE_EXCL; |
| 908 | err = ceph_mdsc_do_request(mdsc, dir, req); |
| 909 | if (!err && !req->r_reply_info.head->is_dentry) |
| 910 | err = ceph_handle_notrace_create(dir, dentry); |
| 911 | ceph_mdsc_put_request(req); |
Yan, Zheng | b1ee94a | 2014-09-16 20:35:17 +0800 | [diff] [blame] | 912 | out: |
| 913 | if (err) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 914 | d_drop(dentry); |
| 915 | return err; |
| 916 | } |
| 917 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 918 | static int ceph_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 919 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 920 | struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb); |
| 921 | struct ceph_mds_client *mdsc = fsc->mdsc; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 922 | struct ceph_mds_request *req; |
Yan, Zheng | b1ee94a | 2014-09-16 20:35:17 +0800 | [diff] [blame] | 923 | struct ceph_acls_info acls = {}; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 924 | int err = -EROFS; |
| 925 | int op; |
| 926 | |
| 927 | if (ceph_snap(dir) == CEPH_SNAPDIR) { |
| 928 | /* mkdir .snap/foo is a MKSNAP */ |
| 929 | op = CEPH_MDS_OP_MKSNAP; |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 930 | dout("mksnap dir %p snap '%pd' dn %p\n", dir, |
| 931 | dentry, dentry); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 932 | } else if (ceph_snap(dir) == CEPH_NOSNAP) { |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 933 | dout("mkdir dir %p dn %p mode 0%ho\n", dir, dentry, mode); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 934 | op = CEPH_MDS_OP_MKDIR; |
| 935 | } else { |
| 936 | goto out; |
| 937 | } |
Yan, Zheng | b1ee94a | 2014-09-16 20:35:17 +0800 | [diff] [blame] | 938 | |
Yan, Zheng | 2596366 | 2018-01-12 16:26:17 +0800 | [diff] [blame^] | 939 | if (op == CEPH_MDS_OP_MKDIR && |
| 940 | ceph_quota_is_max_files_exceeded(dir)) { |
Luis Henriques | b7a2921 | 2018-01-05 10:47:19 +0000 | [diff] [blame] | 941 | err = -EDQUOT; |
| 942 | goto out; |
| 943 | } |
| 944 | |
Yan, Zheng | b1ee94a | 2014-09-16 20:35:17 +0800 | [diff] [blame] | 945 | mode |= S_IFDIR; |
| 946 | err = ceph_pre_init_acls(dir, &mode, &acls); |
| 947 | if (err < 0) |
| 948 | goto out; |
| 949 | |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 950 | req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS); |
| 951 | if (IS_ERR(req)) { |
| 952 | err = PTR_ERR(req); |
| 953 | goto out; |
| 954 | } |
| 955 | |
| 956 | req->r_dentry = dget(dentry); |
| 957 | req->r_num_caps = 2; |
Jeff Layton | 3dd69aa | 2017-01-31 10:28:26 -0500 | [diff] [blame] | 958 | req->r_parent = dir; |
| 959 | set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 960 | req->r_args.mkdir.mode = cpu_to_le32(mode); |
Yan, Zheng | 222b7f9 | 2017-11-23 17:47:15 +0800 | [diff] [blame] | 961 | req->r_dentry_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_AUTH_EXCL; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 962 | req->r_dentry_unless = CEPH_CAP_FILE_EXCL; |
Yan, Zheng | b1ee94a | 2014-09-16 20:35:17 +0800 | [diff] [blame] | 963 | if (acls.pagelist) { |
| 964 | req->r_pagelist = acls.pagelist; |
| 965 | acls.pagelist = NULL; |
| 966 | } |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 967 | err = ceph_mdsc_do_request(mdsc, dir, req); |
Yan, Zheng | 275dd19 | 2014-12-10 16:17:31 +0800 | [diff] [blame] | 968 | if (!err && |
| 969 | !req->r_reply_info.head->is_target && |
| 970 | !req->r_reply_info.head->is_dentry) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 971 | err = ceph_handle_notrace_create(dir, dentry); |
| 972 | ceph_mdsc_put_request(req); |
| 973 | out: |
Yan, Zheng | b20a95a | 2014-02-11 12:55:05 +0800 | [diff] [blame] | 974 | if (!err) |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 975 | ceph_init_inode_acls(d_inode(dentry), &acls); |
Yan, Zheng | b20a95a | 2014-02-11 12:55:05 +0800 | [diff] [blame] | 976 | else |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 977 | d_drop(dentry); |
Yan, Zheng | b1ee94a | 2014-09-16 20:35:17 +0800 | [diff] [blame] | 978 | ceph_release_acls_info(&acls); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 979 | return err; |
| 980 | } |
| 981 | |
| 982 | static int ceph_link(struct dentry *old_dentry, struct inode *dir, |
| 983 | struct dentry *dentry) |
| 984 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 985 | struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb); |
| 986 | struct ceph_mds_client *mdsc = fsc->mdsc; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 987 | struct ceph_mds_request *req; |
| 988 | int err; |
| 989 | |
| 990 | if (ceph_snap(dir) != CEPH_NOSNAP) |
| 991 | return -EROFS; |
| 992 | |
| 993 | dout("link in dir %p old_dentry %p dentry %p\n", dir, |
| 994 | old_dentry, dentry); |
| 995 | req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LINK, USE_AUTH_MDS); |
| 996 | if (IS_ERR(req)) { |
| 997 | d_drop(dentry); |
| 998 | return PTR_ERR(req); |
| 999 | } |
| 1000 | req->r_dentry = dget(dentry); |
| 1001 | req->r_num_caps = 2; |
Sage Weil | 4b58c9b19 | 2013-02-05 13:41:23 -0800 | [diff] [blame] | 1002 | req->r_old_dentry = dget(old_dentry); |
Jeff Layton | 3dd69aa | 2017-01-31 10:28:26 -0500 | [diff] [blame] | 1003 | req->r_parent = dir; |
| 1004 | set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1005 | req->r_dentry_drop = CEPH_CAP_FILE_SHARED; |
| 1006 | req->r_dentry_unless = CEPH_CAP_FILE_EXCL; |
Yan, Zheng | ad88f23 | 2013-07-21 20:25:25 +0800 | [diff] [blame] | 1007 | /* release LINK_SHARED on source inode (mds will lock it) */ |
Yan, Zheng | d19a0b5 | 2017-11-23 17:59:13 +0800 | [diff] [blame] | 1008 | req->r_old_inode_drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1009 | err = ceph_mdsc_do_request(mdsc, dir, req); |
Sage Weil | 70b666c | 2011-05-27 09:24:26 -0700 | [diff] [blame] | 1010 | if (err) { |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1011 | d_drop(dentry); |
Sage Weil | 70b666c | 2011-05-27 09:24:26 -0700 | [diff] [blame] | 1012 | } else if (!req->r_reply_info.head->is_dentry) { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1013 | ihold(d_inode(old_dentry)); |
| 1014 | d_instantiate(dentry, d_inode(old_dentry)); |
Sage Weil | 70b666c | 2011-05-27 09:24:26 -0700 | [diff] [blame] | 1015 | } |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1016 | ceph_mdsc_put_request(req); |
| 1017 | return err; |
| 1018 | } |
| 1019 | |
| 1020 | /* |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1021 | * rmdir and unlink are differ only by the metadata op code |
| 1022 | */ |
| 1023 | static int ceph_unlink(struct inode *dir, struct dentry *dentry) |
| 1024 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1025 | struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb); |
| 1026 | struct ceph_mds_client *mdsc = fsc->mdsc; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1027 | struct inode *inode = d_inode(dentry); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1028 | struct ceph_mds_request *req; |
| 1029 | int err = -EROFS; |
| 1030 | int op; |
| 1031 | |
| 1032 | if (ceph_snap(dir) == CEPH_SNAPDIR) { |
| 1033 | /* rmdir .snap/foo is RMSNAP */ |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 1034 | dout("rmsnap dir %p '%pd' dn %p\n", dir, dentry, dentry); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1035 | op = CEPH_MDS_OP_RMSNAP; |
| 1036 | } else if (ceph_snap(dir) == CEPH_NOSNAP) { |
| 1037 | dout("unlink/rmdir dir %p dn %p inode %p\n", |
| 1038 | dir, dentry, inode); |
David Howells | e36cb0b | 2015-01-29 12:02:35 +0000 | [diff] [blame] | 1039 | op = d_is_dir(dentry) ? |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1040 | CEPH_MDS_OP_RMDIR : CEPH_MDS_OP_UNLINK; |
| 1041 | } else |
| 1042 | goto out; |
| 1043 | req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS); |
| 1044 | if (IS_ERR(req)) { |
| 1045 | err = PTR_ERR(req); |
| 1046 | goto out; |
| 1047 | } |
| 1048 | req->r_dentry = dget(dentry); |
| 1049 | req->r_num_caps = 2; |
Jeff Layton | 3dd69aa | 2017-01-31 10:28:26 -0500 | [diff] [blame] | 1050 | req->r_parent = dir; |
| 1051 | set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1052 | req->r_dentry_drop = CEPH_CAP_FILE_SHARED; |
| 1053 | req->r_dentry_unless = CEPH_CAP_FILE_EXCL; |
Zhi Zhang | 6ef0bc6 | 2018-01-24 21:24:33 +0800 | [diff] [blame] | 1054 | req->r_inode_drop = ceph_drop_caps_for_unlink(inode); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1055 | err = ceph_mdsc_do_request(mdsc, dir, req); |
| 1056 | if (!err && !req->r_reply_info.head->is_dentry) |
| 1057 | d_delete(dentry); |
| 1058 | ceph_mdsc_put_request(req); |
| 1059 | out: |
| 1060 | return err; |
| 1061 | } |
| 1062 | |
| 1063 | static int ceph_rename(struct inode *old_dir, struct dentry *old_dentry, |
Miklos Szeredi | 1cd66c9 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 1064 | struct inode *new_dir, struct dentry *new_dentry, |
| 1065 | unsigned int flags) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1066 | { |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1067 | struct ceph_fs_client *fsc = ceph_sb_to_client(old_dir->i_sb); |
| 1068 | struct ceph_mds_client *mdsc = fsc->mdsc; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1069 | struct ceph_mds_request *req; |
Yan, Zheng | 0ea611a | 2015-04-07 15:36:32 +0800 | [diff] [blame] | 1070 | int op = CEPH_MDS_OP_RENAME; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1071 | int err; |
| 1072 | |
Miklos Szeredi | 1cd66c9 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 1073 | if (flags) |
| 1074 | return -EINVAL; |
| 1075 | |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1076 | if (ceph_snap(old_dir) != ceph_snap(new_dir)) |
| 1077 | return -EXDEV; |
Yan, Zheng | 0ea611a | 2015-04-07 15:36:32 +0800 | [diff] [blame] | 1078 | if (ceph_snap(old_dir) != CEPH_NOSNAP) { |
| 1079 | if (old_dir == new_dir && ceph_snap(old_dir) == CEPH_SNAPDIR) |
| 1080 | op = CEPH_MDS_OP_RENAMESNAP; |
| 1081 | else |
| 1082 | return -EROFS; |
| 1083 | } |
Luis Henriques | cafe21a | 2018-01-05 10:47:20 +0000 | [diff] [blame] | 1084 | /* don't allow cross-quota renames */ |
| 1085 | if ((old_dir != new_dir) && |
| 1086 | (!ceph_quota_is_same_realm(old_dir, new_dir))) |
| 1087 | return -EXDEV; |
| 1088 | |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1089 | dout("rename dir %p dentry %p to dir %p dentry %p\n", |
| 1090 | old_dir, old_dentry, new_dir, new_dentry); |
Yan, Zheng | 0ea611a | 2015-04-07 15:36:32 +0800 | [diff] [blame] | 1091 | req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1092 | if (IS_ERR(req)) |
| 1093 | return PTR_ERR(req); |
Sage Weil | 180061a | 2013-02-05 13:36:05 -0800 | [diff] [blame] | 1094 | ihold(old_dir); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1095 | req->r_dentry = dget(new_dentry); |
| 1096 | req->r_num_caps = 2; |
| 1097 | req->r_old_dentry = dget(old_dentry); |
Sage Weil | 180061a | 2013-02-05 13:36:05 -0800 | [diff] [blame] | 1098 | req->r_old_dentry_dir = old_dir; |
Jeff Layton | 3dd69aa | 2017-01-31 10:28:26 -0500 | [diff] [blame] | 1099 | req->r_parent = new_dir; |
| 1100 | set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1101 | req->r_old_dentry_drop = CEPH_CAP_FILE_SHARED; |
| 1102 | req->r_old_dentry_unless = CEPH_CAP_FILE_EXCL; |
| 1103 | req->r_dentry_drop = CEPH_CAP_FILE_SHARED; |
| 1104 | req->r_dentry_unless = CEPH_CAP_FILE_EXCL; |
| 1105 | /* release LINK_RDCACHE on source inode (mds will lock it) */ |
Yan, Zheng | d19a0b5 | 2017-11-23 17:59:13 +0800 | [diff] [blame] | 1106 | req->r_old_inode_drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL; |
Zhi Zhang | 6ef0bc6 | 2018-01-24 21:24:33 +0800 | [diff] [blame] | 1107 | if (d_really_is_positive(new_dentry)) { |
| 1108 | req->r_inode_drop = |
| 1109 | ceph_drop_caps_for_unlink(d_inode(new_dentry)); |
| 1110 | } |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1111 | err = ceph_mdsc_do_request(mdsc, old_dir, req); |
| 1112 | if (!err && !req->r_reply_info.head->is_dentry) { |
| 1113 | /* |
| 1114 | * Normally d_move() is done by fill_trace (called by |
| 1115 | * do_request, above). If there is no trace, we need |
| 1116 | * to do it here. |
| 1117 | */ |
| 1118 | d_move(old_dentry, new_dentry); |
| 1119 | } |
| 1120 | ceph_mdsc_put_request(req); |
| 1121 | return err; |
| 1122 | } |
| 1123 | |
Sage Weil | 81a6cf2 | 2010-05-14 09:35:38 -0700 | [diff] [blame] | 1124 | /* |
| 1125 | * Ensure a dentry lease will no longer revalidate. |
| 1126 | */ |
| 1127 | void ceph_invalidate_dentry_lease(struct dentry *dentry) |
| 1128 | { |
| 1129 | spin_lock(&dentry->d_lock); |
Miklos Szeredi | 9b16f03c | 2016-06-22 16:35:04 +0200 | [diff] [blame] | 1130 | ceph_dentry(dentry)->time = jiffies; |
Sage Weil | 81a6cf2 | 2010-05-14 09:35:38 -0700 | [diff] [blame] | 1131 | ceph_dentry(dentry)->lease_shared_gen = 0; |
| 1132 | spin_unlock(&dentry->d_lock); |
| 1133 | } |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1134 | |
| 1135 | /* |
| 1136 | * Check if dentry lease is valid. If not, delete the lease. Try to |
| 1137 | * renew if the least is more than half up. |
| 1138 | */ |
Jeff Layton | 14fb9c9 | 2016-07-01 09:39:20 -0400 | [diff] [blame] | 1139 | static int dentry_lease_is_valid(struct dentry *dentry, unsigned int flags, |
| 1140 | struct inode *dir) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1141 | { |
| 1142 | struct ceph_dentry_info *di; |
| 1143 | struct ceph_mds_session *s; |
| 1144 | int valid = 0; |
| 1145 | u32 gen; |
| 1146 | unsigned long ttl; |
| 1147 | struct ceph_mds_session *session = NULL; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1148 | u32 seq = 0; |
| 1149 | |
| 1150 | spin_lock(&dentry->d_lock); |
| 1151 | di = ceph_dentry(dentry); |
Jeff Layton | 14fb9c9 | 2016-07-01 09:39:20 -0400 | [diff] [blame] | 1152 | if (di && di->lease_session) { |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1153 | s = di->lease_session; |
Alex Elder | d8fb02a | 2012-01-12 17:48:10 -0800 | [diff] [blame] | 1154 | spin_lock(&s->s_gen_ttl_lock); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1155 | gen = s->s_cap_gen; |
| 1156 | ttl = s->s_cap_ttl; |
Alex Elder | d8fb02a | 2012-01-12 17:48:10 -0800 | [diff] [blame] | 1157 | spin_unlock(&s->s_gen_ttl_lock); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1158 | |
| 1159 | if (di->lease_gen == gen && |
Miklos Szeredi | 9b16f03c | 2016-06-22 16:35:04 +0200 | [diff] [blame] | 1160 | time_before(jiffies, di->time) && |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1161 | time_before(jiffies, ttl)) { |
| 1162 | valid = 1; |
| 1163 | if (di->lease_renew_after && |
| 1164 | time_after(jiffies, di->lease_renew_after)) { |
Jeff Layton | 14fb9c9 | 2016-07-01 09:39:20 -0400 | [diff] [blame] | 1165 | /* |
| 1166 | * We should renew. If we're in RCU walk mode |
| 1167 | * though, we can't do that so just return |
| 1168 | * -ECHILD. |
| 1169 | */ |
| 1170 | if (flags & LOOKUP_RCU) { |
| 1171 | valid = -ECHILD; |
| 1172 | } else { |
| 1173 | session = ceph_get_mds_session(s); |
| 1174 | seq = di->lease_seq; |
| 1175 | di->lease_renew_after = 0; |
| 1176 | di->lease_renew_from = jiffies; |
| 1177 | } |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1178 | } |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1179 | } |
| 1180 | } |
| 1181 | spin_unlock(&dentry->d_lock); |
| 1182 | |
| 1183 | if (session) { |
| 1184 | ceph_mdsc_lease_send_msg(session, dir, dentry, |
| 1185 | CEPH_MDS_LEASE_RENEW, seq); |
| 1186 | ceph_put_mds_session(session); |
| 1187 | } |
| 1188 | dout("dentry_lease_is_valid - dentry %p = %d\n", dentry, valid); |
| 1189 | return valid; |
| 1190 | } |
| 1191 | |
| 1192 | /* |
| 1193 | * Check if directory-wide content lease/cap is valid. |
| 1194 | */ |
| 1195 | static int dir_lease_is_valid(struct inode *dir, struct dentry *dentry) |
| 1196 | { |
| 1197 | struct ceph_inode_info *ci = ceph_inode(dir); |
| 1198 | struct ceph_dentry_info *di = ceph_dentry(dentry); |
| 1199 | int valid = 0; |
| 1200 | |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1201 | spin_lock(&ci->i_ceph_lock); |
Yan, Zheng | 97aeb6b | 2017-11-27 10:47:46 +0800 | [diff] [blame] | 1202 | if (atomic_read(&ci->i_shared_gen) == di->lease_shared_gen) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1203 | valid = __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1); |
Sage Weil | be65559 | 2011-11-30 09:47:09 -0800 | [diff] [blame] | 1204 | spin_unlock(&ci->i_ceph_lock); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1205 | dout("dir_lease_is_valid dir %p v%u dentry %p v%u = %d\n", |
Yan, Zheng | 97aeb6b | 2017-11-27 10:47:46 +0800 | [diff] [blame] | 1206 | dir, (unsigned)atomic_read(&ci->i_shared_gen), |
| 1207 | dentry, (unsigned)di->lease_shared_gen, valid); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1208 | return valid; |
| 1209 | } |
| 1210 | |
| 1211 | /* |
| 1212 | * Check if cached dentry can be trusted. |
| 1213 | */ |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 1214 | static int ceph_d_revalidate(struct dentry *dentry, unsigned int flags) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1215 | { |
Sage Weil | bf1c6ac | 2011-07-26 11:30:43 -0700 | [diff] [blame] | 1216 | int valid = 0; |
Yan, Zheng | 641235d | 2016-03-16 16:40:23 +0800 | [diff] [blame] | 1217 | struct dentry *parent; |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 1218 | struct inode *dir; |
| 1219 | |
Jeff Layton | f49d1e0 | 2016-07-01 09:39:21 -0400 | [diff] [blame] | 1220 | if (flags & LOOKUP_RCU) { |
Seraphime Kirkovski | 52953d5 | 2016-12-26 10:26:34 +0100 | [diff] [blame] | 1221 | parent = READ_ONCE(dentry->d_parent); |
Jeff Layton | f49d1e0 | 2016-07-01 09:39:21 -0400 | [diff] [blame] | 1222 | dir = d_inode_rcu(parent); |
| 1223 | if (!dir) |
| 1224 | return -ECHILD; |
| 1225 | } else { |
| 1226 | parent = dget_parent(dentry); |
| 1227 | dir = d_inode(parent); |
| 1228 | } |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 1229 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 1230 | dout("d_revalidate %p '%pd' inode %p offset %lld\n", dentry, |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1231 | dentry, d_inode(dentry), ceph_dentry(dentry)->offset); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1232 | |
| 1233 | /* always trust cached snapped dentries, snapdir dentry */ |
| 1234 | if (ceph_snap(dir) != CEPH_NOSNAP) { |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 1235 | dout("d_revalidate %p '%pd' inode %p is SNAPPED\n", dentry, |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1236 | dentry, d_inode(dentry)); |
Sage Weil | bf1c6ac | 2011-07-26 11:30:43 -0700 | [diff] [blame] | 1237 | valid = 1; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1238 | } else if (d_really_is_positive(dentry) && |
| 1239 | ceph_snap(d_inode(dentry)) == CEPH_SNAPDIR) { |
Sage Weil | bf1c6ac | 2011-07-26 11:30:43 -0700 | [diff] [blame] | 1240 | valid = 1; |
Jeff Layton | 14fb9c9 | 2016-07-01 09:39:20 -0400 | [diff] [blame] | 1241 | } else { |
| 1242 | valid = dentry_lease_is_valid(dentry, flags, dir); |
| 1243 | if (valid == -ECHILD) |
| 1244 | return valid; |
| 1245 | if (valid || dir_lease_is_valid(dir, dentry)) { |
| 1246 | if (d_really_is_positive(dentry)) |
| 1247 | valid = ceph_is_any_caps(d_inode(dentry)); |
| 1248 | else |
| 1249 | valid = 1; |
| 1250 | } |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1251 | } |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1252 | |
Yan, Zheng | 200fd27 | 2016-03-17 14:41:59 +0800 | [diff] [blame] | 1253 | if (!valid) { |
| 1254 | struct ceph_mds_client *mdsc = |
| 1255 | ceph_sb_to_client(dir->i_sb)->mdsc; |
| 1256 | struct ceph_mds_request *req; |
Jeff Layton | 1097680 | 2017-01-12 14:42:38 -0500 | [diff] [blame] | 1257 | int op, err; |
| 1258 | u32 mask; |
Yan, Zheng | 200fd27 | 2016-03-17 14:41:59 +0800 | [diff] [blame] | 1259 | |
Jeff Layton | f49d1e0 | 2016-07-01 09:39:21 -0400 | [diff] [blame] | 1260 | if (flags & LOOKUP_RCU) |
| 1261 | return -ECHILD; |
| 1262 | |
Yan, Zheng | 200fd27 | 2016-03-17 14:41:59 +0800 | [diff] [blame] | 1263 | op = ceph_snap(dir) == CEPH_SNAPDIR ? |
Jeff Layton | 5eb9f60 | 2017-01-30 09:47:25 -0500 | [diff] [blame] | 1264 | CEPH_MDS_OP_LOOKUPSNAP : CEPH_MDS_OP_LOOKUP; |
Yan, Zheng | 200fd27 | 2016-03-17 14:41:59 +0800 | [diff] [blame] | 1265 | req = ceph_mdsc_create_request(mdsc, op, USE_ANY_MDS); |
| 1266 | if (!IS_ERR(req)) { |
| 1267 | req->r_dentry = dget(dentry); |
Jeff Layton | 5eb9f60 | 2017-01-30 09:47:25 -0500 | [diff] [blame] | 1268 | req->r_num_caps = 2; |
| 1269 | req->r_parent = dir; |
Yan, Zheng | 200fd27 | 2016-03-17 14:41:59 +0800 | [diff] [blame] | 1270 | |
| 1271 | mask = CEPH_STAT_CAP_INODE | CEPH_CAP_AUTH_SHARED; |
| 1272 | if (ceph_security_xattr_wanted(dir)) |
| 1273 | mask |= CEPH_CAP_XATTR_SHARED; |
Jeff Layton | 1097680 | 2017-01-12 14:42:38 -0500 | [diff] [blame] | 1274 | req->r_args.getattr.mask = cpu_to_le32(mask); |
Yan, Zheng | 200fd27 | 2016-03-17 14:41:59 +0800 | [diff] [blame] | 1275 | |
Yan, Zheng | 200fd27 | 2016-03-17 14:41:59 +0800 | [diff] [blame] | 1276 | err = ceph_mdsc_do_request(mdsc, NULL, req); |
Jeff Layton | c3f4688 | 2016-11-30 15:56:46 -0500 | [diff] [blame] | 1277 | switch (err) { |
| 1278 | case 0: |
| 1279 | if (d_really_is_positive(dentry) && |
| 1280 | d_inode(dentry) == req->r_target_inode) |
| 1281 | valid = 1; |
| 1282 | break; |
| 1283 | case -ENOENT: |
| 1284 | if (d_really_is_negative(dentry)) |
| 1285 | valid = 1; |
| 1286 | /* Fallthrough */ |
| 1287 | default: |
| 1288 | break; |
Yan, Zheng | 200fd27 | 2016-03-17 14:41:59 +0800 | [diff] [blame] | 1289 | } |
| 1290 | ceph_mdsc_put_request(req); |
| 1291 | dout("d_revalidate %p lookup result=%d\n", |
| 1292 | dentry, err); |
| 1293 | } |
| 1294 | } |
| 1295 | |
Sage Weil | bf1c6ac | 2011-07-26 11:30:43 -0700 | [diff] [blame] | 1296 | dout("d_revalidate %p %s\n", dentry, valid ? "valid" : "invalid"); |
Yan, Zheng | 9215aee | 2013-11-30 12:47:41 +0800 | [diff] [blame] | 1297 | if (valid) { |
Sage Weil | bf1c6ac | 2011-07-26 11:30:43 -0700 | [diff] [blame] | 1298 | ceph_dentry_lru_touch(dentry); |
Yan, Zheng | 9215aee | 2013-11-30 12:47:41 +0800 | [diff] [blame] | 1299 | } else { |
| 1300 | ceph_dir_clear_complete(dir); |
Yan, Zheng | 9215aee | 2013-11-30 12:47:41 +0800 | [diff] [blame] | 1301 | } |
Yan, Zheng | 641235d | 2016-03-16 16:40:23 +0800 | [diff] [blame] | 1302 | |
Jeff Layton | f49d1e0 | 2016-07-01 09:39:21 -0400 | [diff] [blame] | 1303 | if (!(flags & LOOKUP_RCU)) |
| 1304 | dput(parent); |
Sage Weil | bf1c6ac | 2011-07-26 11:30:43 -0700 | [diff] [blame] | 1305 | return valid; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1306 | } |
| 1307 | |
| 1308 | /* |
Sage Weil | 147851d | 2011-03-15 14:57:41 -0700 | [diff] [blame] | 1309 | * Release our ceph_dentry_info. |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1310 | */ |
Sage Weil | 147851d | 2011-03-15 14:57:41 -0700 | [diff] [blame] | 1311 | static void ceph_d_release(struct dentry *dentry) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1312 | { |
| 1313 | struct ceph_dentry_info *di = ceph_dentry(dentry); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1314 | |
Sage Weil | 147851d | 2011-03-15 14:57:41 -0700 | [diff] [blame] | 1315 | dout("d_release %p\n", dentry); |
Sage Weil | 3d8eb7a | 2011-11-11 09:48:53 -0800 | [diff] [blame] | 1316 | ceph_dentry_lru_del(dentry); |
Jeff Layton | 5b484a5 | 2016-07-01 09:39:20 -0400 | [diff] [blame] | 1317 | |
| 1318 | spin_lock(&dentry->d_lock); |
| 1319 | dentry->d_fsdata = NULL; |
| 1320 | spin_unlock(&dentry->d_lock); |
| 1321 | |
Sage Weil | 3d8eb7a | 2011-11-11 09:48:53 -0800 | [diff] [blame] | 1322 | if (di->lease_session) |
| 1323 | ceph_put_mds_session(di->lease_session); |
| 1324 | kmem_cache_free(ceph_dentry_cachep, di); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1325 | } |
| 1326 | |
Sage Weil | b58dc41 | 2011-03-15 15:53:40 -0700 | [diff] [blame] | 1327 | /* |
| 1328 | * When the VFS prunes a dentry from the cache, we need to clear the |
| 1329 | * complete flag on the parent directory. |
| 1330 | * |
| 1331 | * Called under dentry->d_lock. |
| 1332 | */ |
| 1333 | static void ceph_d_prune(struct dentry *dentry) |
| 1334 | { |
Yan, Zheng | 5495c2d | 2017-11-27 11:23:48 +0800 | [diff] [blame] | 1335 | struct ceph_inode_info *dir_ci; |
| 1336 | struct ceph_dentry_info *di; |
| 1337 | |
| 1338 | dout("ceph_d_prune %pd %p\n", dentry, dentry); |
Sage Weil | b58dc41 | 2011-03-15 15:53:40 -0700 | [diff] [blame] | 1339 | |
| 1340 | /* do we have a valid parent? */ |
Sage Weil | 8842b3b | 2012-06-07 13:43:35 -0700 | [diff] [blame] | 1341 | if (IS_ROOT(dentry)) |
Sage Weil | b58dc41 | 2011-03-15 15:53:40 -0700 | [diff] [blame] | 1342 | return; |
| 1343 | |
Yan, Zheng | 5495c2d | 2017-11-27 11:23:48 +0800 | [diff] [blame] | 1344 | /* we hold d_lock, so d_parent is stable */ |
| 1345 | dir_ci = ceph_inode(d_inode(dentry->d_parent)); |
| 1346 | if (dir_ci->i_vino.snap == CEPH_SNAPDIR) |
Sage Weil | b58dc41 | 2011-03-15 15:53:40 -0700 | [diff] [blame] | 1347 | return; |
| 1348 | |
Yan, Zheng | 5495c2d | 2017-11-27 11:23:48 +0800 | [diff] [blame] | 1349 | /* who calls d_delete() should also disable dcache readdir */ |
| 1350 | if (d_really_is_negative(dentry)) |
Al Viro | 18fc8ab | 2016-10-28 21:52:50 -0400 | [diff] [blame] | 1351 | return; |
| 1352 | |
Yan, Zheng | 5495c2d | 2017-11-27 11:23:48 +0800 | [diff] [blame] | 1353 | /* d_fsdata does not get cleared until d_release */ |
| 1354 | if (!d_unhashed(dentry)) { |
| 1355 | __ceph_dir_clear_complete(dir_ci); |
| 1356 | return; |
| 1357 | } |
| 1358 | |
| 1359 | /* Disable dcache readdir just in case that someone called d_drop() |
| 1360 | * or d_invalidate(), but MDS didn't revoke CEPH_CAP_FILE_SHARED |
| 1361 | * properly (dcache readdir is still enabled) */ |
| 1362 | di = ceph_dentry(dentry); |
| 1363 | if (di->offset > 0 && |
| 1364 | di->lease_shared_gen == atomic_read(&dir_ci->i_shared_gen)) |
| 1365 | __ceph_dir_clear_ordered(dir_ci); |
Sage Weil | b58dc41 | 2011-03-15 15:53:40 -0700 | [diff] [blame] | 1366 | } |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1367 | |
| 1368 | /* |
| 1369 | * read() on a dir. This weird interface hack only works if mounted |
| 1370 | * with '-o dirstat'. |
| 1371 | */ |
| 1372 | static ssize_t ceph_read_dir(struct file *file, char __user *buf, size_t size, |
| 1373 | loff_t *ppos) |
| 1374 | { |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 1375 | struct ceph_dir_file_info *dfi = file->private_data; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1376 | struct inode *inode = file_inode(file); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1377 | struct ceph_inode_info *ci = ceph_inode(inode); |
| 1378 | int left; |
Sage Weil | ae59808 | 2011-05-12 14:28:05 -0700 | [diff] [blame] | 1379 | const int bufsize = 1024; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1380 | |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1381 | if (!ceph_test_mount_opt(ceph_sb_to_client(inode->i_sb), DIRSTAT)) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1382 | return -EISDIR; |
| 1383 | |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 1384 | if (!dfi->dir_info) { |
| 1385 | dfi->dir_info = kmalloc(bufsize, GFP_KERNEL); |
| 1386 | if (!dfi->dir_info) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1387 | return -ENOMEM; |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 1388 | dfi->dir_info_len = |
| 1389 | snprintf(dfi->dir_info, bufsize, |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1390 | "entries: %20lld\n" |
| 1391 | " files: %20lld\n" |
| 1392 | " subdirs: %20lld\n" |
| 1393 | "rentries: %20lld\n" |
| 1394 | " rfiles: %20lld\n" |
| 1395 | " rsubdirs: %20lld\n" |
| 1396 | "rbytes: %20lld\n" |
| 1397 | "rctime: %10ld.%09ld\n", |
| 1398 | ci->i_files + ci->i_subdirs, |
| 1399 | ci->i_files, |
| 1400 | ci->i_subdirs, |
| 1401 | ci->i_rfiles + ci->i_rsubdirs, |
| 1402 | ci->i_rfiles, |
| 1403 | ci->i_rsubdirs, |
| 1404 | ci->i_rbytes, |
| 1405 | (long)ci->i_rctime.tv_sec, |
| 1406 | (long)ci->i_rctime.tv_nsec); |
| 1407 | } |
| 1408 | |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 1409 | if (*ppos >= dfi->dir_info_len) |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1410 | return 0; |
Chengguang Xu | bb48bd4 | 2018-03-13 10:42:44 +0800 | [diff] [blame] | 1411 | size = min_t(unsigned, size, dfi->dir_info_len-*ppos); |
| 1412 | left = copy_to_user(buf, dfi->dir_info + *ppos, size); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1413 | if (left == size) |
| 1414 | return -EFAULT; |
| 1415 | *ppos += (size - left); |
| 1416 | return size - left; |
| 1417 | } |
| 1418 | |
| 1419 | /* |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1420 | * We maintain a private dentry LRU. |
| 1421 | * |
| 1422 | * FIXME: this needs to be changed to a per-mds lru to be useful. |
| 1423 | */ |
| 1424 | void ceph_dentry_lru_add(struct dentry *dn) |
| 1425 | { |
| 1426 | struct ceph_dentry_info *di = ceph_dentry(dn); |
| 1427 | struct ceph_mds_client *mdsc; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1428 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 1429 | dout("dentry_lru_add %p %p '%pd'\n", di, dn, dn); |
Sage Weil | 3d8eb7a | 2011-11-11 09:48:53 -0800 | [diff] [blame] | 1430 | mdsc = ceph_sb_to_client(dn->d_sb)->mdsc; |
| 1431 | spin_lock(&mdsc->dentry_lru_lock); |
| 1432 | list_add_tail(&di->lru, &mdsc->dentry_lru); |
| 1433 | mdsc->num_dentry++; |
| 1434 | spin_unlock(&mdsc->dentry_lru_lock); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1435 | } |
| 1436 | |
| 1437 | void ceph_dentry_lru_touch(struct dentry *dn) |
| 1438 | { |
| 1439 | struct ceph_dentry_info *di = ceph_dentry(dn); |
| 1440 | struct ceph_mds_client *mdsc; |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1441 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 1442 | dout("dentry_lru_touch %p %p '%pd' (offset %lld)\n", di, dn, dn, |
| 1443 | di->offset); |
Sage Weil | 3d8eb7a | 2011-11-11 09:48:53 -0800 | [diff] [blame] | 1444 | mdsc = ceph_sb_to_client(dn->d_sb)->mdsc; |
| 1445 | spin_lock(&mdsc->dentry_lru_lock); |
| 1446 | list_move_tail(&di->lru, &mdsc->dentry_lru); |
| 1447 | spin_unlock(&mdsc->dentry_lru_lock); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1448 | } |
| 1449 | |
| 1450 | void ceph_dentry_lru_del(struct dentry *dn) |
| 1451 | { |
| 1452 | struct ceph_dentry_info *di = ceph_dentry(dn); |
| 1453 | struct ceph_mds_client *mdsc; |
| 1454 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 1455 | dout("dentry_lru_del %p %p '%pd'\n", di, dn, dn); |
Sage Weil | 3d8eb7a | 2011-11-11 09:48:53 -0800 | [diff] [blame] | 1456 | mdsc = ceph_sb_to_client(dn->d_sb)->mdsc; |
| 1457 | spin_lock(&mdsc->dentry_lru_lock); |
| 1458 | list_del_init(&di->lru); |
| 1459 | mdsc->num_dentry--; |
| 1460 | spin_unlock(&mdsc->dentry_lru_lock); |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1461 | } |
| 1462 | |
Sage Weil | 6c0f3af | 2010-11-16 11:14:34 -0800 | [diff] [blame] | 1463 | /* |
| 1464 | * Return name hash for a given dentry. This is dependent on |
| 1465 | * the parent directory's hash function. |
| 1466 | */ |
Sage Weil | e5f86dc | 2011-07-26 11:30:55 -0700 | [diff] [blame] | 1467 | unsigned ceph_dentry_hash(struct inode *dir, struct dentry *dn) |
Sage Weil | 6c0f3af | 2010-11-16 11:14:34 -0800 | [diff] [blame] | 1468 | { |
Sage Weil | 6c0f3af | 2010-11-16 11:14:34 -0800 | [diff] [blame] | 1469 | struct ceph_inode_info *dci = ceph_inode(dir); |
| 1470 | |
| 1471 | switch (dci->i_dir_layout.dl_dir_hash) { |
| 1472 | case 0: /* for backward compat */ |
| 1473 | case CEPH_STR_HASH_LINUX: |
| 1474 | return dn->d_name.hash; |
| 1475 | |
| 1476 | default: |
| 1477 | return ceph_str_hash(dci->i_dir_layout.dl_dir_hash, |
| 1478 | dn->d_name.name, dn->d_name.len); |
| 1479 | } |
| 1480 | } |
| 1481 | |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1482 | const struct file_operations ceph_dir_fops = { |
| 1483 | .read = ceph_read_dir, |
Al Viro | 77acfa2 | 2013-05-17 16:52:26 -0400 | [diff] [blame] | 1484 | .iterate = ceph_readdir, |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1485 | .llseek = ceph_dir_llseek, |
| 1486 | .open = ceph_open, |
| 1487 | .release = ceph_release, |
| 1488 | .unlocked_ioctl = ceph_ioctl, |
Yan, Zheng | da819c8 | 2015-05-27 11:19:34 +0800 | [diff] [blame] | 1489 | .fsync = ceph_fsync, |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1490 | }; |
| 1491 | |
Yan, Zheng | 38c48b5 | 2015-01-14 13:46:04 +0800 | [diff] [blame] | 1492 | const struct file_operations ceph_snapdir_fops = { |
| 1493 | .iterate = ceph_readdir, |
| 1494 | .llseek = ceph_dir_llseek, |
| 1495 | .open = ceph_open, |
| 1496 | .release = ceph_release, |
| 1497 | }; |
| 1498 | |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1499 | const struct inode_operations ceph_dir_iops = { |
| 1500 | .lookup = ceph_lookup, |
| 1501 | .permission = ceph_permission, |
| 1502 | .getattr = ceph_getattr, |
| 1503 | .setattr = ceph_setattr, |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1504 | .listxattr = ceph_listxattr, |
Guangliang Zhao | 7221fe4 | 2013-11-11 15:18:03 +0800 | [diff] [blame] | 1505 | .get_acl = ceph_get_acl, |
Sage Weil | 72466d0 | 2014-01-29 06:22:25 -0800 | [diff] [blame] | 1506 | .set_acl = ceph_set_acl, |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1507 | .mknod = ceph_mknod, |
| 1508 | .symlink = ceph_symlink, |
| 1509 | .mkdir = ceph_mkdir, |
| 1510 | .link = ceph_link, |
| 1511 | .unlink = ceph_unlink, |
| 1512 | .rmdir = ceph_unlink, |
| 1513 | .rename = ceph_rename, |
| 1514 | .create = ceph_create, |
Miklos Szeredi | 2d83bde | 2012-06-05 15:10:25 +0200 | [diff] [blame] | 1515 | .atomic_open = ceph_atomic_open, |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1516 | }; |
| 1517 | |
Yan, Zheng | 38c48b5 | 2015-01-14 13:46:04 +0800 | [diff] [blame] | 1518 | const struct inode_operations ceph_snapdir_iops = { |
| 1519 | .lookup = ceph_lookup, |
| 1520 | .permission = ceph_permission, |
| 1521 | .getattr = ceph_getattr, |
| 1522 | .mkdir = ceph_mkdir, |
| 1523 | .rmdir = ceph_unlink, |
Yan, Zheng | 0ea611a | 2015-04-07 15:36:32 +0800 | [diff] [blame] | 1524 | .rename = ceph_rename, |
Yan, Zheng | 38c48b5 | 2015-01-14 13:46:04 +0800 | [diff] [blame] | 1525 | }; |
| 1526 | |
Sage Weil | 52dfb8a | 2010-08-03 10:25:30 -0700 | [diff] [blame] | 1527 | const struct dentry_operations ceph_dentry_ops = { |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1528 | .d_revalidate = ceph_d_revalidate, |
Sage Weil | 147851d | 2011-03-15 14:57:41 -0700 | [diff] [blame] | 1529 | .d_release = ceph_d_release, |
Sage Weil | b58dc41 | 2011-03-15 15:53:40 -0700 | [diff] [blame] | 1530 | .d_prune = ceph_d_prune, |
Al Viro | ad5cb12 | 2016-10-28 22:05:13 -0400 | [diff] [blame] | 1531 | .d_init = ceph_d_init, |
Sage Weil | 2817b00 | 2009-10-06 11:31:08 -0700 | [diff] [blame] | 1532 | }; |